服务器上有N个数据库 现在需要备份其中的5个数据库
随性写了个 仅供参考
-- 每天自动备份
db_user="root"
db_passwd="pwd"
time="$(date "+%Y%m%d")"
mysql="/app/mysql/bin/mysql"
mysqldump="/app/mysql/bin/mysqldump"
backup_dir="/app/db_backup"
backup_log_dir="/app/db_backup/log"
dump_time=`date '+%Y%m%d%H%M%S'`
all_db="$($mysql -u$db_user -p$db_passwd -Bse 'show databases;')"
for db in $all_db
do
if [ "$db" = "db_name1" ]; then
echo "$dump_time|$db" >>$backup_log_dir/dump.log 2>&1
$mysqldump -u$db_user -p$db_passwd $db > "$backup_dir/$db.$time.sql"
fi
if [ "$db" = "db_name2" ]; then
echo "$dump_time|$db" >>$backup_log_dir/dump.log 2>&1
$mysqldump -u$db_user -p$db_passwd $db > "$backup_dir/$db.$time.sql"
fi
if [ "$db" = "db_name3" ]; then
echo "$dump_time|$db" >>$backup_log_dir/dump.log 2>&1
$mysqldump -u$db_user -p$db_passwd $db > "$backup_dir/$db.$time.sql"
fi
if [ "$db" = "db_name4" ]; then
echo "$dump_time|$db" >>$backup_log_dir/dump.log 2>&1
$mysqldump -u$db_user -p$db_passwd $db > "$backup_dir/$db.$time.sql"
fi
if [ "$db" = "db_name5" ]; then
echo "$dump_time|$db" >>$backup_log_dir/dump.log 2>&1
$mysqldump -u$db_user -p$db_passwd $db > "$backup_dir/$db.$time.sql"
fi
done
gzip *sql
#解压命令 gunzip
-- 每天自动删除7天前的备份
-- rm back_up 7 days ago
cd /data/db_backup/
rm_date_format=`date -d '7 day ago' '+%Y%m%d'`
rm -f *$rm_date_format.sql.gz
然后将2个文件 放到crontab 里面定时执行就可以了、