SQL Server for Linux automatic backup database script

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.