In CentOS Linux, MySQL databases are automatically backed up every day.
In CentOS Linux, MySQL databases are automatically backed up every day.
1. Create the following three folders on the server: mkdir/bak, mkdir/bak/bakmysql, mkdir/bak/bakmysqlold. Create a shell script in the/bak/bakmysql Folder: touch/bak/bakmysql/backup. sh.
Authorize the file: chmod 755/bak/bakmysql/backup. sh.
2. Edit the shell script vi/bak/bakmysql/backup. sh.
#! /Bin/sh
Cd/bak/bakmysql
Echo "You are in bakmysql directory"
Mv bakmysql */bak/bakmysqlold
Echo "Old databases are moved to bakmysqlold folder"
Now = $ (date + "% d-% m-% Y ")
File = bakmysql-$ Now. SQL
Mysqldump-uroot-p 'Password' db_bbs> $ File
Echo "Your database backup successfully completed"
SevenDays = $ (date-d-7day + "% d-% m-% Y ")
If [-f/bak/bakmysqlold/bakmysql-$ SevenDays. SQL]
Then
Rm-rf/bak/bakmysqlold/bakmysql-$ SevenDays. SQL
Echo "You have delete 7 days ago bak file"
Else
Echo "7 days ago bak file not exist"
Fi
3. shell script: Replace the red font in mysqldump-uroot-p 'Password' db_bbs> $ File with your own mysql database user, password, and database name. The script first moves the backup file that bakmysql starts with the file name in the/bak/bakmysql folder to/bak/bakmysqlold, and then generates the latest backup file, finally, check whether the files in the/bak/bakmysqlold folder existed seven days ago. If the files exist, delete them and end them. In this way, the database scripts for the last seven days can be automatically backed up.
This is the master server:
4. set the daily automatic shell script: vi/etc/crontab to add the following line at the end (For details, refer to the notes in/etc/crontab ): 0 0 *** root/bak/bakmysql/backup. sh. Restart crontab:/sbin/service crond restart. If crontab is not set as auto-start, you can set it as follows: chkconfig-level 35 crond on
This article permanently updates the link address: