Note: the compression file of the snapshot (20121220 refers to the date of the day when the backup is executed) is retained only for the last 7 days. 1. create and save the backup file path: homemysql_data
Note: I want to back up all the files under/var/lib/MySql in the mysql database directory to/home/mysql_data and save them as compressed files of mysql_data2012121_tar.gz (20121220 refers to the date on which the backup was executed) then, only the backups of the last 7 days will be retained. 1. the path for creating and saving the backup file is/home/mysql_data.
Note: I want to back up all the files under/var/lib/MySql in the mysql storage directory to/home/mysql_data.
Save the compressed file mysql_data201212316tar.gz (20121220 refers to the date on which the backup was executed)
Then, only the backups of the last 7 days are retained.
1. create the path to save the backup file:/home/mysql_data
Cd/home
Mkdir mysql_data
2. create a backup script file:/home/mysql_data/mysql_databak.sh
Cd/home
Cd mysql_data
Touch mysql_databak.sh
Vim mysql_databak.sh
Enter the following content:
######################################## ######################################## ##
#! /Bin/sh
/Etc/init. d/mysqld stop # stop MySql before performing backup to prevent data writing and backup errors
Date = 'date + % Y % m % d' # get the current date
DAYS = 7 # DAYS = 7 indicates that the backup created seven DAYS ago is deleted, that is, only the backup of the last seven DAYS is retained.
BK_DR =/home/mysql_data # backup file storage path
DB_DR =/var/lib/mysql # Database path
LINUX_USER = root # system username
Tar zcvf $ BK_DR/mysql_data1_date.tar.gz $ DB_DR # back up data
/Etc/init. d/mysqld start # After The backup is complete, start MySql
Chown-R $ LINUX_USER: $ LINUX_USER $ BK_DR # Change the owner of the backup database file
Find $ BK_DR-name "mysql_data *"-type f-mtime + $ DAYS-exec rm {}\; # Delete the backup file seven DAYS ago
######################################## ######################################## ##
3. Modify file attributes to make them executable.
Chmod + x/home/mysql_data/mysql_databak.sh
4. modify/etc/crontab
Vi/etc/crontab
Add
35 13 *** root/home/mysql_data/mysql_databak.sh
Indicates that a backup is performed at every day.
5. restart crond to make the settings take effect.
/Etc/rc. d/init. d/crond restart
Chkconfig crond on # set to boot
Service crond start # start
Every day, you can see compressed files such as mysql_data2012121_tar.gz under the/home/mysql_datadirectory.
To restore a file, you only need to decompress the file.
Decompress tar-zxvf mysql_data2012121_tar.gz