Database backup Scripts

Source: Internet
Author: User

1. Simple Backup script:

#!/bin/bashUser="Root"pwd="[Email protected]!"DBS="Charging"Backuppath="/root/hssy_dbback"CD$backuppathBackupName="$dbs"_$ (Date +%y-%m-%d) mysqldump-U$user-P$pwd $dbs>$backupname. SQLif["$?" =="0"]then Echo$ (date +"%y-%m-%d%h:%m:%s")"============ $dbs =========== mysqldump sucess =========">>./log/Backup.logElseEcho$ (date +"%y-%m-%d%h:%m:%s")">++++++++++> $dbs +++++++++++> mysqldump failed ++++++++>">>./log/Backup.log Exit0fi

2. Script 1 Improved version (for multiple databases with the same script backup):

Since I have two databases a charging a charging2, in order to facilitate the backup of two databases, made the following improvements, let the script run into parameters. So I execute./db_backup.sh 2 can back up the charging2. Of course, you can also bring in with the multi-parameter to the single table for backup, etc., for later study.

Ps:

$ A is the name of the script file, "$" is the first parameter, and $2nd ..., $9 will need to be bracketed, such as ${10},${11},${12} ...

The code is as follows:

#!/bin/bashUser="Root"pwd="***"if[" $"=="2"]then DBS="charging2"ElseDBS="Charging"Exit0Fibackuppath="/root/hssy_dbback"CD$backuppathBackupName="$dbs"_$ (Date +%y-%m-%d) mysqldump-U$user-P$pwd $dbs>$backupname. SQLif["$?"=="0"]then Echo$ (date +"%y-%m-%d%h:%m:%s")"============ $dbs =========== mysqldump sucess =========">>./log/Backup.logElseEcho$ (date +"%y-%m-%d%h:%m:%s")">++++++++++> $dbs +++++++++++> mysqldump failed ++++++++>">>./log/Backup.log Exit0fi

Reference: http://w55554.blog.51cto.com/947626/1132079

3. Remote backup, compress scheduled Delete (keep one months of records)

#!/bin/bashhost=Xxx.xxx.xxx.xxxuser="GPRS"pwd="***"DBS="GPRS"Backuppath="/home/gprs_data_backup" Day=30#[!-d $backuppath]&&mkdir-p $backuppathCd$backuppathBackupName="$dbs"_$ (Date +%y-%m-%d) mysqldump-H$host-U$user-P$pwd $dbs>$backupname. SQLif["$?"=="0"]then Echo$ (date +"%y-%m-%d%h:%m:%s")"============ $dbs =========== mysqldump sucess =========">>./log/Backup.logElseEcho$ (date +"%y-%m-%d%h:%m:%s")">++++++++++> $dbs +++++++++++> mysqldump failed ++++++++>">>./log/Backup.log Exit0Fitar-czf$backupname. tar.gz *. SQLif["$?"=="0"]then Echo$ (date +"%y-%m-%d%h:%m:%s")"============ tar sucess =========">>./log/Backup.logElseEcho$ (date +"%y-%m-%d%h:%m:%s")">++++++++++> tar failed +++++++++>">>./log/Backup.log Exit0firm- F*. Sqldelname=gprs_$ (date-d"$day day ago"+%y-%m-%d). Tar.gzrm- F $delname

Database backup Scripts

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.