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