MySQL backup script
#!/bin/bash ## author: liwenlong ## 2015 December 29 ## description: backup file, smart child row card database backup ######### ###### #数据库备份脚本 ####################### user=backup # #数据库连接用户 PASSWD= "* * * " # #数据库连接密码 database=" test test3 Test4 " # #数据库名, if you want to add a database to be backed up, hostname=" 192.168.1.47 " # #服务器IP地址 port=" 3306 " # #数据库端口 backup_dir=/home/backup/mysql_backup/ # #备份目录 option= " -h$HOSTNAME -P $PORT -u$ user -p$passwd " time= ' date +%f-%h:%m:%s ' # #年月日时分秒 backup_log_file=/var/log/mysql_backup.log # #日志文件, create delete_log_file=/var/log/mysql_ manually delete.log # #日志文件, manually create ###################################################### ################# #创建数据库备Catalogue ################## for i in $DATABASE ; do # #如果目录不存在, creating directories if [ ! -d $BACKUP _dir$i ] ; then mkdir -p $BACKUP _dir$i fi done ############## ########### #备份数据库 ################## for i in $DATABASE ; do mysqldump $OPTION $i | bzip2 -c > $BACKUP _dir$i/$i-$TIME. sql.bz2 if [[ $? == 0 ]]; then #如果数据库备份成功, write log file; echo " $BACKUP _dir$i backup successful! $TIME " >> $BACKUP _log_file else # #否则写入错误日志文件; echo
" $BACKUP _dir$i backup fail... $TIME " >> $BACKUP _log_file fi Done ########## #删除一个月之前的备份文件 ######################## for i in $DATABASE ; do find $ Backup_dir$i/ -mtime +3 -type f | xargs rm -f if [[ $? = =&nBsp;0 ]]; then # #删除一个月前的数据库, writing log files echo " $BACKUP _ dir$i delete successful! $TIME " >> $DELETE _log_file else echo " $BACKUP _dir$i delete fail... $TIME " >> $DELETE _log_file fi Done
This article is from the "obsessed" blog, make sure to keep this source http://dragondragon.blog.51cto.com/6170889/1729577
MySQL Database backup script