快捷搜索:  

WIN服务器下使用PHP代码自动备份MYSQL

一、让PHP定时运行吧!
1、 编辑如下代码,并保存为test.php:
<?php
$fp = @fopen("test.txt", "a+");
fwrite($fp, date("Y-m-d H:i:s") . " 让PHP定时运行吧!\n");
fclose($fp);
?>

2、添加一个任务计划,在输入命令:D:\php4\php.exe -q D:\php4\test.php

3、时间设置为每隔1分钟运行一次,然后运行这个任务。

4、现在我们来看看d:\php4\test.txt文件的内容时候是否成功。如果内容为如下所示,那么恭喜你成功了。

2003-03-03 11:08:01 让PHP定时运行吧!
2003-03-03 11:09:02 让PHP定时运行吧!
2003-03-03 11:10:01 让PHP定时运行吧!
2003-03-03 11:11:02 让PHP定时运行吧!

二、让MYSQL实现自动备份变成可能!
1、编辑如下代码,并保存为backup.php,如果要压缩可以拷贝一个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 would 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 $dbname.rar $dbname.sql");
@unlink("$dbname.sql");
echo "Backup complete!";
}
?>

2、添加一个任务计划,在(如图2所示)这一步输入命令:
D:\php4\php.exe -q D:\php4\backup.php databasename
3、时间设置为每天运行一次,然后运行这个任务。
4、最后会在d:\php4\目录下生成一个以数据库名和当前时间组成的rar文件。
顶(0)
踩(0)

您可能还会对下面的文章感兴趣:

最新评论