打印

PHP备份progress数据库

PHP备份progress数据库

第一次将PHP当作shell用,纪念一下

[code:1]
#!/opt/lampp/bin/php -q
<?php
/**
* \文件名:backup.php
* 描述:用於erp資料庫表的備份
* 維護人員:Waterfrog
* 創建日期:2006-10-17
*/
require("Mail.php");

$BKDIR="/backup/erp";
$BKDB=array("admapsh","disapsh","finapsh","humapsh","mfgapsh");
$DBDIR="/dbs/v9db";
$PROUTIL="/usr/bin/proutil";
$date=date("YmdH");

$LOGFILE="log.txt";
$fLog=fopen($LOGFILE,"a");

//判斷備份目錄是否存在,如果不存在並且備份目錄有寫的許可權,創建該目錄,否則報錯.
if (! file_exists($BKDIR."/".$date) && is_writable($BKDIR))
{
        mkdir($BKDIR."/".$date);
        $curbakdir=$BKDIR."/".$date;
}
elseif (file_exists($BKDIR."/".$date) && is_dir($BKDIR."/".$date))
{
        $curbakdir=$BKDIR."/".$date;
}
else
{
        fputs($fLog,"Open  or Create Directory  ".$BKDIR."/".$date." Error!\n");
        $backuperror=1;
        $backuperrormsg.="打開或創建".$BKDIR."/".$date."目錄有錯誤\n";
}


//根據需要備份的庫名稱,在備份目錄下創建該庫的備份目錄並對表進行備份工作
for ($i=0;$i<count($BKDB);$i++)
{
        //如果不存在備份目錄,創建該目錄
        if (! file_exists($curbakdir."/".$BKDB[$i]) && is_writable($curbakdir))       
        {
                mkdir($curbakdir."/".$BKDB[$i]);
        }
        else
        {
                fputs($fLog,"Open  or Create Directory  ".$curbakdir."/".$BKDB[$i]." Error!\n");
                $backuperror=1;
                $backuperrormsg.="打開或創建".$curbakdir."/".$$BKDB[$i]."目錄有錯誤\n";
        }
       
        //從文本文件中讀取該備份庫中的所有表
        if (! $fp=fopen($BKDB[$i],"r"))
        {
                fputs($fLog,"Open table file ".$BKDB[$i]." Error!\n");
                $backuperror=1;
                $backuperrormsg.="打開資料表文件 ".$$BKDB[$i]." 時出錯\n";
        }
       
        //歷遍資料表文件,並對裏面的所有表進行備份
        while ($bktable=trim(fgets($fp)))
        {
                if ($bktable=="")        //如果為空行,繼續迴圈
                {        continue;        }
               
                unset($returnvalue);
                //對表進行備份
                system( $PROUTIL." /dbs/v9db/".$BKDB[$i]."  -C dump pub.".$bktable." ".$curbakdir."/".$BKDB[$i]."/ ",$returnvalue);
                //echo $PROUTIL." /dbs/v9db/".$BKDB[$i]."  -C dump pub.".$bktable." ".$curbakdir."/".$BKDB[$i]."/ \n";
               
                if ($returnvalue)        //如果備份失敗,將備份命令行及相關資訊輸入到變數中
                {
                        $backuperror=1;
                        fputs($fLog,$PROUTIL." /dbs/v9db/".$BKDB[$i]."  -C dump pub.".$bktable." ".$curbakdir."/".$BKDB[$i]."/ \n");
                        $backuperrormsg.=$PROUTIL." /dbs/v9db/".$BKDB[$i]."  -C dump pub.".$bktable." ".$curbakdir."/".$BKDB[$i]."/ \n";
                        $backuperrormsg.="備份 ".$BKDB[$i].".".$bktable." 表時失敗\n";
                }

        }


       
}

//將df檔拷入到備份目錄,每次資料庫結構或升級後需重建df檔及表列表檔
` cp ./*apsh $BKDIR./$date `; //將表列表檔拷入備份目錄
` cp ./*.df $BKDIR./$date `;        //將資料庫定義檔拷入備份目錄



//將備份目錄進行壓縮
unset($returnvalue);
system("tar --remove-files -zcvf ".$BKDIR."/".$date.".tar.gz  ".$BKDIR."/".$date , $returnvalue);

if ($returnvalue)
{
        $backuperror=1;
        fputs($fLog,"tar --remove-files -zcvf ".$BKDIR."/".$date.".tar.gz  ".$BKDIR."/".$date."\n");
        $backuperrormsg.="tar --remove-files -zcvf ".$BKDIR."/".$date.".tar.gz  ".$BKDIR."/".$date."\n";
        $backuperrormsg.="壓縮 ".$BKDIR."/".$date.".tar.gz 時出錯!!\n";
}

//設置mail基本參數
$conf['mail'] = array('host' => '192.168.7.112',
                                                'auth' => true,
                                                'username' => '帐号',
                                                'password' => '密码'
                                                );

$headers['From'] = 'root@at-scale.com';
$headers['To'] = '我的邮箱,别人的邮箱';

$headers['Subject'] = 'ERP備份提示';
$mail_object = &Mail::factory('smtp', $conf['mail']);

//


//如果備份過程中存在錯誤資訊,將出錯資訊?#93;件給mis所有人員
if ($backuperror)
{
        $headers['Subject'].= "--ERP Backup have troubles!!!";
        $mail_res=$mail_object->send($headers['To'], $headers, $backuperrormsg);
}

else
{
        $headers['Subject'].= "--ERP Backup success!!!";
        $msg=$date." ERP backup success!!!";
        $mail_res=$mail_object->send($headers['To'], $headers, $msg);
}

?>
[/code:1]
^-^水浴清蟾^-^ http://waterfrog.cn http://waterfrog.blog.com.cn 水浴清蟾的池塘

TOP

用PostgreSQL的命令行 http://www.postgresql.org/docs/8.1/static/backup.html#BACKUP-DUMP
An individual human existence should be like a river - small at first, narrowly contained within its banks, and rushing passionately past boulders and over waterfalls. Gradually the river grows wider, the banks recede, the waters flow more quietly, and in the end, without any visible break, they become merged in the sea, and painlessly lose their individual being.

TOP

楼上,如果是远程服务器没有开通 ssh ,就只能用 php 代码了……
不想要 ID 了请找我。

TOP

二位樓上的,我是說Progress數據庫而不是postgresql
http://www.progress.com/index.ssp
要是postgresql或是mysql之類的,就不用傷這麼多腦筋了.
^-^水浴清蟾^-^ http://waterfrog.cn http://waterfrog.blog.com.cn 水浴清蟾的池塘

TOP