Detailed explanation of the implementation of regular backup of MySQL database tables

Detailed explanation of the implementation of regular backup of MySQL database tables

Implementation of regular backup of Mysql database tables

0. Background

In the actual development environment, the front-end program needs to update/insert data into the database table specified by MySQL within a given time period. As the amount of data increases and the base of basic database tables grows, there will be a delay of about 5 seconds for each update.

Improvement plan 1: Batch update, accumulating 10 or 100 records to perform an update operation;

Improvement plan 2: Back up the data one month before the current date and delete the data one month ago in the current database table. It has been determined that this method improves access efficiency to a certain extent. Root cause: The base table has a small cardinality, so the query efficiency is relatively improved.

1. Summary of scheduled backup of database tables

Step 1: Back up the specified database table in MySQL.

Use mysqldump and set the period to 30 days.

Step 2: Delete the backed up files and compressed packages that are older than 60 days.

Step 3: Delete the data in the database table that is 30 days before the current date. (A backup has been made in step 1).

Step 4: Set the timer.

crontab settings.

[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 to 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

#Regularly delete the 60-day backup package find $BAK_DIR -name "name_*.sql.gz" -type f -mtime +60 -exec rm {} \; > /dev/null 2>&1

#Delete the specified database table data 30 days ago (the 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. Scheduled settings: backup at 1:00 every 30 days.

[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 crontab service

service crond restart

Thank you for reading, I hope it can help you, thank you for your support of this site!

You may also be interested in:
  • Very comprehensive Mysql database, database table, data basic operation notes (including code)
  • PHP+mysqli method to replace database table prefixes in batches
  • MySQL database table repair MyISAM
  • Script to check and repair MySQL database tables using shell
  • Specific methods to check and repair MySQL database tables
  • Create database and database table code with MySQL
  • Code to extract random data from MySQL database table

<<:  ES6 loop and iterable object examples

>>:  How to remount the data disk after initializing the system disk in Linux

Recommend

HTML uses canvas to implement bullet screen function

Introduction Recently, I needed to make a barrage...

Ubuntu terminal multi-window split screen Terminator

1. Installation The biggest feature of Terminator...

Vue+js realizes video fade-in and fade-out effect

Vue+js realizes the fade in and fade out of the v...

Implementation of fastdfs+nginx cluster construction

1. Introduction to fastdfs 1. What is fastdfs Fas...

How to install iso file in Linux system

How to install iso files under Linux system? Inst...

Radio buttons and multiple-choice buttons are styled using images

I've seen people asking before, how to add sty...

30 Tips for Writing HTML Code

1. Always close HTML tags In the source code of p...

A detailed discussion of components in Vue

Table of contents 1. Component Registration 2. Us...

How to get the maximum or minimum value of a row in sql

Original data and target data Implement SQL state...

Vue makes a simple random roll call

Table of contents Layout part: <div id="a...

Three common methods for HTML pages to automatically jump after 3 seconds

In practice, we often encounter a problem: how to...

Linux uses lsof command to check file opening status

Preface We all know that in Linux, "everythi...

CSS perfectly solves the problem of front-end image deformation

I saw an article in Toutiao IT School that CSS pe...

CSS to achieve Tik Tok subscription button animation effect

I was watching Tik Tok some time ago and thought ...

CSS code to distinguish ie8/ie9/ie10/ie11 chrome firefox

Website compatibility debugging is really annoyin...