多个数据库同时备份脚本
# vi back.sh
#!/bin/bash
Date=`date +%Y%m%d`
olddate=`date +%Y%m%d -d "-7 days"`
db_user='root'
db_passwd='123456'
db_name='mysql match user'
mkdir /usr/local/apache/back/$Date
olddate=`date +%Y%m%d -d "-7 days"`
db_user='root'
db_passwd='123456'
db_name='mysql match user'
mkdir /usr/local/apache/back/$Date
mkdir /logs/$Date
rm -rf /logs/$olddate
rm -rf /usr/local/apache/back/$olddate
for i in $db_name
do
rm -rf /usr/local/apache/back/$olddate
for i in $db_name
do
echo "$i back begin at `date +%H:%M:%S`" >> /logs/$Date/back.log
mysqldump -u $db_user -p$db_passwd $i | gzip > /usr/local/apache/back/$Date/$i-$(date +%y%m%d).sql.gz
mysqldump -u $db_user -p$db_passwd $i | gzip > /usr/local/apache/back/$Date/$i-$(date +%y%m%d).sql.gz
echo "$i back begin at `date +%H:%M:%S`" >> /logs/$Date/back.log
done
done
echo "all back over!" >> /logs/$Date/back.log
脚本注释:
1、同时备份数据库mysql、match、user,并压缩成.gz文件,并删除7天前的备份文件,备份文件格式为mysql-120313.sql.gz(即2012年3月13日)
2、记录数据库开始备份的时间和结束备份的时间(几点几分几秒)
转载于:https://blog.51cto.com/yangzhiming/834289