● Ubuntu版本查看
lsb_release -a
● mysql 版本查看
mysql --version
我的是ubuntu 20.04,mysql是5.7.36,所以要用 install_percona-xtrabackup-24
备份
恢复
百度网盘总是和谐我的资源,所以现在我取消会员,直接改用移动硬盘了
各个包都可以从此处下载 https://ubuntu.pkgs.org/22.04/ubuntu-main-amd64/locales_2.35-0ubuntu3_all.deb.html
下载好后,统一放进一个文件夹内
#!/bin/bash
# -*- coding: utf-8 -*-
# @Time : 2023/09/18 14:38
# @Author: kudoxi
# @File : install_percona-xtrabackup-80.sh
# @Desc : 该安装脚本只适用于ubuntu20.04
# 当前目录
path=$(pwd)
# 判断是否已经安装了,安装过了最好谨慎安装,防止是系统库
is_exist(){
package=$1
package_deb=$2
if test ! -z "$(dpkg -l | grep -w $package)";then
echo -e "$package exist "
# dpkg -i ${path}/$package_deb
else
dpkg -i ${path}/$package_deb
fi
}
install(){
is_exist libdbi-perl libdbi-perl_1.643-1ubuntu0.1_amd64.deb
is_exist libdbd-mysql-perl libdbd-mysql-perl_4.050-3ubuntu0.2_amd64.deb
is_exist libcurl4-openssl libcurl4-openssl-dev_7.68.0-1ubuntu2.19_amd64.deb
is_exist libev4:amd64 libev4_4.31-1_amd64.deb
is_exist percona-xtrabackup-24 percona-xtrabackup-24_2.4.20-1.focal_amd64.deb
}
install
innobackupex --version
#!/bin/bash
# MySQL 用户名,密码和主机名。需要备份权限。
MYSQL_USER="root"
MYSQL_PASSWORD="123456"
MYSQL_HOST="127.0.0.1"
# 是否自动 0为手动 1为定时自动
IS_TIMER_OPEN=1
# 备份目录和日志文件的路径。确保这些目录存在并具有适当的权限。
ROOT_PATH="/home/kudoxi"
BASE_DIR="$ROOT_PATH/temp"
FULL_BACKUP_DIR="$BASE_DIR/full"
INCREMENTAL_BACKUP_DIR="$BASE_DIR/incremental"
LOG_FILE="$BASE_DIR/backup.log"
INCREMENTAL_DATE="$(date +%Y-%m-%d)"
# 非定时任务创建增量备份目录
if [ $IS_TIMER_OPEN -eq 0 ]; then
mkdir -p "$BASE_DIR"
# 给权限
cd $ROOT_PATH
sudo chmod -R 777 "$BASE_DIR"
cd $BASE_DIR
mkdir -p "$INCREMENTAL_BACKUP_DIR"
mkdir -p "$INCREMENTAL_DIR"
sudo chmod -R 777 "$INCREMENTAL_BACKUP_DIR"
sudo chmod -R 777 "$INCREMENTAL_DIR"
fi
INCREMENTAL_DIR="$INCREMENTAL_BACKUP_DIR/$INCREMENTAL_DATE"
# 检查全量备份是否需要准备
if [ ! -f "$FULL_BACKUP_DIR/xtrabackup_checkpoints" ]; then
echo "Full backup needs to be prepared. Running prepare command..." >> "$LOG_FILE"
innobackupex --defaults-file=/etc/mysql/my.cnf --user="$MYSQL_USER" --password="$MYSQL_PASSWORD" --no-timestamp $FULL_BACKUP_DIR
fi
# 执行增量备份
/usr/bin/xtrabackup --backup --target-dir="$INCREMENTAL_DIR" --incremental-basedir="$FULL_BACKUP_DIR" --user="$MYSQL_USER" --password="$MYSQL_PASSWORD" --host="$MYSQL_HOST" >> "$LOG_FILE" 2>&1
# 检查备份结果
if [ $? -eq 0 ]; then
echo "Incremental backup completed successfully." >> "$LOG_FILE"
else
echo "Incremental backup failed. Please check the log for details." >> "$LOG_FILE"
fi
#!/bin/bash
# MySQL 用户名,密码和主机名
MYSQL_USER="root"
MYSQL_PASSWORD="123456"
MYSQL_HOST="127.0.0.1"
# 备份目录和日志文件的路径
ROOT_PATH="/home/kudoxi"
BASE_DIR="$ROOT_PATH/temp"
FULL_BACKUP_DIR="$BASE_DIR/full"
INCREMENTAL_BACKUP_DIR="$BASE_DIR/incremental"
LOG_FILE="$BASE_DIR/restore.log"
systemctl stop mysql
mv /var/lib/mysql "/var/lib/mysql_bak_$(date '+%Y-%m-%d-%H-%M-%S')"
# 恢复到的数据库目录
RESTORE_DIR="$BASE_DIR/restore"
# 清除之前的恢复目录
if [ -d "$RESTORE_DIR" ]; then
rm -rf "$RESTORE_DIR"
fi
# 恢复全量备份
echo "Restoring full backup..." >> "$LOG_FILE"
innobackupex --defaults-file=/etc/mysql/my.cnf --copy-back --user="$MYSQL_USER" --password="$MYSQL_PASSWORD" $FULL_BACKUP_DIR >> "$LOG_FILE" 2>&1
# 检查是否成功恢复全量备份
if [ $? -eq 0 ]; then
# 恢复增量备份
echo "Applying incremental backups..." >> "$LOG_FILE"
for dir in $(ls -d "$INCREMENTAL_BACKUP_DIR"/*/); do
echo "Applying $dir..." >> "$LOG_FILE"
innobackupex --defaults-file=/etc/mysql/my.cnf --apply-log --redo-only --user="$MYSQL_USER" --password="$MYSQL_PASSWORD" --incremental-dir="$dir" $RESTORE_DIR >> "$LOG_FILE" 2>&1
done
# 合并增量备份
echo "Merging incremental backups..." >> "$LOG_FILE"
innobackupex --defaults-file=/etc/mysql/my.cnf --apply-log --user="$MYSQL_USER" --password="$MYSQL_PASSWORD" --redo-only $RESTORE_DIR >> "$LOG_FILE" 2>&1
# 恢复完成后,将恢复的数据目录移动到MySQL的数据目录
if [ -d "$RESTORE_DIR" ]; then
mv "$RESTORE_DIR"/* /var/lib/mysql/
else
echo "Error: Restore directory does not exist or is empty." >> "$LOG_FILE"
fi
# 重新授权MySQL数据目录的权限
chown -R mysql:mysql /var/lib/mysql
# 重启MySQL服务
systemctl restart mysql
# 检查恢复结果
if [ $? -eq 0 ]; then
echo "Database restore completed successfully." >> "$LOG_FILE"
else
echo "Database restore failed. Please check the log for details." >> "$LOG_FILE"
fi
else
echo "Error: Failed to restore full backup. Please check the log for details." >> "$LOG_FILE"
fi
进入目录
cd ~/mysql-5.7.36
sudo bash install_percona-xtrabackup-24.sh
innobackupex --version
sudo vim /etc/rsyslog.d/50-default.conf
sudo service rsyslog restart
tail -100 /var/log/cron.log
mysql -uroot -p
如果失败,则说明当前用户没有权限直接执行mysql命令
如果成功,则跳过这一步
sudo usermod -aG mysql $USER
sudo newgrp mysql
cd ~/nas
sudo mkdir 164_mysql
修改备份脚本存储的真实路径
sudo vim /home/manteia/mysql-5.7.36/mysql_incremental_backup.sh
左下角输入:%s#temp#备份存放的路径
#g
回车确定,全局替换
2. 然后设定为手动模式
把1改为0
然后按ESC后输入:x
回车保存退出
cd ~/mysql-5.7.36
sudo bash mysql_incremental_backup.sh
异常时可以查看日志
tail -100 ~/nas/164_mysql/backup.log
crontab -e
30 10 * * * /bin/bash /home/kudoxi/mysql-5.7.36/mysql_incremental_backup.sh
sudo service cron restart
sudo vim /home/kudoxi/mysql-5.7.36/mysql_incremental_backup.sh
sudo chmod +x /home/kudoxi/mysql-5.7.36/mysql_incremental_backup.sh
~/nas/164_mysql/incremental
目录下就可以自动生成一个新的当天日期命名的数据tail -100 ~/nas/164_mysql/backup.log
sudo bash /home/kudoxi/mysql-5.7.36/mysql_restore_backup.sh
tail -100 ~/nas/164_mysql/restore.log