Backupscript in PHP für VHCS

dragonknight

New Member
Hab jetzt mir mal ein Backup Script gebastelt, welche primär für einen VHCS Server gedacht ist:

PHP:
#!/usr/bin/php5 -q
<?php
    #wpWDG Backup Solution Script
$dir = "/root/backup";
$inc = 0;
if(!is_dir($dir)){
    echo ":==>".mkdir ($dir,0777,1);
    $c = fopen($dir."/increment.num","w");
    fwrite($c,"1");
    fclose($c);
    $inc = 1;
}else{
    $c = fopen($dir."/increment.num","r");
    $inc = fgetc($c);
    fclose($c);
    $c = fopen($dir."/increment.num","w+");
    fwrite($c,intval($inc)+1);
    fclose($c);
}
if(intval($inc>7)){
    $inc = 1;
    unlink($dir."/backup.7.tar");
}
chmod($dir,0777);
echo "----------------------------------------------------------------------";
echo "Beginning Backup: ".date("r")."";
echo "----------------------------------------------------------------------";
exec("nice --adjustment=19 tar -cz --file=".$dir."/etc.tgz /etc ",$array1,$return);
echo "Backup: /etc";
echo $return;
echo "----------------------------------------------------------------------";
exec("nice --adjustment=19 tar -cz --file=".$dir."/virtual.tgz /var/www/virtual ",$array1,$return);
echo "Backup: /var/www/virtual";
echo $return;
echo "----------------------------------------------------------------------";
exec("nice --adjustment=19 tar -cz --file=".$dir."/vhcs2.tgz /var/www/vhcs2 ",$array1,$return);
echo "Backup: //var/www/vhcs2";
echo $return;
echo "----------------------------------------------------------------------";
exec("nice --adjustment=19 tar -cz --file=".$dir."/mysql_files.tgz /var/lib/mysql ",$array1,$return);
echo "Backup: /var/lib/mysql";
echo $return;
echo "----------------------------------------------------------------------";
exec("nice --adjustment=19 tar -cz --file=".$dir."/mail.tgz /var/mail ",$array1,$return);
echo "Backup: /var/mail";
echo $return;
echo "----------------------------------------------------------------------";
echo "Backup: MySQL Datenbank Backup";
$mysqlbkpdir = "".$dir."/mysql";
if(!is_dir($mysqlbkpdir)){
    mkdir ($mysqlbkpdir,0777,1);
}
chmod($mysqlbkpdir,0777);
system("/usr/bin/mysqldump -uroot -pxxxxxxxx -h localhost db1 > ".$mysqlbkpdir."/db1.sql");
system("/usr/bin/mysqldump -uroot -pxxxxxxxxx -h localhost db2> ".$mysqlbkpdir."/db2.sql");
system("/usr/bin/mysqldump -uroot -pxxxxxxxxxxxxxxxxx -h localhost db3> ".$mysqlbkpdir."/db3.sql");
system("/usr/bin/mysqldump -uroot -pxxxxxxxxxxxxxxxx -h localhost db4> ".$mysqlbkpdir."/db4.sql");
system("/usr/bin/mysqldump -uroot -pyyyyyyyyyyyyyyyy -h localhost db5> ".$mysqlbkpdir."/db5.sql");
exec("nice --adjustment=19 tar -cz --file=".$dir."/mysql.tgz $mysqlbkpdir/*.sql ",$array1,$return);
echo "Backup: MySQL Database Backup: sql";
echo $return;
exec("nice --adjustment=19 tar -c --file=".$dir."/backup.$inc.tar /root/backup/*.tgz ",$array1,$return);
echo "Making Big Archiv";
echo $return;
echo "----------------------------------------------------------------------";
unlink($dir."virtual.tgz");
unlink($dir."vhcs2.tgz");
unlink($dir."mysql_files.tgz");
unlink($dir."mail.tgz");
unlink($dir."mysql.tgz");
unlink($mysqlbkpdir."db1.sql");
unlink($mysqlbkpdir."db2.sql");
unlink($mysqlbkpdir."db3.sql");
unlink($mysqlbkpdir."db4.sql");
unlink($mysqlbkpdir."db5.sql");
echo "Cleaning Up";
echo $return;
echo "----------------------------------------------------------------------";
$conn_id = ftp_connect("ftpurl");
$login_result = ftp_login($conn_id, "user", "passwort");
if ((!$conn_id) || (!$login_result)) {
    ftp_chdir($conn_id,"backup/".$inc);
    echo "If OLD Backup Files Exists, Delete it";
    $delete = ftp_delete($conn_id,"backup.tar");
    echo "Delete: ".$delete;
    echo "Uploading FTP Files";
    $upload = ftp_put($conn_id,"backup.tar","backup.$inc.tar",FTP_BINARY);
    echo "Upload: $upload";
    unlink("$dir/backup.$inc.tgz");
    echo "Removing Local Backup";
    ftp_close($conn_id);
}else{
    echo "Leaving Local Files";
    echo $return;
}
echo "----------------------------------------------------------------------";
echo "Backup Finished at: ".date("r")."";
echo "----------------------------------------------------------------------";
fclose($a);
?>

