Centos7 automatic backup mysql database

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_45754572/article/details/102723215

Article Directory
1 new executable file
2 added to the task execution timing of Centos7

All operations described herein are performed by a user based on the root
statement Centos7 primarily using the automatic backup mysql crontab command to the timing of performing backup mysql

1 Create an executable file
need to create an executable file, such as bakup.sh

mkdir /root/bakup
cd /root/bakup
vim bakup.sh

bakup.sh reads as follows:

#!/bin/bash
cd /opt/mysql/bin
./mysqldump -uusername -ppassword dbname | gzip > /targetpath/dbname_$(date +%Y%m%d_%H%M%S).sql.gz

When you're done press the Esc key, then enter: wq to save and exit the operation
explanation of the above content:

! # / bin / bash
fixing
cd / opt / mysql / bin
switch to mysql bin directory, the reader be modified according to the actual path
./mysqldump -uusername -ppassword dbname | gzip> / targetpath / dbname _ $ (date +% Y% m D_% H% M% S%) .sql.gz
./mysqldump mysql executable command in the current bin directory
-uusername -u represents the next mysql is followed by the user name, the user name is zhangsan example, this it should be -uzhangsan
-Ppassword the -p represents the next followed by mysql password is relatively user names, such as user name zhangsan mysql password is 123456, this should be -p123456
database name dbname pledged to back up
| gzip He expressed the need for compression

/targetpath/dbname_ ( d a t e + (Date +% Y% m% d_% H% M% S) .sql.gz said to be backed up to the target path, the need / targetpath modified according to the actual situation of the actual concrete path, dbname herein may be modified to the name of the database, but also You can modify other; time (date +% Y% m%where the time to become specific date, day, hour, no need to modify
``

cd /root/bakup
chmod +x bakup.sh

2 added to the task execution timing Centos7
use the crontab command to the timing of the implementation of the above executable file
using the following command to edit the task you want to perform:

crontab -e

After using the above command, will enter edit mode and use vim to enter the edit mode using a similar
need to edit the content of specific format to find information on their own, here is a simple example as follows:

0 0 * * * /root/bakup/bakup.sh

After editing press the Esc key, then enter: wq logout operation to save
the above description:

0 0 * * * is the timing of execution time, the format of the reference time cron expression
/root/bakup/bakup.sh specific file to be executed

Note: crond the need to review the state to see if it starts, if there is no need to start to start with the following start command, if crond service has been launched, and then use the crontab -e command to edit without having to restart the service

1. See the crond Status Status systemctl
2. restart the restart the crond systemctl
3. Start the crond start systemctl
4. Stop systemctl stop crond

Guess you like

Origin blog.csdn.net/weixin_45754572/article/details/102723215
Recommended