Regular backup practice of MySQL database

1. Background introduction

Maybe you will find that this site has been built for a long time, but the various types of visits to this site are always very low. There are probably two reasons for this. One is because after the site was upgraded to 2.0, all article access data has not Synchronization (not just articles, all data is synchronized, such as user data, etc., maybe these data are not important); another reason is that an update statement was executed about ten days ago (the default MySQL client The transaction is submitted automatically), and the visits of all articles are cleared. The visits data of the whole station of the system is also perfected and added in the near future (within one month) of this site 2.0. The reason for the former is to give up voluntarily. The latter is caused by his own mistakes, so this article is mainly to realize the regular backup of a MySQL database, which can be regarded as making the data more secure (maybe the site is hacked and the data is lost).

2. Backup script

The regular backup of the MySQL database implemented in this article is mainly to achieve a high degree of fit with the individual. If there are very high requirements for the data, it needs to be realized by real-time backup or synchronization (not the purpose of this practice ), write the backup script file in the form of shell script, add it to the scheduled task of the system, and execute it automatically. Refer to the backup script file (named backup.sh) as shown below.

#!/bin/bash

#
# Copyright (c) 2023 by chendd 88911006
# 简单备份MySQL数据库脚本
# All rights reserved.

#数据库IP
MYSQL_HOST="localhost"
# 数据库名称
MYSQL_DATABASE="chendd-blog"
# 数据库用户名
MYSQL_USERNAME="root"
# 数据库密码
MYSQL_PASSWORD="root"
# 备份根路径
BACKUP_DIR=$(cd `dirname $0`; pwd)
# 年月日时分秒
DATETIME=$(date +%Y%m%d_%H%M%S)

# 如果不指定jdk则使用默认
if [[ -n "$MYSQL_HOME" ]] && [[ -x "$MYSQL_HOME/bin/mysqldump" ]]; then
    MYSQL_EXE="$MYSQL_HOME/bin/mysqldump"
elif type -p mysqldump > /dev/null 2>&1; then
    MYSQL_EXE=$(type -p mysqldump)
elif [[ -x "/app/mysql/bin/mysqldump" ]];  then
    MYSQL_EXE="/app/mysql/bin/mysqldump"
else
    echo "$(date "+%Y-%m-%d %H:%M:%S") ERROR Unable to find MySQL mysqldump" >> ${BACKUP_DIR}/backup.log
    exit 1
fi

#输出一个空行
echo " " >> ${BACKUP_DIR}/backup.log
echo "$(date "+%Y-%m-%d %H:%M:%S") INFO 开始执行批次为 [$DATETIME] 的备份" >> ${BACKUP_DIR}/backup.log

# 备份命令
COMMAND="$MYSQL_EXE --host=$MYSQL_HOST --user=$MYSQL_USERNAME --password=$MYSQL_PASSWORD $MYSQL_DATABASE > $BACKUP_DIR/backup_${MYSQL_DATABASE}_${DATETIME}.sql"
echo "$(date "+%Y-%m-%d %H:%M:%S") INFO 开始备份数据库,命令为:$COMMAND" >> ${BACKUP_DIR}/backup.log
# 执行备份语句
echo "$(date "+%Y-%m-%d %H:%M:%S") INFO 结束备份" >> ${BACKUP_DIR}/backup.log
/bin/sh -c "$COMMAND"
# 压缩命令(保留压缩原sql文件)
echo "$(date "+%Y-%m-%d %H:%M:%S") INFO 开始压缩数据库文件" >> ${BACKUP_DIR}/backup.log
gzip -c $BACKUP_DIR/backup_${MYSQL_DATABASE}_${DATETIME}.sql > $BACKUP_DIR/backup_${MYSQL_DATABASE}_${DATETIME}.sql.gz
echo "$(date "+%Y-%m-%d %H:%M:%S") INFO 结束压缩" >> ${BACKUP_DIR}/backup.log

#删除10天之前的备份
find ${BACKUP_DIR} -name backup_"*.sql" -type f -mtime +10 -exec rm -rf {} \; >> ${BACKUP_DIR}/backup.log
find ${BACKUP_DIR} -name backup_"*.sql.gz" -type f -mtime +10 -exec rm -rf {} \; >> ${BACKUP_DIR}/backup.log

echo "$(date "+%Y-%m-%d %H:%M:%S") INFO 结束执行批次为 [$DATETIME] 的备份" >> ${BACKUP_DIR}/backup.log

3. Introduction to backup scripts

(1) Assuming that the script command is "bachup.sh", the permission of the executable file needs to be granted;

(2) Limited search for the MYSQL_HOME environment variable, if not found, the default MySQL installation path is /app/mysql;

(3) The configured parameters include MySQL database server IP, database user name, database password, database name, and backup file path. These parameters need to be adjusted as needed;

(4) After each key script is executed, output some prompts to the command log file "backup.log", the format is: date time + log level + description information; there are prompts for each start and end execution;

(5) Only one database is backed up by default, and this script is not suitable for backing up multiple databases;

(6) The format of the generated backup sql file is named "backup_database name_script execution date_script execution time.sql";

(7) Compress the produced backup sql file into a compressed package file of "backup_database name_script execution date_script execution time.sql.gz". After the file is compressed, the original sql format file will not be deleted by default;

(8) After each execution according to the file naming format, the files older than 10 days will be deleted. In practice, it has been verified that the backup files deleted 1 minute ago, and the trigger frequency of the scheduled task is 1 minute;

4. Execute the script regularly

# 查看定时任务列表:每天临时0:30分触发
crontab -l
# 增加定时任务:
echo '30 0 0 * *  sh /app/backup.sh' >> /var/spool/cron/root
#刷新定时任务
service crond reload

5. Running results

(run the script manually)

(script execution log)

(script execution file)

Script download: backup.zip

The original address is the personal blog address, refer to: https://www.chendd.cn/blog/article/1637269874421841921.html

Guess you like

Origin blog.csdn.net/haiyangyiba/article/details/129652227
Recommended