The server backs up the MySQL database and automatically packs the database every morning, and deletes the backup files five days ago.

Source: Internet
Author: User

Finally, let the server back up the MySQL database every morning and automatically package it, and delete the backup files five days ago. Share as follows:

1. Environment: Windows Server 2003 + Apache 2.0 + PhP5 + MySQL 4.0.26.

2. Assume that the PHP installation directory is D:/PHP, And the MySQL installation directory is D:/MySQL.

3. Create the WinRAR directory in D:/PHP and copy winrar.exe and rarreg. Key in the WinRAR installation directory to D:/PHP/WinRAR.

4. D:/PHP file mysql_backup.php:

/* // # filename: mysql_backup.php # Author: faisun # Website: http://www.softpure.com //////////// // Save the directory, the path must use a backslash. You need to create it manually. $ Store_folder = 'd: \ databse_backup '; // user name and password // This account must have the permission to operate [all] databases and files // otherwise, some databases cannot be backed up $ db_username = "root"; $ db_password = ""; $ time = Time (); $ nowdir = "$ store_folder \". date ("ymd", $ time ). "; if(file_exists({%nowdir.rar") Die ("file exists. \ n "); @ mkdir ($ nowdir); mysql_connect (" localhost "," $ db_username "," $ db_password "); $ query = mysql_list_dbs (); while ($ result = mysql_fetch_array ($ query) {system (Dir Name (_ file __). '\.. \ mysql \ bin \ mysqldump -- opt '. "$ result [database]-U {$ db_username}". ($ db_password? "-P {$ db_password}": ""). "> $ nowdir \ $ result [database]. SQL "); echo" dumping database '$ result [database]'... \ n ";} echo" \ nwinrar loading... \ n "; System (dirname (_ file __). "\ WinRAR \ winrar.exe A-EP1-R-O +-M5-DF \ has reached nowdir.rar \" "$ nowdir \" "); // delete the file five days ago @ unlink ("$ store_folder \". date ("ymd", $ time-86400 * 52.16.w..rar "); echo" \ nok! \ N ";>

5. D:/PHP creates the file mysql_backup.bat, which contains only one sentence:

Php.exe mysql_backup.php

6. Double-click the BAT file to run the task. If the file can be backed up, OK. Next, add the task plan.

7. add D:/PHP/mysql_backup to the task schedule. Select daily as the time. based on the monitoring results of the server, the traffic is low every morning from 5 to 8. because some database cleanup operations are performed during the 5-7 period, you can set the time to eight o'clock in the morning. Detailed source reference: http://www.jb51.net/article/5416.htm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.