Xunyu (nickname)
Background: In view of the devastating damage to the QAS database, it was extremely sandy. In order to avoid this bath water situation again, we found a solution. Here to share how to implement a scheduled backup MySQL database script.
Target: Make a daily backup of the data and tables on the QAS database and delete the backup files up to 10 days ago.
working: Three steps to get it done.
Step 1: Prepare two files
Build backup folder Qas_db_bak, build script file autobackupmysql_qas.sh
[[Email protected]***** wewang]# ls
autobackupmysql_qas.sh Qas_db_bak
Open the script file:
[Email protected]***** wewang]# VI autobackupmysql_qas.sh
Add content to the script:
#!/bin/bash Backupdir=/home/**/wewang/qas_db_bak now=$ (date + "%y-%m-%d--%h:%m:%s") /usr/bin/mysqldump-u ghs-pacxiom qas|gzip > $backupdir/qas_bak_$now.sql.gz Find $backupdir-name "qas_bak_*.sql.gz"-type f-mtime +10-exec rm {} \; >/dev/null 2>&1 |
: Wq Returns a change to the file execution permission after exiting, ensuring that the script can be executed:
[Email protected]***** wewang]# chmod +xautobackupmysql_qas.sh
As follows:
Analytical:
name : Custom backup file prefix identification.
-type F to find a normal type of file, F represents a normal file.
-mtime +10 to find files according to the time the file was changed, +10 indicates that the file change time is now Ten days ago; -mmin +10 indicates that the file change time is now 5 minutes ago.
-exec rm {} \; represents executing a shell command,followed by theexec option with the command or script to execute, followed by a pair of {}, A space and a \, and finally a semicolon.
/dev/null 2>&1 REDIRECT standard error to standard output and throw it to /dev/null go below. In layman's words, all standard output and standard errors are thrown into the trash, where the & means that the command executes in the background.
Step 2: Change the timed execution script, here are two case:
Case 1: General situation. Open the automatic execution file /etc/crontab, and include the following in the crontab file to automate the task
[Email protected] wewang]# Vi/etc/crontab
Add the following code:
Here's an example of an hourly 26-minute execution:
* * * * * root/home/**/wewang/autobackupmysql_qas.sh
If executed at three O ' Day, then:
XX 3 * * * root/home/**/wewang/autobackupmysql_qas.sh
Case 2:redhat situation. ( that is, the currently used Linux system ) Copy the script you just edited to the appropriate directory
[Email protected]***** wewang]# CP autobackupmysql_qas.sh/etc/cron.daily/ |
As follows:
parsing : This is done every day around 4:28, but the job execution time is also random.
Redhat's crontab takes 4 directories per time (
/etc/cron.hourly: Per hour;
/etc/cron.daily: Every day;
/etc/cron.weekly: Weekly;
/etc/cron.monthly: Monthly) The way the script comes out of the running.
Step 3: Restart etc
[Email protected] wewang]#/etc/rc.d/init.d/crondrestart
stopping Crond: [OK]
Starting Crond: [OK]
The effect of a scheduled backup is as follows:
Ps: If you want to unzip the view, then view by command:gunzip-c qas-bak-2015-07-30--04:29:02.gz > 1.bak
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Program Ape (yuan) one of the essential skills of shell scripting: How to automatically back up MySQL data under Linux