数据库备份脚本
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
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。