Use the crontab system to regularly back up the MySQL database every day

  
        

Using the system crontab to periodically execute backup files, save the backup results by date, and achieve the purpose of backup.

1. Create a path to save the backup file /mysqldata #mkdir /mysqldata 2. Create /usr/sbin/bakmysql file #vi /usr/sbin/bakmysql Enter rq=` date +%Y%m%d ` tar zcvf /mysqldata/mysql$rq.tar.gz /var/lib/mysql or write rq=` date +%Y%m%d ` mysqldump --all-databases -u root -p980405 > /mysqldata/mysql $rq.tar.gz /var/lib/mysql is the directory of your database files, some users are /usr/local/mysql/data, each person may be different /mysqldata/means to save the backup file directory, this everyone can also Do it according to your own requirements. 3, modify the file properties to make it executable # chmod +x /usr/sbin/bakmysql 4, modify /etc/crontab #vi /etc/crontab Add 01 3 * * * root /usr/sbin/bakmysql Perform backup at 3 o'clock, restart crond # /etc/rc.d/init.d/crond restart to complete. So every day you can see such a file in /mysqldata mysql20040619.tar.gz You can download it directly. Stop the database service process or lock the database before the tar command is executed. Otherwise, when the data is restored, the database will be damaged. If you are lucky, you can repair it. If you are not lucky, you will not be able to.

Copyright © Windows knowledge All Rights Reserved