MySQL数据库备份多循环(实用)
定时任务:
vi /etc/crontab
00 00 * * * root sh /mnt/data/zzy/scripts/backupMySQL_alldb.sh &>/dev/null
cat backupMySQL_alldb.sh
#!/bin/bash
#用户名 username=root #密码 password=aaa.4A3c# HOST=127.0.0.1 #保存备份个数,备份31天数据 number=3
#日期 dd=
date +%Y-%m-%d-%H-%M-%S
#备份工具 tool=/usr/local/mysql/bin/mysqldump echo $tool socketpath=/mnt/data/mysqldata/mysql.sock#备份保存路径
zzy="/mnt/data/zzy"
DB_LIST=$(mysql --socket=$socketpath -u$username -p$password -s -e "show databases;" 2>/dev/null |egrep -v "Database|information_schema|mysql|performance_schema|sys")
echo $DB_LIST for DB in $DB_LIST; do
#将要备份的数据库 database_name=$DB backup_dir=${zzy}/data/back/mysql-${database_name} mkdir -p ${backup_dir} #如果文件夹不存在则创建 if [ ! -d $backup_dir ]; then mkdir -p $backup_dir; fi #简单写法 mysqldump -u root -p123456 users > /root/mysqlbackup/users-$filename.sql $tool --socket=$socketpath -u $username -p$password $database_name > $backup_dir/$database_name-$dd.sql #写创建备份日志 echo "create $backup_dir/$database_name-$dd.dupm" >> $backup_dir/log.txt #找出需要删除的备份 delfile=`ls -l -crt $backup_dir/*.sql | awk '{print $9 }' | head -1` #判断现在的备份数量是否大于$number count=`ls -l -crt $backup_dir/*.sql | awk '{print $9 }' | wc -l` if [ $count -gt $number ]; then #删除最早生成的备份,只保留number数量的备份 rm $delfile #写删除文件日志 echo "delete $delfile" >> $backup_dir/log.txt fi echo $backup_dir
done