打印

虛擬主機目錄及資料庫定期自動備份程式說明

本主題由 jimi 於 2009-12-23 14:24 解除高亮

虛擬主機目錄及資料庫定期自動備份程式說明

很多的站長其實在架設網站的時候,好像都不太在意有關於網站和資料庫備份的問題,也許吧,通常您的網站是不會有什麼問題,除非是站長您自己將您的資料毀滅掉,不然我們介紹的主機公司,如果是它們本身有問題,甚至於當您違反規定而被主機公司停用,它們還是會給您備份檔案 ~

機率不大,但是有備無患,底下我們列出幾個備份的程式碼供各位參考,包括 3種,分別是 1.備份您的資料庫email給您自己,2.備份您的資料庫 FTP到另一處,3.備份您的網站資料 FTP到另一處

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
?>
OK !!

最後,這個程式您可以分別命名存檔成 PHP的檔案,然後在 CPanel或是 vDeck中的定期作業(Cron job)中加進去,類似像下面這一行(name.php是您的檔案命名,username是您在主機公司的帳號,folder是您程式放的目錄名稱)
複製內容到剪貼板
代碼:
php -q /home/username/public_html/folder/name.php
如果您目錄和資料庫都備份,我們的建議如下 :

1. 如果網站檔案不常異動,建議一周備份一次即可
2. 資料庫可以每天備份,但請注意,這兩個程式在CPanel中的執行時間請間隔至少 30分鐘以上

有任何問題歡迎您的詢問囉 ~

吉米

TOP

hello 吉米:
我自已實測後結果如下
如果在ie上打入http://www.lovepd.org/backuptest/backup_sql.php
那系統的確正確無誤的傳了一封mysql 資料庫的備份檔給我

但在cpanel 的 cron job 功能中 分別打入以下測試
測試一
command to run:public_html/backuptest/backup_sql.php
結果mail傳回以下訊息
/bin/sh: public_html/backuptest/backup_sql.php: Permission denied

測試二
command to run:php -q/public_html/backuptest/backup_sql.php
結果mail傳回以下訊息
/bin/sh: lovepd0/www/backuptest/backup_sql.php: No such file or directory

我找過lunarpage 的userguide(http://www.lunarpages.com/new_manual/index.html)
設定cron jobs的部份,在範例中也沒有php -q的command  ??
維一正常的就是時間設定後的回傳。
仍然無法成功備份啦
吉米大大請問這是什麼回事呢?

TOP

Dear :

測試一和測試二的路徑您寫的都不對

試試 :
php -q /home/lovepd0/public_html/backuptest/backup_sql.php



吉米

TOP

備份成功

Hello 吉米
改了之後的確成功收到備份檔囉
可以請問一下嗎。那個-q代表什麼意思阿。
我要在那裡找到更多其它的用法

TOP

...成功就好囉 ~

PHP大概買本書來看看吧 ~ 不然線上的文件也可以查一查 !

PHP.NET 的官網有關 command line 的說明應該在這一頁吧
您可以參考 : http://www.php.net/manual/en/features.commandline.php

TOP

to jimi大
之前使用"1.備份您的資料庫email給您自己"來備份自已的數據庫
一直以來都很正常
但最近系統卻出現問題
20-May-2009 20:00:12] PHP Fatal error:  Allowed memory size of 33554432 bytes exhausted (tried to allocate 8987854 bytes) in /home/fiber/backup/sql_backup.php on line 37

---------------------------------------------------------------
但第37行為
$content.= $data."\r\n";

請問要如何解決呢??

TOP

Dear fiber2000 :

既然你是用 email的,備份檔案應該不算太大 ?
有再跑一次看看嗎 ? 還是之後就一直這樣有錯誤訊息 ?

吉米

TOP

引用:
原帖由 jimi 於 2009-5-22 00:06 發表
Dear fiber2000 :

既然你是用 email的,備份檔案應該不算太大 ?
有再跑一次看看嗎 ? 還是之後就一直這樣有錯誤訊息 ?

吉米
吉米大..連試二天都還是出現一樣的問題
23-May-2009 20:00:11] PHP Fatal error:  Allowed memory size of 33554432bytes exhausted (tried to allocate 8987854 bytes) in/home/fiber/backup/sql_backup.php on line 37

  這是email的問題嗎??
若是轉到申請的備份主機那...是否問題就會消失呢?
謝謝

TOP

Dear fiber2000 :

試試看 : http://cocohosting.org/forum/thread-331-1-2.html

但是您不是要把檔案變成 2MB,前半段照著做 ~
您要把 PHP設定的記憶容量變大一點

在 php.ini中將
memory_limit = 32M
改成
memory_limit = 64M
試看看吧 !!

或是在備份程式中的第 37行以前,加上下面這一行,申請大一點的記憶體容量
複製內容到剪貼板
代碼:
ini_set( 'memory_limit', '64M' );
吉米

TOP

引用:
原帖由 jimi 於 2009-5-25 12:11 發表
Dear fiber2000 :

試試看 : http://cocohosting.org/forum/thread-331-1-2.html

但是您不是要把檔案變成 2MB,前半段照著做 ~
您要把 PHP設定的記憶容量變大一點

在 php.ini中將
memory_limit = 32M
改成
memory_ ...
to jimi:
剛發現WebHostingPad只提供32mb,而且php.ini無法改..orz
我看我還是利用cocohosting所提供的備份主機..來備份好..

TOP

Dear fiber2000 :

自己寫一份 php.ini放到您的網站目錄下 ~
會生效嗎 ??

我沒有試過 Webhostingpad,Lunarpages和很多家使用 CPanel的虛擬主機商這樣做是OK的 ~

吉米

TOP

吉米
我的備份問題搞定了
http://domainclub.org/showthread.php?t=25200

[ 本帖最後由 hcrackpot 於 2009-6-26 21:25 編輯 ]

TOP

hi..吉米,
我試了2.備份您的資料庫 FTP到另一處,可以正常,

3.備份您的網站資料 FTP到另一處 ,
show:

Warning: ftp_put(/home/xxx/public_html/xxx/Full_Account_Backup-2009-09-12_23-21-59.tar.gz) [function.ftp-put]: failed to open stream: No such file or directory in /home/xxx/public_html/xxx/backup.php on line 41
FTP upload has failed.
Warning: unlink(/home/xxx/public_html/xxx/Full_Account_Backup-2009-09-12_23-21-59.tar.gz) [function.unlink]: No such file or directory in /home/xxx/public_html/xxx/backup.php on line 56

xxx是用戶名,不想公開..thx

[ 本帖最後由 gg5033 於 2009-9-13 12:25 編輯 ]

TOP