Detailed Explanation of Periodic Backup of Mysql Database Table
- 2021-07-10 21:03:40
- OfStack
Realization of Periodic Backup of Mysql Database Table
0. Background
In the real development environment, the front-end program needs to update/insert data into the library table specified by MySQL within a given time period. With the increase of data volume and the increase of basic library table base, there will be about 5s stuck every update.
Improvement Scheme 1: Batch update, accumulating 10 items or 100 items for one update and warehousing operation;
Improvement 2: Backup the data before 1 month before the current date, and delete the data before 1 month in the current library table. After measurement, this method improves the access efficiency to a certain extent. Root cause: The base number of basic tables is small, and the query efficiency is relatively improved.
1. Summary of regular backup of library tables
Step 1: Back up the formulation library table in the database specified by Mysql.
Use mysqldump with a set period of 30 days.
Step 2: Delete the backup files and compressed packages 60 days before the date.
Step 3: Delete the data in the library table 30 days before the current date. (Step 1 has been backed up).
Step 4: Set the timing.
crontab setting.
[root@mysql_bak]# cat mysql_bak.sh
#!/bin/sh
#DATABASE INFO
DB_NAME="ppdb"
DB_USER="root"
DB_PASS="password"
DISPOSE_TABLE="dispose_ticles"
RST_TABLE="match_rst"
DB_IP=100.55.1.129
BIN_DIR="/usr/bin"
BAK_DIR="/home/mysql_bak/data"
DATE=`date +%Y%m%d_%H%M%S`
#mkdir -p $BAK_DIR
# Backup package Form a compressed package
$BIN_DIR/mysqldump $DB_NAME $DISPOSE_TABLE > $BAK_DIR/$DISPOSE_TABLE.dump_$DATE.sql
$BIN_DIR/mysqldump $DB_NAME $DISPOSE_TABLE | gzip > $BAK_DIR/$DISPOSE_TABLE.dump_$DATE.sql.gz
$BIN_DIR/mysqldump $DB_NAME $RST_TABLE > $BAK_DIR/$RST_TABLE.dump_$DATE.sql
$BIN_DIR/mysqldump $DB_NAME $RST_TABLE | gzip > $BAK_DIR/$RST_TABLE.dump_$DATE.sql.gz
# Periodic deletion 60 Days of backup package
find $BAK_DIR -name "name_*.sql.gz" -type f -mtime +60 -exec rm {} \; > /dev/null 2>&1
#30 Data deletion operation of specified library table days ago ( Current time minus 30 Days)
delete_date=`date --date='30 day ago' +%Y-%m-%d`
echo "delete_date=$delete_date"
# Delete rst Table information
rst_sql="delete from $RST_TABLE where update_time <= $delete_date order by update_time;";
echo "rst_sql=$rst_sql"
#ret=$(mysql -u $DB_USER -h ${DB_IP} -p${DB_PASS} $DB_NAME -e "$sql");
ret=$(mysql -h${DB_IP} $DB_NAME -e "$rst_sql");
echo $ret
# Delete dispose Table information
dispose_sql="delete from $DISPOSE_TABLE where judge_time <= $delete_date order by judge_time;";
echo "dispose_sql=$dispose_sql"
ret=$(mysql -h${DB_IP} $DB_NAME -e "$dispose_sql");
echo $ret
2. Timing setting: Backup every 30 days at 1 o'clock.
[root@mysql_bak]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
0 1 */30 * * /home/mysql_bak/mysql_bak.sh > /dev/null 2>&1
[root@mysql_bak]# crontab -e
0 1 */30 * * /home/mysql_bak/mysql_bak.sh > /dev/null 2>&1
Restart the crontab service
service crond restart
Thank you for reading, hope to help everyone, thank you for your support to this site!