最好的办法是检查脚本上次运行的时间,然后上传带有ftp_*
函数的文件夹。
<?php
$username = 'root'; // and this
$password = 'password'; // this also
$host = 'my-remote-server.com'; // and this
$remote_backup = 'backups/'; // folder on remote server to upload to
$backup_folder = 'to_backup/'; // folder to backup
$temp_folder = 'temp_files/'; // a folder on the local server i can write to
$last_run = file_get_contents("{$temp_folder}last_run.txt"); // You'll probably want to get this from a database instead
if($last_run <= strtotime('-1 day'))
{
file_put_contents("{$temp_folder}last_run.txt", time()); // Update the last time this was ran
$file = time() . '_backup.zip'; // what the file will be called both remotely and locally
$ftp = ftp_connect($host); // connect to the ftp server
ftp_login($ftp, $username, $password); // login to the ftp server
$zip = new ZipArchive; // create a new instance of ZipArchive
$zip->open($temp_folder . $file, ZIPARCHIVE::CREATE); // Create a new archive
foreach(glob($backup_folder . '*') as $file) // Loop through all files in the local backup directory
{
$zip->addFile($file); // add that file
}
ftp_chdir($ftp, $remote_backup); // cd into the remote backup folder
$upload = ftp_nb_put($ftp, $remote_backup . $file, $temp_folder . $file); // non-blocking put, uploads the local backup onto the remote server
while($upload === FTP_MOREDATA)
{
// do something else while we're waiting for the non-blocking upload to finish
}
ftp_close($ftp); // closes the connection
}
它应该是非阻塞的(嗯 - 上传到远程服务器),所以如果你没有很多文件要压缩,它会很好地包含在index page
例如。没有任何错误处理,因此您可能想要添加它。它也不会删除本地备份,您可能想要处理它。