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

Introduction to query commands for MySQL stored procedures

As shown below: select name from mysql.proc where...

MySQL dual-master (master-master) architecture configuration solution

In enterprises, database high availability has al...

Several ways of running in the background of Linux (summary)

1. nohup Run the program in a way that ignores th...

WePY cloud development practice in Linux command query applet

Hello everyone, today I will share with you the W...

Implementation of effective user groups and initial user groups in Linux

First check the /etc/group file: [root@localhost ...

About debugging CSS cross-browser style bugs

The first thing to do is to pick a good browser. ...

javascript countdown prompt box

This article example shares the specific code of ...

Solution to forgetting mysql database password

You may have set a MySQL password just now, but f...

Implementing Priority Queue in JavaScript

Table of contents 1. Introduction to priority que...

Docker MQTT installation and use tutorial

Introduction to MQTT MQTT (Message Queuing Teleme...

How to build your own Angular component library with DevUI

Table of contents Preface Creating a component li...

vue-element-admin global loading waiting

Recent requirements: Global loading, all interfac...

Vue implements image drag and drop function

This article example shares the specific code of ...

HTML5+CSS3 header creation example and update

Last time, we came up with two header layouts, on...

A brief discussion on HTML ordered lists, unordered lists and definition lists

Ordered List XML/HTML CodeCopy content to clipboa...