スクリプトが最後に実行されたことを確認してから、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
に含めて大丈夫です。エラー処理がないので、それを追加することもできます。また、ローカルバックアップも削除しません。どちらかを処理したい場合もあります。
あなたがファイルを転送するために使用するプロトコルは何ですか? ftp? ssh?その他?あなたはHTTPについて話していますが、例えば、リモートサーバから '.php'ファイルをバックアップしたい場合には、問題が発生します。 –
http://stackoverflow.com/questions/1691530/sending-a-file-via-http-put-in-php – Cypher
もう1つはここにあります。 – transilvlad