#!/bin/bash
# 配置
BACKUP_BASE_DIR="/backup/mysql"
BACKUP_DIR="$BACKUP_BASE_DIR/full_backup" # 临时备份目录
FINAL_BACKUP_DIR="$BACKUP_BASE_DIR/backups" # 最终备份文件存储目录
MYSQL_USER="root"
MYSQL_PASSWORD="your_password"
DATE=$(date +%Y%m%d_%H%M%S)
RETENTION_DAYS=7 # 保留最近 7 天的备份
# 创建目录
mkdir -p $BACKUP_DIR
mkdir -p $FINAL_BACKUP_DIR
# 清理临时备份目录
echo "Cleaning up temporary backup directory..."
rm -rf $BACKUP_DIR/*
# 执行备份
echo "Starting MySQL backup..."
xtrabackup --backup --user=$MYSQL_USER --password=$MYSQL_PASSWORD --target-dir=$BACKUP_DIR
# 检查备份是否成功
if [ $? -eq 0 ]; then
echo "Backup completed successfully."
# 准备备份
echo "Preparing backup..."
xtrabackup --prepare --target-dir=$BACKUP_DIR
# 压缩备份
echo "Compressing backup..."
tar -czvf $FINAL_BACKUP_DIR/full_backup_$DATE.tar.gz -C $BACKUP_DIR .
# 清理临时备份目录
echo "Cleaning up temporary backup directory..."
rm -rf $BACKUP_DIR/*
# 删除旧备份(保留最近 $RETENTION_DAYS 天的备份)
echo "Deleting old backups..."
find $FINAL_BACKUP_DIR -name "full_backup_*.tar.gz" -type f -mtime +$RETENTION_DAYS -exec rm -f {} \;
echo "Backup and cleanup completed successfully."
else
echo "Backup failed. Please check the logs."
# 清理临时备份目录(即使备份失败)
rm -rf $BACKUP_DIR/*
exit 1
fi
xtrabackup脚本
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- author:sufei 版本:xtrabackup 2.4.12 说明:生产使用备份工具xtrabackup造成...
- MySQL的XtraBackup 备份与恢复 https://shockerli.net/post/xtrabac...