MySQL备份的shell脚本
经过测试该脚本可以远程备份,但需要配置远程登录用户的权限,经过测试啊,在把这个脚本添加到计划任务的时候是无法识别mysql命令的(即使是将mysql添加到环境变量也无法识别,是因为/etc/crontab这个配置文件中的环境变量的问题,将MySQL的环境变量添加进去就可以了),所以添加到定时任务中的时候需要给命令绝对路径
#!/bin/bash
# Shell script to backup MySql database
# Author:fansik
# Date 2016-6-23
# Database info
dbuser="root"
dbpasswd="hzsite"
host="127.0.0.1"
port="3306"
#backup dir
if [ ! -d /backup ]; then
mkdir /backup
fi
backupdir=/backup
#datetime
datetime="$(date +"%Y-%m-%d")"
# Database list
dblist="$(/usr/local/mysql/bin/mysql -u$dbuser -p$dbpasswd -h$host -P$port -Bse 'show databases')"
# use for cycle backup ervery database
for dbname in $dblist
do
/usr/local/mysql/bin/mysqldump -u$dbuser -p$dbpasswd -h$host -P$port $dbname > $backupdir/$dbname.sql
done
# pack files for backup
cd $backupdir
tar zcvf $datetime.tar.gz *.sql --remove-files
# delete 30days ago files
find $backupdir -mtime +30 -name "*.tar.gz" -exec rm -rf {} \;