1、全量备份在周末晚上22点执行备份,增量是周一到周五晚上22点执行
2、考虑到增量备份第一次是根据全量备份开始备份,后面都是根据上一次增量备份在增量脚本做了if判断,周日做一次目录清理
3、每周日晚上91点50清理目录 22点就在次备份,增量备份在周一会根据最新的周日备份进行增量备份,
4、打包备份目录可以按月来清理
full_mysqlback全量备份数据包 full_inc_mysqlback是第一次根据全量数据做的增倍数据包
inc_mysqlback 是每吃一次增倍的数据包
0 22 * * 0 sh /root/full.sh
0 22 * * 1-5 sh /root/inc.sh
50 21 * * 0 rm -rf /back/back_full/*
50 21 * * 0 rm -rf /back/back_inc/*
50 21 * * 0 rm -rf /opt/full_inc_mysqlbackup/*
50 21 * * 0 rm -rf /opt/full_mysqlbackup/*
50 21 * * 0 rm -rf /opt/inc_mysqlbackup/*
全量备份脚本
- [root@cloudserver src]# pwd
- /opt/src
- [root@cloudserver src]# cat /root/full.sh
- #!/bin/bash
- #远程备份IP地址
- back_ip="192.168.40.112"
-
- #主机ip
- #数据库信息
- mysql_data_dir="/usr/local/mysql/data"
- mysql_user="back"
- mysql_password="123456"
- mysql_host="192.168.40.66"
- mysql_port=3306
-
- #定义备份目录
- backup_dir="/backup/back_full"
-
- echo "执行全量备份..."
- # 获取当前时间戳
- timestamp=$(date +'%Y%m%d%H%M%S')
- back_dir="${backup_dir}/${timestamp}"
- # 创建备份目录
- mkdir -p "$back_dir"
-
-
- #获取最新的全量备份目录
- new_backup_dir=$(ls -1t "$backup_dir" | head -1)
-
- #全量备份目录
- full_dir="${backup_dir}/${new_backup_dir}"
-
- #本地打包备份目录
- tar_backup="/opt/full_mysqlbackup"
- mkdir -p $tar_backup
- # 执行全量备份
- xtrabackup --backup --target-dir="${back_dir}" --user="$mysql_user" --password="$mysql_password" --host="$mysql_host" --port="$mysql_port"
-
- echo "压缩最新的全量备份..."
- cd $full_dir
- tar -cvf "${tar_backup}/${timestamp}.tar.gz" $full_dir
- scp ${tar_backup}/${timestamp}.tar.gz $back_ip:$tar_backup
增量备份脚本
- [root@cloudserver src]# cat /root/full.sh
- #!/bin/bash
- #远程备份IP地址
- back_ip="192.168.40.112"
-
- #主机ip
- #数据库信息
- mysql_data_dir="/usr/local/mysql/data"
- mysql_user="back"
- mysql_password="123456"
- mysql_host="192.168.40.66"
- mysql_port=3306
-
- #定义备份目录
- backup_dir="/backup/back_full"
-
- echo "执行全量备份..."
- # 获取当前时间戳
- timestamp=$(date +'%Y%m%d%H%M%S')
- back_dir="${backup_dir}/${timestamp}"
- # 创建备份目录
- mkdir -p "$back_dir"
-
-
- #获取最新的全量备份目录
- new_backup_dir=$(ls -1t "$backup_dir" | head -1)
-
- #全量备份目录
- full_dir="${backup_dir}/${new_backup_dir}"
-
- #本地打包备份目录
- tar_backup="/opt/full_mysqlbackup"
- mkdir -p $tar_backup
- # 执行全量备份
- xtrabackup --backup --target-dir="${back_dir}" --user="$mysql_user" --password="$mysql_password" --host="$mysql_host" --port="$mysql_port"
-
- echo "压缩最新的全量备份..."
- cd $full_dir
- tar -cvf "${tar_backup}/${timestamp}.tar.gz" $full_dir
- scp ${tar_backup}/${timestamp}.tar.gz $back_ip:$tar_backup
- [root@cloudserver src]# cat /root/inc.sh
- #!/bin/bash
- # 远程备份IP地址
- back_ip="192.168.40.112"
-
- # 数据库信息
- mysql_user="back"
- mysql_password="123456"
- mysql_host="192.168.40.66"
- mysql_port=3306
-
- # 定义增量备份目录
- backup_dir="/backup/back_inc"
-
- # 获取上一次的增量备份目录
- last_backup_dir=$(ls -1t "$backup_dir" | head -1)
-
- # 如果没有上一次的增量备份目录,那么使用最新的全量备份目录
- if [ -z "$last_backup_dir" ]; then
- full_back_dir="/backup/back_full/"
- last_backup_dir=$(ls -1t "$full_back_dir" | head -1)
- backup_type="full_inc"
- else
- backup_type="inc"
- fi
-
- # 获取当前时间戳
- timestamp=$(date +'%Y%m%d%H%M%S')
- back_dir="${backup_dir}/${timestamp}"
-
- # 创建增量备份目录
- mkdir -p "$back_dir"
-
- # 本地打包备份目录
- tar_backup="/opt/${backup_type}_mysqlbackup"
- mkdir -p "$tar_backup"
-
- # 根据备份类型设置备份目录
- if [ "$backup_type" = "full_inc" ]; then
- backup_basedir="$full_back_dir"
- else
- backup_basedir="$backup_dir"
- fi
-
- # 执行增量备份
- xtrabackup --backup --target-dir="${back_dir}" --user="$mysql_user" --password="$mysql_password" --host="$mysql_host" --port="$mysql_port" --incremental-basedir="${backup_basedir}/${last_backup_dir}"
-
- echo "压缩最新的${backup_type}备份..."
- cd $back_dir
- tar -cvf "${tar_backup}/${timestamp}.tar.gz" $back_dir
- scp ${tar_backup}/${timestamp}.tar.gz $back_ip:$tar_backup
全量恢复
全量、增量恢复