MySQL数据备份,压缩并清理

    技术2022-07-10  93

    前言:

    本脚本只是个人的编写,仅供参考,若有提议请留言。

    #!/bin/bash backup_dir=/home/db_backup #存放备份文件的路劲 date_time=$(date +%F) #时间 database=(wg bw rg) #数据库 db_user=root #用户 db_password=123 #密码 db_host=127.0.0.1 #主机地址 [ -d $backup_dir/$date_time ] || mkdir -p $backup_dir/$date_time #判断路劲是否存在如果不存在创建 for i in ${database[*]} do mysqldump -u$db_user -p$db_password -h$db_host -B $i > $backup_dir/$date_time/$i.sql sleep 2 if [ $? -eq 0 ];then echo "$i$(date +%F\ %T)备份成功!" >> /tmp/backup_db.log else echo "$i$(date +%F\ %T)备份失败!" >> /tmp/backup_db.log fi done echo "正在执行压缩..." >> /tmp/backup_db.log cd $backup_dir && tar zcf $date_time.tar.gz $date_time && rm -rf $date_time if [ -f $backup_dir/$date_time.tar.gz ];then echo "压缩完成">> /tmp/backup_db.log else echo "压缩失败">> /tmp/backup_db.log fi echo "正在清理7天前备份文件..">> /tmp/backup_db.log old_file=$(find $backup_dir -type f -mtime -7) for i in $old_file do echo $(date %F_%T) 删除 $i >> /tmp/delet.log rm -f $i if [ $? -eq 0 ];then echo "清理完成!">> /tmp/backup_db.log else echo "清理失败,无过期文件!">> /tmp/backup_db.log fi done
    Processed: 0.009, SQL: 9