Let PHP regularly run the backup MySQL

Source: Internet
Author: User
Let PHP regularly run the backup MySQL!

Author: phpteam, ptker

I believe many readers (PhP fans) often complain during their work and study: how can we enable PHP to send messages automatically at regular intervals in win ?? How can I enable MySQL to achieve automatic backup without any worries ?? If you rely solely on manual work, you can also achieve it, but the operation seems too cumbersome! Don't worry, use the system's task planProgram(Windows 98 is called a scheduled task) This problem can be easily solved. This feature is often ignored by many users or never imagined :).

The so-called task plan is to use a computer to automatically call the user's preset applications, so as to simplify user operations. We can schedule any script, program, or document to run at the most appropriate time using the Windows 2000 Task Scheduler (equivalent to the cron program under * nix, which is not detailed here, to meet your own needs. The following uses Windows 2000 as an example.

Specifically, if you need to use the task scheduler to run automatically, perform the following steps:

1. click Start, select "program"> "attachment"> "System Tools"> "task plan" (or "set"> "Control Panel"> "Task Plan "), start the task scheduler Management Program for Windows 2000.

2. in the "Task Scheduler" window, double-click the "add Task Scheduler" icon to start the "Task Scheduler wizard" of the system, and then click the "Next" button, select the application to be automatically run in the program list, and click "Next.
Set the appropriate task plan name and select the time frequency for Automatic Execution of the task (such as daily, weekly, monthly, one-time, every start of the computer, and every login time ), click "Next.
At this time, the system will require the user to set the specific time for the program to run, such as the number, the number of minutes, and the time period to run. We only need to set the time according to our own needs.

4. The system will ask the user to set the appropriate user name and password (as shown in Figure 5) so that the system can run automatically in the future.

5. finally, you only need to click the "finish" button to add the corresponding task to the task scheduler in Windows 2000. Then it will automatically "remember" The task, once the system time and related conditions are consistent with the user-defined plan, it automatically calls the user-specified application, which is very convenient (every time Windows 2000 is started, the task scheduler will be automatically started and run in the background to ensure that your plans can be executed on time ).

Now let's test whether the task we just created is successful. Right-click the "php" program icon (6) and select "run" in the pop-up menu ". In general, the program icon can be started as long as it is activated and running. If the operation fails, check whether the user and password are correctly set, and check whether the "Task Scheduler" service has been started. I turned it off to save system resources, causing the operation to fail, I have been searching for it for a long time. In addition, you can view the cause of the failure in the system log.

Now we have discussed the application of such a multi-task plan. Here are two examples:

1. run php regularly!
1. Edit as follows:CodeAnd save it as test. php:
<? PHP
$ Fp = @ fopen ("test.txt", "A + ");
Fwrite ($ FP, date ("Y-m-d h: I: s"). "Let PHP run regularly! \ N ");
Fclose ($ FP );
?>

2. Add a task scheduler. in step 2, enter the command D: \ PhP4 \ php.exe-q D: \ PhP4 \ test. php.

3. Set the time to run the task once every one minute and then run the task.

4. Now let's see if the content in the D: \ PhP4 \ test.txt file is successful. If the content is as follows, congratulations.

11:08:01 let PHP run on a regular basis!
11:09:02 let PHP run on a regular basis!
11:10:01 let PHP run on a regular basis!
11:11:02 let PHP run on a regular basis!

Ii. make it possible for MySQL to implement automatic backup!
1. Edit the code below and save it as backup.php. If you want to compress the code, you can copy rar.exe:
<? PHP
If ($ argc! = 2 | in_array ($ argv [1], array ('-- help ','-? '))){
?>
Backup ver 0.01, for Win95/Win98/winnt/Win2000/WINXP on i32
Copyright (c) 2000 ptker All Rights Reserved.
This is free software, and you are welcome to modify and redistribute it
Under the GPL license

PHP shell script for the backup MySQL database.

Usage: <? PHP echo $ argv [0];?> <Option>

<Option> can be database name you wowould like to backup.
With the -- help, or -? Options, you can get this help and exit.
<? PHP
} Else {
$ Dbname = $ argv [1];
$ Dump_tool = "C: \ mysql \ bin \ mysqldump ";
$ Rar_tool = "d :\\ PhP4 \ RAR ";
@ Exec ("$ dump_tool -- opt-u user-ppassword $ dbname>./$ dbname. SQL ");
@ Exec ("$ rar_tool A-ag_yyyy_mm_dd_hh_mm mongodbname.rar $ dbname. SQL ");
@ Unlink ("$ dbname. SQL ");
Echo "backup complete! ";
}
?>

2. Add a task scheduler and enter the following command in step (2:
D: \ PhP4 \ php.exe-q D: \ PhP4 \ backup. php databasename
3. Set the time to run the task once every day and then run the task.
4. An rarfile consisting of the Database Name and current time will be generated in the D: \ PhP4 \ directory.
5. Congratulations! Success!

Of course, there are many backup methods, so readers can do what they like!

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.