Regular backup of MySQL is an important task, but manual operation is too cumbersome and it is difficult to avoid omissions. Use the following method to make the system regularly back up data.
◆1. Create a backup folder
#cd /www
#makedir backup
◆2. Write a running script
#vi autobackup
Write the following:
filename=`date +%Y%m%d`
mysql_bin_dir/mysqldump –opt dataname -u user -ppassword | gzip > /www/mysqlbackup/name$filename.gz
Save and exit
illustrate:
(1) mysql_bin_dir: bin path of mysql;
(2)dataname: database name;
(3)user: database user name;
(4)password: user password;
(5)name: Customize the backup file prefix identifier.
As in the above example, the mysql database will be automatically backed up and stored in gzip compression, with the file name being name20080101.gz.
◆3. Add execution permissions to the script
#chmod +x autobackup
◆4. Let crontab complete regularly executed tasks
In this step, Redhat's method will be different, which will be given later.
Edit crontab:
#vi /etc/crontab
In the last line add:
01 5 * * * root /www/autobackup
Run the script at 5 o'clock every day. You can also modify 5 to other specified times.
Redhat method:
Redhat's crontab uses four directories based on time (/etc/cron.hourly: every hour; /etc/cron.daily: every day; /etc/cron.weekly: every week; /etc/cron.monthly: every month) The way the script is run.
In Redhat, you only need to copy the script you just edited to the corresponding directory.
◆5. Restart crontab
#/etc/rc.d/init.d/crond restart