Automatic MYSQL backup in Ubuntu :?? Implementation steps :? 1. Create a directory for saving the backup file: varmysqlbakdata ?? 2. Create a backup script file: varmysqlbakmysql_databak.sh? Cdvarmysqlbak # Enter the directory? Touchmysql_databak.sh # create a file? Vimmysql_databak.sh # Edit
Automatic MYSQL backup in Ubuntu :? ? Implementation steps :? 1. Create a directory for saving the backup file:/var/mysqlbak/data? ? 2. Create a backup script file:/var/mysqlbak/mysql_databak.sh? Cd/var/mysqlbak # Enter the directory? Touch mysql_databak.sh # create a file? Vim mysql_databak.sh # editing
Automatic MYSQL backup in Ubuntu
Note:
?
?
Steps:
?
1. Create a directory to save the backup file:/var/mysqlbak/data
?
?
2. Create a backup script file:/var/mysqlbak/mysql_databak.sh
?
Cd/var/mysqlbak # enter the Directory
?
Touch mysql_databak.sh # create a file
?
Vim mysql_databak.sh # edit the file and enter the following content:
?
######################################## ######################################## #######################
?
#! /Bin/sh
?
DUMP =/usr/bin/mysqldump # mysqldump backup program execution path
?
OUT_DIR =/home/mysql_data # backup file storage path
?
LINUX_USER = root # system username
?
DB_NAME = zhongchan # Name of the database to be backed up
?
DB_USER = root # database account Note: Non-root users must use the backup parameter -- skip-lock-tables; otherwise, an error may occur.
?
DB_PASS = root # Database Password
?
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.
?
Cd $ OUT_DIR # enter the backup storage directory
?
DATE = 'date + % Y _ % m _ % d' # obtain the current system time
?
OUT_ SQL = "$ DATE. SQL" # Name of the backup database file
?
TAR_ SQL = "mysqldata_bak_1_date.tar.gz" # Name of the final database backup file
?
$ DUMP-u $ DB_USER-p $ DB_PASS $ DB_NAME -- default-character-set = utf8 -- opt-Q-R -- skip-lock-tables> $ OUT_ SQL # backup
?
Tar-czf $ TAR_ SQL./$ OUT_ SQL compressed into .tar.gz format
?
Rm $ OUT_ SQL # Delete backup files in. SQL format
?
Chown $ LINUX_USER: $ LINUX_USER $ OUT_DIR/$ TAR_ SQL # change the owner of the backup database file
?
Find $ OUT_DIR-name "mysqldata_bak *"-type f-mtime + $ DAYS-exec rm {}\; # Delete the backup file seven DAYS ago (Note :{}\; there is a space in the middle)
?
######################################## ######################################## #######################
?
?
?
3. Modify file attributes to make them executable.
?
Chmod + x/var/mysqlbak/mysql_databak.sh
?
4. Modify/etc/crontab
?
Vim/etc/crontab # edit the file and add
?
45 22 * root/var/mysqlbak/mysql_databak.sh # indicates that backup is performed at every day.
?
?
5. Restart crond to make the settings take effect.
?
Service cron stop # stop
?
Service cron start # start
?
Chkconfig cron on # Set to boot
?
You can view compressed files such as mysqldata_bak_2012_04_11.tar.gz in the/var/mysqlbak/datadirectory every day.
?
To restore a file, extract the file tar-zxvf mysqldata_bak_2012_04_11.tar.gz.
?
Import the data to the database.
?
So far, the MySql database backup script under Ubuntu Server is complete.