mysql database backup (mysqldump)

mysqldump command to backup data

mysqldump -u root -p --databases 数据库1 数据库2 > xxx.sql

Mysqldump common operation examples

1. Back up all database data and structures

mysqldump -uroot -p123456 -A > /data/mysqlbackup/mydb.sql

2. Back up the structure of all databases (add -d parameter)

mysqldump -uroot -p123456 -A -d > /data/mysqlbackup/mydb.sql

3. Back up all database data (add -t parameter)

mysqldump -uroot -p123456 -A -t > /data/mysqlbackup/mydb.sql

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

mysqldump -uroot-p123456 mydb > /data/mysqlbackup/mydb.sql

5. Back up the structure of a single database

mysqldump -uroot -p123456 mydb -d > /data/mysqlbackup/mydb.sql

6. Back up data of a single database

mysqldump -uroot -p123456 mydb -t > /data/mysqlbackup/mydb.sql

7. Back up the data and structures of multiple tables (the separate backup methods for data and structures are the same as above)

mysqldump -uroot -p123456 mydb t1 t2 > /data/mysqlbackup/mydb.sql

8. Back up multiple databases at once

mysqldump -uroot -p123456 --databases db1 db2 > /data/mysqlbackup/mydb.sql

Restore MySQL backup content

1. In the system command line, enter the following to restore:

mysql -uroot -p123456 < /data/mysqlbackup/mydb.sql

2. After logging in to the mysql system, use the source command to find the file in the corresponding system and restore it:

mysql> source /data/mysqlbackup/mydb.sql

Scheduled tasks to backup

In Linux, BASH scripts are usually used to write the content that needs to be executed, and the crontab command is regularly executed to realize automatic log generation.
The function of the following code is to back up mysql. In conjunction with crontab, the backup content 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 -p123456 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 to be backed up, 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 files that need to be deleted: Use the ls command to get the ninth column, which is the file name column, and then define the file that needs to be deleted with the latest operation time.

4. Define the number of backups: Use the ls command plus wc -l to count the number of lines in files ending with sql.

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

Use crontab to execute backup scripts regularly

In Linux, periodically executed tasks are generally handled by the cron daemon [ps -ef|grep cron]. cron reads one or more configuration files, which contain command lines and their calling times. The cron configuration file is called "crontab", which is the abbreviation of "cron table".

cron service

Cron is a scheduled execution tool under Liunx that can run jobs without manual intervention.

# 启动服务
service crond start
# 关闭服务
service crond stop
# 重启服务
service crond restart
# 重新载入配置
service crond reload
# 查看服务状态 
service crond status

crontab syntax

The crontab command is used to install, remove, or list the tables used to drive the cron background process. The user puts the sequence of commands that need 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 through the crontab command.

How to enter the command and time to be executed in the crontab file. Each line in this file includes six fields, the first five fields specify the time when the command is 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 values ​​00-59 00-23 01-31 01-12 0-6 (0 is sunday) In addition to numbers, there are several special symbols: "*", "/" and "-", ",", * Represents all numbers within the value range, "/" means every, "/5" means every 5 units, "-" means from a certain number to a certain number, "," separates several discrete numbers . -l displays 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 exit, the edited files will be installed automatically.

Create cron script

Step 1: Write a cron script file and name it mysqlRollBack.cron. 15,30,45,59 * * * * echo “xgmtest…” >> xgmtest.txt means, execute the print command every 15 minutes

Step 2: Add a scheduled task. Execute the command "crontab crontest.cron". Done

Step 3: "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 directly replaces the crontab under the user, rather than adding a new one.

Regularly execute the written scheduled task script (remember to give the shell script execution permission first)

0 2 * * * /root/mysql_backup_script.sh

Then use the crontab command to specify the written timing script

crontab mysqlRollback.cron

Then check whether the scheduled task has been created through the command:

Attached is an example of using crontab:

1. Every morning at 6 o'clock

0 6 * * * echo "Good morning." >> /tmp/test.txt
# 注意单纯echo,从屏幕上看不到任何输出,因为cron把任何输出都email到root的信箱了。

2. Every two hours

0 */2 * * * echo "Have a break now." >> /tmp/test.txt

3. Every two hours between 11pm and 8am and 8am

0 23-7/2,8 * * * echo "Have a good dream" >> /tmp/test.txt

4. On the 4th of every month and every Monday to Wednesday at 11 a.m.

0 11 4 * 1-3 command line

5. January 1, 4 a.m.

0 4 1 1 * command line SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root
# 如果出现错误,或者有数据输出,数据作为邮件发给这个帐号 HOME=/

6. Execute the script in /etc/cron.hourly every hour

01 * * * * root run-parts /etc/cron.hourly

7. Execute the script in /etc/cron.daily every day

02 4 * * * root run-parts /etc/cron.daily

8. Execute the script in /etc/cron.weekly every week

22 4 * * 0 root run-parts /etc/cron.weekly

9. Execute the script in /etc/cron.monthly every month

42 4 1 * * root run-parts /etc/cron.monthly
注意: "run-parts" 这个参数了,如果去掉这个参数的话,后面就可以写要运行的某个脚本名,而不是文件夹名。

10. Execute the command at 5 minutes, 15 minutes, 25 minutes, 35 minutes, 45 minutes, and 55 minutes at 4 p.m., 5 p.m., and 6 p.m. every day.

5,15,25,35,45,55 16,17,18 * * * command

11. The system enters maintenance status at 3:00 pm every Monday, Wednesday, and Friday and the system is restarted.

00 15 * * 1,3,5 shutdown -r +5

12. At 10 minutes and 40 minutes every hour, execute the innd/bbslin command in the user directory:

10,40 * * * * innd/bbslink

13. Execute the bin/account command in the user directory at 1 minute every hour:

1 * * * * bin/account

Guess you like

Origin blog.csdn.net/ZHAI_KE/article/details/132875297