Ist im Moment ein reines Shell Script.
Für Crons gibts auch die gleiche Variante, welches das Backup in eine Log Datei zur Protokolierung schreibt.
;)
mfg
chris
 
Last edited by a moderator:
Vieleicht solltest du jetzt nochmal darüber nachdenken ob du dein Mysqlpasswort nicht eventuell änderst. :D
 
danke für den hint

^^

Hab aber auch gerad das Update durchgezogen:

shell.php:
PHP:
#!/usr/bin/php5 -q
<?php
#wpWDG Backup Solution Script
/**
* Setup
*/
$dir = "/root/backup";
$ftp_server = "xxx.xxx.xxx.xx";
$ftp_user = "xxxxxxxxxxx";
$ftp_password = "xxxxxxxxxxxxxx";
$ftp_backupdirectory = "backup/";
$localbackup = false;
$mysqluser = "xxxxxxxxxx";
$mysqlpassword = "xxxxxxxxxxxxx";
$inc = 0;
/*vhosts to Sage*/
$wwwfiles[] = "xxxxxxxxxxxxx";
$wwwfiles[] = "xxxxxxxxxxxxx";
$wwwfiles[] = "xxxxxxxxxxxxx";
/*paths*/
$mysqlpath = "/var/lib/mysql";
$mailpath = "/var/mail";
$etcpath = "/etc";
$panelpath = "/var/www/vhcs2";
/*Datenbanken*/
$dbs[] = "xxxxxxxxxxxxx";
$dbs[] = "xxxxxxxxxxxxx";
$dbs[] = "xxxxxxxxxxxxx";
$dbs[] = "xxxxxxxxxxxxx";
$dbs[] = "xxxxxxxxxxxxx";
/**
* Function
*/
function return_filesize($file){
    $a = filesize($file);
    $c = $a / 1024;
    $c = $a / 1024;
    return $c . " Kb";
}
if(!is_dir($dir)){
    echo ":==>".mkdir ($dir,0777,1);
    $c = fopen($dir."/increment.num","w");
    fwrite($c,"2");
    fclose($c);
    define ("inc_number",1);
}else{
    if(!file_exists($dir."/increment.num")){
        $c = fopen($dir."/increment.num","w");
        fwrite($c,"2");
        fclose($c);
        define ("inc_number",1);
    }else{
        $c = fopen($dir."/increment.num","r");
        $inc = fgetc($c);
        define ("inc_number",intval($inc));
        fclose($c);
        $c = fopen($dir."/increment.num","w+");
        fwrite($c,intval($inc)+1);
        fclose($c);
    }
}

