php備份程式

回覆文章
布魯斯
Site Admin
文章: 211
註冊時間: 週四 2月 16, 2006 3:34 pm

php備份程式

文章 布魯斯 »

1.備份您的資料庫email給您自己

代碼: 選擇全部

<?
$datestamp = date("Y-m-d");      // 您的資料會備份到以日期方式命名的格式YYYY-MM-DD

/* CONFIGURE THE FOLLOWING SEVEN VARIABLES TO MATCH YOUR SETUP */
$dbuser = "";            // 您的Database username
$dbpwd = "";            // 您的Database password
$dbname = "";            // 您的Database name. 如果您要備份所有的資料庫, 請使用 --all-databases
$filename= "backup-$datestamp.sql.gz";   // The name (and optionally path) of the dump file

$to = "you@remotesite.com";      // 您要收備份檔的 Email address
$from = "you@yourhost.com";      // 這封Email顯示來自於哪個 Address .
$subject = "MySQL backup file";      // 這封Email的主旨

$command = "mysqldump -u $dbuser --password=$dbpwd $dbname | gzip > $filename";
$result = passthru($command);

$attachmentname = array_pop(explode("/", $filename));   // If a path was included, strip it out for the attachment name

$message = "Compressed database backup file $attachmentname attached.";
$mime_boundary = "<<<:" . md5(time());
$data = chunk_split(base64_encode(implode("", file($filename))));

$headers = "From: $from\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: multipart/mixed;\r\n";
$headers .= " boundary=\"".$mime_boundary."\"\r\n";

$content = "This is a multi-part message in MIME format.\r\n\r\n";
$content.= "--".$mime_boundary."\r\n";
$content.= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$content.= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$content.= $message."\r\n";
$content.= "--".$mime_boundary."\r\n";
$content.= "Content-Disposition: attachment;\r\n";
$content.= "Content-Type: Application/Octet-Stream; name=\"$attachmentname\"\r\n";
$content.= "Content-Transfer-Encoding: base64\r\n\r\n";
$content.= $data."\r\n";
$content.= "--" . $mime_boundary . "\r\n";

mail($to, $subject, $content, $headers);

unlink($filename);   //delete the backup file from the server
?>
2.備份您的資料庫 FTP到另一處

代碼: 選擇全部

<?
$datestamp = date("Y-m-d");      // 您的資料會備份到以日期方式命名的格式YYYY-MM-DD

/* CONFIGURE THE FOLLOWING THREE VARIABLES TO MATCH YOUR SETUP */
$dbuser = "";            // 您的Database username
$dbpwd = "";            // 您的Database password
$dbname = "";            // 您的Database name. 如果您要備份所有的資料庫, 請使用 --all-databases
$filename= "backup-$datestamp.sql.gz";   // The name (and optionally path) of the dump file

$command = "mysqldump -u $dbuser --password=$dbpwd $dbname | gzip > $filename";
$result = passthru($command);

/* CONFIGURE THE FOLLOWING FOUR VARIABLES TO MATCH YOUR FTP SETUP */
$ftp_server = "";   // 請填入您要備份到哪一個 FTP空間去(IP或Domain name),注意,此欄位不要填開頭的 ftp://
$ftp_port = "21";            // FTP port - 留空白也是預設 21
$ftp_username = "anonymous";         // 您登入這個 FTP的帳號, 如果是匿名請填 anonymous
$ftp_password = "";         // 您登入這個 FTP的密碼,如果是匿名請留空白

// set up basic connection
$ftp_conn = ftp_connect($ftp_server);

// Turn PASV mode on or off
ftp_pasv($ftp_conn, false);

// login with username and password
$login_result = ftp_login($ftp_conn, $ftp_username, $ftp_password);

// check connection
if ((!$ftp_conn) || (!$login_result))
{
   echo "FTP connection has failed.";
   echo "Attempted to connect to $ftp_server for user $ftp_username";
   exit;
}
else
{
   echo "Connected to $ftp_server, for user $ftp_username";
}

// upload the file
$upload = ftp_put($ftp_conn, $filename, $filename, FTP_BINARY);

// check upload status
if (!$upload)
{
   echo "FTP upload has failed.";
}
else
{
   echo "Uploaded $filename to $ftp_server.";
}

// close the FTP stream
ftp_close($ftp_conn);

unlink($filename);   //delete the backup file from the server
?>
3.備份您的網站資料 FTP到另一處

代碼: 選擇全部

<?
$datestamp = date("Y-m-d_H-i-s");      // 您的資料會備份到以日期方式命名的格式YYYY-MM-DD

/* CONFIGURE THE FOLLOWING VARIABLES TO MATCH YOUR SETUP */
$filename= "Full_Account_Backup-$datestamp.tar";   // The name (and optionally path) of the dump file
$ftp_server = "123.123.123.123";      // 請填入您要備份到哪一個 FTP空間去(IP或Domain name),注意,此欄位不要填開頭的 ftp://
$ftp_port = "21";   // FTP port - 留空白也是預設 21
$ftp_username = "anonymous";      // 您登入這個 FTP的帳號, 如果是匿名請填 anonymous
$ftp_password = "";      // 您登入這個 FTP的密碼,如果是匿名請留空白
$filename = "/home/YOURACCOUNT/" . $filename . ".gz";

$command = "tar cvf ~/$filename ~/*";
$result = exec($command);

$command = "gzip -9 -S .gz ~/$filename";
$result = exec($command);

// set up basic connection
$ftp_conn = ftp_connect($ftp_server);

// Turn PASV mode on or off

ftp_pasv($ftp_conn, false);

// login with username and password
$login_result = ftp_login($ftp_conn, $ftp_username, $ftp_password);

// check connection
if ((!$ftp_conn) || (!$login_result))
{
   echo "FTP connection has failed.";
   echo "Attempted to connect to $ftp_server for user $ftp_username";
   exit;
}
else
{
   echo "Connected to $ftp_server, for user $ftp_username";
}

// upload the file
$upload = ftp_put($ftp_conn, "foo.tar.gz", $filename, FTP_BINARY);

// check upload status
if (!$upload)
{
   echo "FTP upload has failed.";
}
else
{
   echo "Uploaded $filename to $ftp_server.";
}

// close the FTP stream
ftp_close($ftp_conn);

unlink($filename);   //delete the backup file from the server
?>
最後可以設定crontab中執行

代碼: 選擇全部

php -q /home/username/public_html/folder/name.php
回覆文章