标签:
1.简单备份脚本:
#!/bin/bash user="root" pwd="HuaShang@155!" dbs="charging" backuppath="/root/hssy_dbback" cd $backuppath backupname="$dbs"_$(date +%Y-%m-%d) mysqldump -u$user -p$pwd $dbs >$backupname.sql if [ "$?" == "0" ] then echo $(date +"%Y-%m-%d %H:%M:%S")" ============ $dbs =========== mysqldump sucess =========">>./log/backup.log else echo $(date +"%Y-%m-%d %H:%M:%S")" >++++++++++> $dbs +++++++++++> mysqldump failed ++++++++>">>./log/backup.log exit 0 fi
2.脚本1改进版(适合多个数据库同一脚本备份):
由于我有两个数据库一个charging 一个charging2 ,为了方便对两个数据库备份,做了如下改进,让脚本运行的时候带入参数。这样我执行./db_backup.sh 2 就可以备份charging2了。当然还可以带入跟多参数对单个表进行备份等,留待以后研究。
ps:
代码如下:
#!/bin/bash user="root" pwd="***" if [ "$1"=="2" ] then dbs="charging2" else dbs="charging" exit 0 fi backuppath="/root/hssy_dbback" cd $backuppath backupname="$dbs"_$(date +%Y-%m-%d) mysqldump -u$user -p$pwd $dbs >$backupname.sql if [ "$?"=="0" ] then echo $(date +"%Y-%m-%d %H:%M:%S")" ============ $dbs =========== mysqldump sucess =========">>./log/backup.log else echo $(date +"%Y-%m-%d %H:%M:%S")" >++++++++++> $dbs +++++++++++> mysqldump failed ++++++++>">>./log/backup.log exit 0 fi
参考:http://w55554.blog.51cto.com/947626/1132079
3.远程备份、压缩定时删除(保留一个月的记录)
#!/bin/bash host=xxx.xxx.xxx.xxx user="gprs" pwd="***" dbs="gprs" backuppath="/home/gprs_data_backup" day=30 #[ !-d $backuppath ]&&mkdir -p $backuppath cd $backuppath backupname="$dbs"_$(date +%Y-%m-%d) mysqldump -h$host -u$user -p$pwd $dbs >$backupname.sql if [ "$?"=="0" ] then echo $(date +"%Y-%m-%d %H:%M:%S")" ============ $dbs =========== mysqldump sucess =========">>./log/backup.log else echo $(date +"%Y-%m-%d %H:%M:%S")" >++++++++++> $dbs +++++++++++> mysqldump failed ++++++++>">>./log/backup.log exit 0 fi tar -czf $backupname.tar.gz *.sql if [ "$?"=="0" ] then echo $(date +"%Y-%m-%d %H:%M:%S")" ============ tar sucess =========">>./log/backup.log else echo $(date +"%Y-%m-%d %H:%M:%S")" >++++++++++> tar failed +++++++++>">>./log/backup.log exit 0 fi rm -f *.sql delname=gprs_$(date -d "$day day ago" +%Y-%m-%d).tar.gz rm -f $delname
标签:
原文地址:http://www.cnblogs.com/hzrong/p/4284290.html