if(intval($inc>7)){
    $inc = 1;
}
chmod($dir,0777);
unlink($dir."/backup.$inc.tar");
echo "----------------------------------------------------------------------\n";
echo "| wpWDG Backup Script                                                 |\n";
echo "----------------------------------------------------------------------\n";
echo "| Version:    1.0.0                                                   |\n";
echo "----------------------------------------------------------------------\n";
echo "| Copyright: by wpWDG (http://www.wpwdg.de                            |\n";
echo "----------------------------------------------------------------------\n";
echo "Beginning Backup: ".date("r").""."\n";
$time = time();
echo "----------------------------------------------------------------------\n";
echo "Increment Number: ".inc_number."\n";
echo "----------------------------------------------------------------------\n";
echo "Backup: /etc"."\n";
exec("nice --adjustment=19 tar -cz --file=".$dir."/etc.tgz $etcpath ",$array1,$return);
echo "File Size: ".return_filesize($dir."/etc.tgz")."\n";
foreach($wwwfiles as $key){
    echo "----------------------------------------------------------------------\n";
    echo "Backup: /var/www/virtual/autosport.at"."\n";
    exec("nice --adjustment=19 tar -cz --file=".$dir."/virtual.$key.tgz /var/www/virtual/$key/ ",$array1,$return);
    echo "File Size: ".return_filesize($dir."/virtual.$key.tgz")."\n";

}
echo "----------------------------------------------------------------------"."\n";
echo "Backup: //var/www/vhcs2"."\n";
exec("nice --adjustment=19 tar -cz --file=".$dir."/vhcs2.tgz /var/www/vhcs2 ",$array1,$return);
echo "File Size: ".return_filesize($dir."/vhcs2.tgz")."\n";
echo "----------------------------------------------------------------------"."\n";
echo "Backup: $mysqlpath"."\n";
exec("nice --adjustment=19 tar -cz --file=".$dir."/mysql_files.tgz $mysqlpath ",$array1,$return);
echo "File Size: ".return_filesize($dir."/mysql_files.tgz")."\n";
echo "----------------------------------------------------------------------";
echo "Backup: $mailpath"."\n";
exec("nice --adjustment=19 tar -cz --file=".$dir."/mail.tgz $mailpath ",$array1,$return);
echo "File Size: ".return_filesize($dir."/mail.tgz")."\n";
echo "----------------------------------------------------------------------"."\n";
echo "Backup: MySQL Datenbank Backup"."\n";
$mysqlbkpdir = "".$dir."/mysql";
if(!is_dir($mysqlbkpdir)){
    mkdir ($mysqlbkpdir,0777,1);
}
chmod($mysqlbkpdir,0777);
unset($key);
foreach($$dbs as $key){
    system("/usr/bin/mysqldump -u".$mysqluser." -p".$mysqlpassword." -h localhost $key > ".$mysqlbkpdir."/$key.sql");
}
echo "Backup: MySQL Database Backup: sql"."\n";
exec("nice --adjustment=19 tar -cz --file=".$dir."/mysql.tgz $mysqlbkpdir/*.sql ",$array1,$return);
echo "File Size: ".return_filesize($dir."/mysql.tgz")."\n";
echo "----------------------------------------------------------------------"."\n";
echo "Making Big Archiv"."\n";
exec("nice --adjustment=19 tar -c --file=".$dir."/backup.".inc_number.".tar /root/backup/*.tgz ",$array1,$return);
echo "File Size: ".return_filesize($dir."/backup.".inc_number.".tar")."\n";
echo "----------------------------------------------------------------------"."\n";
echo "Cleaning Up"."\n";
echo "*.tgz ==> ".unlink($dir."/*.tgz")."\n";
$mysqlbkpdir = $mysqlbkpdir."/";
echo $mysqlbkpdir."*.sql ==> ".unlink($mysqlbkpdir."*.sql")."\n";
echo "Removeing MySQL BAckup Directory ==> ".rmdir($mysqlbkpdir)."\n";
echo "----------------------------------------------------------------------"."\n";
$conn_id = ftp_connect($ftp_server);
echo "FTP Login: ".$conn_id."\n";
$login_result = ftp_login($conn_id, $ftp_user, $ftp_password);
echo "FTP Autentifikation: ".$login_result."\n";
if (($conn_id) and ($login_result) and ($localbackup==false)) {
    ftp_chdir($conn_id,$ftp_backupdirectory.inc_number);
    echo "If OLD Backup Files Exists, Delete it"."\n";
    $delete = ftp_delete($conn_id,"backup.tar");
    echo "Delete: ".$delete."\n";
    echo "Uploading FTP Files"."\n";
    $upload = ftp_put($conn_id,"backup.tar","backup.".inc_number.".tar",FTP_BINARY);
    echo "Upload: $upload"."\n";
    unlink("$dir/backup.$inc.tgz");
    echo "Removing Local Backup"."\n";
    ftp_close($conn_id);
}else{
    echo "Leaving Local Files"."\n";
}
echo "----------------------------------------------------------------------"."\n";
echo "Backup Finished at: ".date("r")."\n";
echo "Backup Time: ".date("H:i:s",$time-time())."\n";
echo "----------------------------------------------------------------------"."\n";
?>

und die Datei cron.php:
PHP:
#!/usr/bin/php5 -q
<?php
    #wpWDG Backup Solution Script
    ob_start();
    include_once("./shell.php");
    $data = ob_get_flush();
    $a = fopen("/root/backup/bkp.log","a+");
    fwrite($a, $data);
    fclose($a);
?>

Cron um es als Cront Tab laufen zu lassen, ohne shell Ausgaben

Was ist noch geplant:
einen Daemon als worker den man via telnet oder so erreichen kann.
Dort soll der Backuptask und mehr beendet werden können, oder bestimmte configs sollen auch angepasst werden können
^^
 
Last edited by a moderator:
für ispCP umschreiben ?!

Hast du schon was von ispCP Omega gehört ?
Das ist der nachfolger von VHCS - wäre vielleicht mal interessant ob du
es auch dafür umschreiben könntest/willst.

VHCS scheint in meinen Augen eh tot zu sein, und wenn das Updatescript von 2.4.7.1 auf ispCP Omega fertig ist war es dann wohl mit VHCS.

Hier mal der Link --> The home of IspCP Omega a VHCS fork

Grüße BeNe
 
man muß nur die paramterer am anfang ändern.
man kann es genau genommen für jedes panel verwenden.
einziges problem:
Man muß die Kunden und die Datenbank händisch eintragen.
Warum?
Ganz einfach, ich will nicht alle Accounts sichern, sondern nur ausgewählte.
 
MOD: Fullquote entfernt.

Du hast recht, jedoch ist isp-cp noch nicht für den Produktiven einsatz zu verwenden.
Da VHCS2 bisher bei mir noch nicht eingeknickt ist, bleib ich noch dabei.
ISP-CP schaut zwar schön aus, jedoch kamm ich nach der ersten Anmeldung schon nicht mehr rein.
Hab dann das herum doktoren aufgegeben und auf dem Server Plesk Installiert, welches seit dem Stabil und mit minimalen RAM verbrauch glänzt (was man von ISP-CP nicht sagen konnte)


^^

so viel
chris
 
Last edited by a moderator:
Back
Top