Not much to say directly on the script
#/bin/bashuser=multiversepasswd=Multiverse1113back_path=/var/opt/backup/db_name=multiverseback_time=`date +%Y%m%d_%H%M%S`back_filename=$back_path$db_name$back_timedel_time=`date -d "2 day ago" +"%Y%m%d"`del_backfile=$back_path$db_name$del_timesqlcmd -S localhost -U $user -P $passwd -d master -Q "BACKUP DATABASE $db_name to disk=‘$back_filename.bak‘"tar -zcPf $back_filename.tar.gz $back_filename.bakrm -f $back_filename.bakif [ -e $back_filename.tar.gz ];then rm -rf $del_backfile*.gz echo "database[multiverse] backup success! "else echo "database[multiverse] backup failed!"fi
The command to back up the database is as follows:
Backup database TestDB to disk= '/opt/dbbackup/testdb.bak '
This command backs up the database TestDB to/opt/dbbackup/testdb.bak. Note that it is important to ensure that the MSSQL account has permission to operate the/opt/dbbackup directory.
There are two methods of backup:
The command to restore the database is as follows:
Restore database TestDB from disk= '/opt/dbbackup/testdb.bak '
This is used in cases where the database does not exist. If the database exists, you need to overwrite it with the following command:
Restore database TestDB from disk= '/opt/dbbackup/testdb.bak ' with replace
SQL Server also has two operations: detaching a database and attaching a database.
First, detach the database:
sp_detach_db TestDB
So, where are the separated files? In fact, do not worry at all, it is too easy to find a file under the Linux system.
This is the two files that can be copied out to a different machine for backup.
The command to attach a database is slightly more complex, but it is also better understood.
sp_attach_db @dbname=TestDB,@filename1=N‘/opt/dbbackup/TestDB.mdf‘,@filename2=N‘/opt/dbbackup/TestDB_log.ldf‘
SQL Server for Linux automatic backup database script