MySQL多个库备份脚本,shell数组方式完成
#!/bin/bash
time=`date '+%F'`
nowtime=`date +"%F %T"`
cd /data/dbbak
dbs=(db1 db2 db3)
dbnum=${#dbs[*]}
n=$((dbnum-1))
#echo $n
for i in `seq 0 $n`
do
echo $i
echo "$nowtime begin backup database ${dbs[$i]} ..." >> backup.log
/usr/local/mysql/bin/mysqldump -uroot -ppasswd -hlocalhost --master-data --default-character-set=utf8 --skip-lock-tables ${dbs[$i]} > ${dbs[$i]}-${time}.sql
tar czvf ${dbs[$i]}-${time}.sql.tar.gz ${dbs[$i]}-${time}.sql
rm -rf ${dbs[$i]}-${time}.sql
endtime=`date +"%F %T"`
echo "$endtime finish backup database ${dbs[$i]} ..." >> backup.log
done
查看日志,运行结果: