[Dry goods] MySQL database scheduled backup summary

Author: Phantom

foreword

In the process of operating data, it may lead to data errors or even database crash, and effective regular backup can protect the database well. This article mainly describes several methods for regular MySQL backup database.

image-20220120084015828.png

Classification of database backups

There are many database backup methods, which are divided from the perspective of affecting the database:

  • Hot backup: reads and writes are not affected
  • Warm backup: only read operations can be performed
  • Cold backup: offline backup, read and write operations are suspended

Divided by backup method:

  • Physical backup: The value is a backup of the physical files (such as data files, log files, etc.) of the database operating system.
  • Logical backup: refers to the backup of the logical components of the database (such as tables and other database objects).

Divided from backup strategy:

  • Full Backup: Take a full backup of the database each time. The entire database can be backed up, including user tables, system tables, indexes, views, and all database objects in stored procedures. But it takes more time and space, so it takes longer to do a full backup.
  • Differential backup: Backs up files that have been modified since the last full backup. Part of the value backup database is smaller than a full backup, so storage and recovery is faster.
  • Incremental backup: Only those files that have been modified since the last full or incremental backup will be backed up.

image-20220120083809083.png

MySQL data backup

mysqldump command to backup data

MySQL provides a convenient tool mysqldump to export database data and files from the command line. We can directly export the database content through the command line. First, let's briefly understand the usage of the mysqldump command:

#MySQLdump常用
mysqldump -u root -p --databases 数据库1 数据库2 > xxx.sql

Examples of common operations of mysqldump

Backup data and structure of all databases

mysqldump -uroot -proot -A > /data/mysqlDump/mydb.sql
  • Back up the structure of all databases (add -d parameter)
mysqldump -uroot -proot -A -d > /data/mysqlDump/mydb.sql
  • Backup all database data (add -t parameter)
mysqldump -uroot -proot -A -t > /data/mysqlDump/mydb.sql

Back up the data and structure of a single database (, database name mydb)

mysqldump -uroot-proot mydb > /data/mysqlDump/mydb.sql
  • Back up the structure of a single database
mysqldump -uroot -proot mydb -d > /data/mysqlDump/mydb.sql
  • Back up data from a single database
mysqldump -uroot -proot mydb -t > /data/mysqlDump/mydb.sql
  • Back up the data and structure of multiple tables (the separate backup method of data and structure is the same as above)
mysqldump -uroot -proot mydb t1 t2 > /data/mysqlDump/mydb.sql
  • Back up multiple databases at once
mysqldump -uroot -proot --databases db1 db2 > /data/mysqlDump/mydb.sql

MySQL restore backup data

There are two ways to restore, the first is in the MySQL command line, the second is to use the SHELL line to complete the restore

  • On the system command line, enter the following to restore:
mysql -uroot -proot < /data/mysqlDump/mydb.sql
  • After logging in to the mysql system, use the source command to find the files in the corresponding system to restore:
mysql> source /data/mysqlDump/mydb.sql

Writing scripts to maintain backup database files

In Linux, BASH scripts are usually used to write the content to be executed, and the crontab command is executed regularly to realize automatic log generation.

The following code function is to back up mysql, with crontab, the content of the backup is the daily mysql database records in the past month (31 days).

Write BASH to maintain a fixed number of backup files

In Linux, use vi or vim to write the script content and name it: mysql_dump_script.sh

#!/bin/bash

#保存备份个数,备份31天数据
number=31
#备份保存路径
backup_dir=/root/mysqlbackup
#日期
dd=`date +%Y-%m-%d-%H-%M-%S`
#备份工具
tool=mysqldump
#用户名
username=root
#密码
password=TankB214
#将要备份的数据库
database_name=edoctor

#如果文件夹不存在则创建
if [ ! -d $backup_dir ];
then   
    mkdir -p $backup_dir;
fi

#简单写法 mysqldump -u root -proot users > /root/mysqlbackup/users-$filename.sql
$tool -u $username -p$password $database_name > $backup_dir/$database_name-$dd.sql

#写创建备份日志
echo "create $backup_dir/$database_name-$dd.dupm" >> $backup_dir/log.txt

#找出需要删除的备份
delfile=`ls -l -crt $backup_dir/*.sql | awk '{print $9 }' | head -1`

#判断现在的备份数量是否大于$number
count=`ls -l -crt $backup_dir/*.sql | awk '{print $9 }' | wc -l`

if [ $count -gt $number ]
then
  #删除最早生成的备份,只保留number数量的备份
  rm $delfile
  #写删除文件日志
  echo "delete $delfile" >> $backup_dir/log.txt
fi

The main meaning of the above code is as follows:

1. First set various parameters, such as number, the maximum number of backups, backup path, user name, password, etc.

2. Execute the mysqldump command to save the backup file, and print the operation to log.txt in the same directory to mark the operation log.

3. Define the file to be deleted: Obtain the ninth column, which is the file name column, through the ls command, and then define the file that needs to be deleted with the latest operation time.

4. Define the number of backups: add the ls command

Count the number of lines in files ending in sql.

5. If the file exceeds the limit size, delete the earliest created sql file

Periodically execute backup scripts using crontab

In Linux, periodic tasks are generally handled by the cron daemon [ps -ef|grep cron]. cron reads one or more configuration files that contain command lines and when they are invoked. The configuration file for cron is called "crontab", short for "cron table".

cron service

cron is a timing execution tool under Linux that can run jobs without human intervention.

service crond start //Start the service service crond stop //Close the service service crond restart //Restart the service service crond reload //Reload the configuration service crond status //View the service status

crontab syntax

The crontab command is used to install, remove, or list tables used to drive cron daemons. The user puts the sequence of commands to be executed into the crontab file for execution. Each user can have their own crontab file. The crontab file under /var/spool/cron cannot be created or modified directly. The crontab file is created by the crontab command.

How to enter the command and time to be executed in the crontab file. Each line in the file contains six fields, where the first five fields specify when the command is to be executed, and the last field is the command to be executed. Use spaces or tabs to separate each field.

The format is as follows: minute hour day-of-month month-of-year day-of-week commands legal value 00-59 00-23 01-31 01-12 0-6 (0 is sunday)

In addition to numbers, there are several special symbols that are "_", "/" and "-", ",", _ represents all numbers within the range of values, "/" represents the meaning of each, "/5" Indicates every 5 units, "-" represents from a certain number to a certain number, "," separates several discrete numbers.

-l Display the current crontab on standard output. -r Deletes the current crontab file. -e Edit the current crontab file using the editor pointed to by the VISUAL or EDITOR environment variable. When you finish editing and leave, the edited file will be installed automatically.

Create cron script

Step 1: Write a cron script file named mysqlRollBack.cron. 15,30,45,59 * * * * echo "xgmtest....." >> xgmtest.txt means that every 15 minutes, execute the print command Step 2: Add a scheduled task. Execute the command "crontab crontest.cron". Get the third step: "crontab -l" to check whether the scheduled task is successful or whether the corresponding cron script is generated under /var/spool/cron

Note: This operation is to directly replace the crontab under the user, not to add

Execute the script of the scheduled task regularly (remember to give the shell script execution permission first)

0 2 * * * /root/mysql_backup_script.sh

Then use the crontab command to periodically write the timing script

crontab mysqlRollback.cron

Then use the command to check whether the scheduled task has been created.

Example of using crontab:

  • every morning at 6 am
0 6 * * * echo "Good morning." >> /tmp/test.txt //注意单纯echo,从屏幕上看不到任何输出,因为cron把任何输出都email到root的信箱了。
  • every two hours
0 */2 * * * echo "Have a break now." >> /tmp/test.txt
  • Every two hours between 11pm and 8am and 8am
0 23-7/2,8 * * * echo "Have a good dream" >> /tmp/test.txt
  • 4th of every month and every Monday to Wednesday at 11:00 am
0 11 4 * 1-3 command line
  • January 1st at 4am
0 4 1 1 * command line SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root //如果出现错误,或者有数据输出,数据作为邮件发给这个帐号 HOME=/
  • Execute the script in /etc/cron.hourly every hour
01 * * * * root run-parts /etc/cron.hourly
  • Execute the script in /etc/cron.daily every day
02 4 * * * root run-parts /etc/cron.daily
  • Execute the script in /etc/cron.weekly every week
22 4 * * 0 root run-parts /etc/cron.weekly
  • Execute the script in /etc/cron.monthly every month
42 4 1 * * root run-parts /etc/cron.monthly

Note: "run-parts" is the parameter. If you remove this parameter, you can write the name of a script to be run instead of the folder name.

  • Execute the command at 5, 15, 25, 35, 45, and 55 minutes at 4:00, 5:00, and 6:00 every day.
5,15,25,35,45,55 16,17,18 * * * command
  • Every Monday, Wednesday, and Friday at 3:00 pm, the system enters the maintenance state and restarts the system.
00 15 * * 1,3,5 shutdown -r +5
  • Execute the command innd/bbslin in the user directory at 10 minutes and 40 minutes of every hour:
10,40 * * * * innd/bbslink
  • Execute the bin/account command in the user directory at 1 minute of every hour:

Want to learn more from the tech giants? Where are the problems encountered in development discussed? How to obtain the massive resources of financial technology?

Hang Seng LIGHT Cloud Community, a professional financial technology community platform built by Hang Seng Electronics, shares practical technical dry goods, resource data, financial technology industry trends, and embraces all financial developers.

Scan the QR code of the applet below to join us!

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324140272&siteId=291194637