Step 1: Define the backup directory on your Linux Server:
CopyCode The Code is as follows: mkdir/var/lib/mysqlbackup
CD/var/lib/mysqlbackup
Step 2: The following is the most important step: Write a regular backup script.
Copy code The Code is as follows: VI dbbackup. Sh
The code file is as follows:
Copy code The Code is as follows :#! /Bin/sh
# Mysql Data backup script
#
# Use mysqldump -- help, get more detail.
#
Bakdir =/root/back/ysqlbackup
Logfile =/root/back/MySQL/mysqlbak. Log
Date = 'date + % Y % m % d % H % m % s'
Echo ""> $ logfile
Echo ""> $ logfile
Echo "-----------------------------------------"> $ logfile
Echo $ (date + "% Y-% m-% d % H: % m: % s") >>$ logfile
Echo "--------------------------"> $ logfile
CD $ bakdir
Dumpfile = $ date. SQL
Gzdumpfile1_date. SQL .tar.gz
/Usr/local/bin/mysqldump-u user-pname -- database-H 127.0.0.1 dB> $ dumpfile
Echo "Dump done" >>$ logfile
Tar czvf $ gzdumpfile $ dumpfile >>$ logfile 2> & 1
Echo "[$ gzdumpfile] backup success! ">>> $ Logfile
Rm-F $ dumpfile
# Cd $ bakdir/daily
CD $ bakdir
Echo "backup done! "
Echo "Please check $ bakdir directory! "
Echo "copy it to your local disk or FTP to somewhere !!! "
Find $ bakdir-ctime + 30-exec RM {};
Echo "delete file over 30 days"
Tip: Modify the user, name, and DB according to your situation.
Save and exit, and then grant the executable permission to the file:
Copy code The Code is as follows: # chmod 777 mysqlautobackup. Sh
Run the backup script code regularly with crontab:
Crontab-e
If you back up data at three o'clock P.M. every day, add the following code,
Code:
Copy code The Code is as follows: 20 15 ***/var/lib/mysqlbackup/dbbackup. Sh
In this way, you can back up the MySQL database regularly every day.