function backup($ID){ $date_start=time(); $sock=new sockets(); $q=new mysql(); $unix=new unix(); $users=new usersMenus(); $GLOBALS["RESOURCE_MOUNTED"]=true; $sql="SELECT * FROM backup_schedules WHERE ID='$ID'"; if($GLOBALS["VERBOSE"]){backup_events($ID,"initialization","$sql");} $mount_path="/opt/artica/mounts/backup/$ID"; $servername=$users->fqdn; $servername=str_replace('.(none)',"",$servername); $servername=str_replace(')',"",$servername); $servername=str_replace('(',"",$servername); $ExecBackupDeadAfterH=$sock->GET_INFO("ExecBackupDeadAfterH"); if(!is_numeric($ExecBackupDeadAfterH)){$ExecBackupDeadAfterH=2;} if($ExecBackupDeadAfterH<2){$ExecBackupDeadAfterH=2;} $ExecBackupDeadAfterH=$ExecBackupDeadAfterH*60; $ligne=@mysql_fetch_array($q->QUERY_SQL($sql,"artica_backup")); if(!$q->ok){ send_email_events("Backup Task $ID:: Mysql database error !","Aborting backup\n$q->mysql_error","backup"); backup_events($ID,"initialization","ERROR, Mysql database error\n$q->mysql_error"); return false; } if(!$GLOBALS["ONNLY_MOUNT"]){ $pid=$ligne["pid"]; if($unix->process_exists($pid)){ $time=$unix->PROCCESS_TIME_MIN($pid); if($time>$ExecBackupDeadAfterH){ send_email_events("Backup Task $ID:: Already instance $pid running since {$time}Mn","The old process was killed and a new backup task will be performed\nstatus:\n\n$unix->proc_status","backup"); }else{ send_email_events("Backup Task $ID:: Already instance $pid running since {$time}Mn","Aborting backup\n$unix->proc_status","backup"); backup_events($ID,"initialization","ERROR, Already instance $pid running since {$time}Mn",$unix->proc_status); return false; } } } $sql="UPDATE backup_schedules set pid='".getmypid()."' WHERE ID='$ID'"; $q->QUERY_SQL($sql,"artica_backup"); $ressources=unserialize(base64_decode($ligne["datasbackup"])); if(count($ressources)==0){ backup_events($ID,"initialization","ERROR,No source specified"); send_email_events("Backup Task $ID:: No source specified","Aborting backup","backup"); return false; } if($ressources["OPTIONS"]["STOP_IMAP"]==1){$GLOBALS["NO_STOP_CYRUS"]=" --no-cyrus-stop";} $backup=new backup_protocols(); $resource_type=$ligne["resource_type"]; $pattern=$ligne["pattern"]; $first_ressource=$backup->extractFirsRessource($ligne["pattern"]); $container=$ligne["container"]; backup_events($ID,"initialization","resource: $resource_type -> $first_ressource"); if($resource_type==null){ backup_events($ID,"initialization","ERROR,No resource specified"); send_email_events("Backup Task $ID:: No resource specified !","Aborting backup","backup"); return false; } if($resource_type=="smb"){ $mounted_path_sep="/"; if(!mount_smb($pattern,$ID,true)){ backup_events($ID,"initialization","ERROR,$first_ressource unable to mount"); send_email_events("Backup Task $ID:: resource: $first_ressource unable to mount","Aborting backup","backup"); return false; } } if($resource_type=="usb"){ $mounted_path_sep="/"; if(!mount_usb($pattern,$ID,true)){ backup_events($ID,"initialization","ERROR,$first_ressource unable to mount"); send_email_events("Backup Task $ID:: resource: $first_ressource unable to mount","Aborting backup","backup"); return false; } } if($resource_type=="rsync"){ $mounted_path_sep=null; $mount_path=null; $GLOBALS["RESOURCE_MOUNTED"]=false; $GLOBALS["USE_RSYNC"]=true; $GLOBALS["NO_UMOUNT"]=true; if(!mount_rsync($pattern,$ID,true)){ backup_events($ID,"initialization","ERROR,$first_ressource unable to connect"); send_email_events("Backup Task $ID:: resource: $first_ressource unable to connect","Aborting backup","backup"); return false; }else{ backup_events($ID,"initialization","INFO,$first_ressource connect success"); } } if($resource_type=="automount"){ $mounted_path_sep="/"; $mount_path=$first_ressource; $GLOBALS["RESOURCE_MOUNTED"]=false; $GLOBALS["USE_RSYNC"]=true; $GLOBALS["NO_UMOUNT"]=true; $GLOBALS["MOUNTED_PATH_FINAL"]=$first_ressource; CheckCommandLineCopy(); if(!mount_automount($pattern,$ID,true)){ backup_events($ID,"initialization","ERROR,$first_ressource unable to connect"); send_email_events("Backup Task $ID:: resource: $first_ressource unable to connect","Aborting backup","backup"); return false; } backup_events($ID,"initialization","INFO,$first_ressource connect success"); } if($resource_type=="local"){ $mounted_path_sep="/"; $mount_path=$first_ressource; $GLOBALS["RESOURCE_MOUNTED"]=false; $GLOBALS["USE_RSYNC"]=true; $GLOBALS["NO_UMOUNT"]=true; $GLOBALS["MOUNTED_PATH_FINAL"]=$first_ressource; CheckCommandLineCopy(); if(!is_dir($first_ressource)){ backup_events($ID,"initialization","ERROR,$first_ressource no such directory"); send_email_events("Backup Task $ID:: resource: $first_ressource no such directory","Aborting backup","backup"); return false; } backup_events($ID,"initialization","INFO,$first_ressource success"); } if($GLOBALS["ONLY_TESTS"]){ if($GLOBALS["RESOURCE_MOUNTED"]){ writelogs(date('m-d H:i:s')." "."[TASK $ID]:umount $first_ressource",__FUNCTION__,__FILE__,__LINE__); exec("umount -l $mount_path"); } writelogs(date('m-d H:i:s')." "."[TASK $ID]: terminated...",__FUNCTION__,__FILE__,__LINE__); return; } if($GLOBALS["ONNLY_MOUNT"]){return $mount_path;} if($container=="daily"){ backup_events($ID,"initialization","INFO, Daily container"); $mount_path_final=$mount_path.$mounted_path_sep."backup.".date('Y-m-d')."/$servername"; }else{ backup_events($ID,"initialization","INFO, Weekly container"); $mount_path_final=$mount_path.$mounted_path_sep."backup.".date('Y-W')."/$servername"; } if($GLOBALS["DEBUG"]){ $cmd_verb=" --verbose"; writelogs(date('m-d H:i:s')." "."[TASK $ID]: Verbose mode detected",__FUNCTION__,__FILE__,__LINE__); } @mkdir($mount_path_final,644,true); if(!$GLOBALS["NO_STANDARD_BACKUP"]){ $GLOBALS["MOUNTED_PATH_FINAL"]=$mount_path_final; while (list ($num, $WhatToBackup) = each ($ressources) ){ if(is_array($WhatToBackup)){$WhatToBackup_ar=implode(",",$WhatToBackup);} backup_events($ID,"initialization","INFO, WhatToBackup ($WhatToBackup) $WhatToBackup_ar"); if($WhatToBackup=="all"){ backup_events($ID,"initialization","INFO, Backup starting Running macro all cyrus, mysql, LDAP, Artica..."); send_email_events("Backup Task $ID:: Backup starting Running macro all ","Backup is running","backup"); if($users->cyrus_imapd_installed){ backup_events($ID,"initialization","INFO, cyrus-imapd mailboxes processing"); backup_cyrus($ID); } backup_events($ID,"initialization","INFO, LDAP Database processing"); backup_ldap($ID); backup_events($ID,"initialization","INFO, Mysql Database processing"); backup_mysql($ID); backup_events($ID,"initialization","INFO, Artica settings processing"); backup_artica($ID); if($users->ZARAFA_INSTALLED){if($sock->GET_INFO("ZarafaStoreOutside")==1){backup_events($ID,"initialization","INFO, Zarafa external attachments processing...");backup_ZarafaOutside($ID);}} backup_events($ID,"initialization","continue to next process"); continue; } } }else{ backup_events($ID,"initialization","INFO, Skipping standard macros"); } $sql="SELECT * FROM backup_folders WHERE taskid=$ID"; $results=$q->QUERY_SQL($sql,"artica_backup"); if(!$q->ok){ backup_events($ID,"personal","ERROR, mysql $q->mysql_error"); return; } while($ligne=@mysql_fetch_array($results,MYSQL_ASSOC)){ $dd1=time(); if($ligne["recursive"]==1){$recursive=" --recursive";}else{$recursive=null;} $path=trim(base64_decode($ligne["path"])); if(!is_dir($path)){ backup_events($ID,"personal","ERROR, [$path] no such file or directory"); continue; } backup_events($ID,"personal","INFO, Backup starting for $path"); send_email_events("Backup Task $ID:: Backup starting $path","Backup is running for path $path","backup"); backup_mkdir($path); $results=backup_copy($path,$path,$ID); $calculate=distanceOfTimeInWords($dd1,time()); backup_events($ID,"personal","INFO, Backup finish for $path\n$results $calculate"); } if(!$GLOBALS["NO_UMOUNT"]){ backup_events($ID,"initialization","INFO, umount $mount_path_final"); writelogs(date('m-d H:i:s')." "."[TASK $ID]:umount $mount_path_final",__FUNCTION__,__FILE__,__LINE__); exec("umount -l $mount_path_final"); } $date_end=time(); $calculate=distanceOfTimeInWords($date_start,$date_end); backup_events($ID,"TIME","INFO, Time: $calculate ($mount_path_final)"); backup_events($ID,"initialization","INFO, Backup task terminated"); send_email_events("Backup Task $ID:: Backup stopping","Backup is stopped","backup"); shell_exec(LOCATE_PHP5_BIN2()." ".dirname(__FILE__)."/exec.cleanfiles.php"); }
function backup($ID) { $date_start = time(); $sock = new sockets(); $q = new mysql(); $unix = new unix(); $users = new usersMenus(); $GLOBALS["RESOURCE_MOUNTED"] = true; $sql = "SELECT * FROM backup_schedules WHERE ID='{$ID}'"; if ($GLOBALS["VERBOSE"]) { backup_events($ID, "initialization", "{$sql}", __LINE__); } $mount_path = "/opt/artica/mounts/backup/{$ID}"; if (!$q->TABLE_EXISTS("backup_storages", "artica_backup", true)) { $q->BuildTables(); if (!$q->TABLE_EXISTS("backup_storages", "artica_backup", true)) { backup_events($ID, "initialization", "ERROR, backup_storages, no such table", __LINE__); return; } } $servername = $users->fqdn; $servername = str_replace('.(none)', "", $servername); $servername = str_replace(')', "", $servername); $servername = str_replace('(', "", $servername); $GLOBALS["MYSERVERNAME"] = $servername; $ExecBackupDeadAfterH = $sock->GET_INFO("ExecBackupDeadAfterH"); if (!is_numeric($ExecBackupDeadAfterH)) { $ExecBackupDeadAfterH = 2; } if ($ExecBackupDeadAfterH < 2) { $ExecBackupDeadAfterH = 2; } $ExecBackupDeadAfterH = $ExecBackupDeadAfterH * 60; $ligne = @mysql_fetch_array($q->QUERY_SQL($sql, "artica_backup")); if (!$q->ok) { send_email_events("Backup Task {$ID}:: Mysql database error !", "Aborting backup\n{$q->mysql_error}", "backup"); backup_events($ID, "initialization", "ERROR, Mysql database error\n{$q->mysql_error}", __LINE__); return false; } if (!$GLOBALS["ONNLY_MOUNT"]) { $pid = $ligne["pid"]; if ($unix->process_exists($pid)) { $time = $unix->PROCCESS_TIME_MIN($pid); if ($time > $ExecBackupDeadAfterH) { send_email_events("Backup Task {$ID}:: Already instance {$pid} running since {$time}Mn", "The old process was killed and a new backup task will be performed\nstatus:\n\n{$unix->proc_status}", "backup"); } else { send_email_events("Backup Task {$ID}:: Already instance {$pid} running since {$time}Mn", "Aborting backup\n{$unix->proc_status}", "backup"); backup_events($ID, "initialization", "ERROR, Already instance {$pid} running since {$time}Mn", $unix->proc_status); return false; } } } $sql = "UPDATE backup_schedules set pid='" . getmypid() . "' WHERE ID='{$ID}'"; $q->QUERY_SQL($sql, "artica_backup"); $ressources = unserialize(base64_decode($ligne["datasbackup"])); if (count($ressources) == 0) { backup_events($ID, "initialization", "ERROR,No source specified"); send_email_events("Backup Task {$ID}:: No source specified", "Aborting backup", "backup"); return false; } if ($ressources["OPTIONS"]["STOP_IMAP"] == 1) { $GLOBALS["NO_STOP_CYRUS"] = " --no-cyrus-stop"; } $backup = new backup_protocols(); $resource_type = $ligne["resource_type"]; $pattern = $ligne["pattern"]; $first_ressource = $backup->extractFirsRessource($ligne["pattern"]); $container = $ligne["container"]; backup_events($ID, "initialization", "resource: {$resource_type} -> {$first_ressource}", __LINE__); if ($resource_type == null) { backup_events($ID, "initialization", "ERROR,No resource specified"); send_email_events("Backup Task {$ID}:: No resource specified !", "Aborting backup", "backup"); return false; } if ($resource_type == "smb") { $GLOBALS["CAN_CLEAN_CONTAINERS"] = true; $mounted_path_sep = "/"; if (!mount_smb($pattern, $ID, true)) { backup_events($ID, "initialization", "ERROR,{$first_ressource} unable to mount mount_smb()", __LINE__); send_email_events("Backup Task {$ID}:: resource: {$first_ressource} unable to mount", "Aborting backup", "backup"); return false; } $GLOBALS["PCOPY"] = true; } if ($resource_type == "ssh") { $GLOBALS["CAN_CLEAN_CONTAINERS"] = true; $mounted_path_sep = "/"; if (!mount_ssh($pattern, $ID, true)) { backup_events($ID, "initialization", "ERROR,{$first_ressource} unable to mount mount_ssh()", __LINE__); send_email_events("Backup Task {$ID}:: resource: {$first_ressource} unable to mount to remote ssh service", "Aborting backup", "backup"); return false; } $GLOBALS["PCOPY"] = true; } if ($resource_type == "usb") { $GLOBALS["CAN_CLEAN_CONTAINERS"] = true; $mounted_path_sep = "/"; if (!mount_usb($pattern, $ID, true)) { backup_events($ID, "initialization", "ERROR,{$first_ressource} unable to mount mount_usb()", __LINE__); send_email_events("Backup Task {$ID}:: resource: {$first_ressource} unable to mount", "Aborting backup", "backup"); return false; } backup_events($ID, "initialization", "INFO, using external device trough USB", __LINE__); $GLOBALS["PCOPY"] = true; } if ($resource_type == "rsync") { $mounted_path_sep = null; $mount_path = null; $GLOBALS["RESOURCE_MOUNTED"] = false; $GLOBALS["USE_RSYNC"] = true; $GLOBALS["NO_UMOUNT"] = true; $GLOBALS["CAN_CLEAN_CONTAINERS"] = false; if (!mount_rsync($pattern, $ID, true)) { backup_events($ID, "initialization", "ERROR,{$first_ressource} unable to connect"); send_email_events("Backup Task {$ID}:: resource: {$first_ressource} unable to connect", "Aborting backup", "backup"); return false; } else { backup_events($ID, "initialization", "INFO,{$first_ressource} connect success"); } } if ($resource_type == "automount") { $mounted_path_sep = "/"; $mount_path = $first_ressource; $GLOBALS["RESOURCE_MOUNTED"] = false; $GLOBALS["USE_RSYNC"] = true; $GLOBALS["NO_UMOUNT"] = true; $GLOBALS["CAN_CLEAN_CONTAINERS"] = true; $GLOBALS["MOUNTED_PATH_FINAL"] = $first_ressource; CheckCommandLineCopy(); if (!mount_automount($pattern, $ID, true)) { backup_events($ID, "initialization", "ERROR,{$first_ressource} unable to connect"); send_email_events("Backup Task {$ID}:: resource: {$first_ressource} unable to connect", "Aborting backup", "backup"); return false; } backup_events($ID, "initialization", "INFO,{$first_ressource} connect success"); $GLOBALS["PCOPY"] = true; } if ($resource_type == "local") { $mounted_path_sep = "/"; $mount_path = $first_ressource; $GLOBALS["RESOURCE_MOUNTED"] = false; $GLOBALS["CAN_CLEAN_CONTAINERS"] = true; $GLOBALS["USE_RSYNC"] = true; $GLOBALS["NO_UMOUNT"] = true; $GLOBALS["MOUNTED_PATH_FINAL"] = $first_ressource; CheckCommandLineCopy(); if (!is_dir($first_ressource)) { backup_events($ID, "initialization", "{$first_ressource} directory doesn't exsits, create it..", __LINE__); @mkdir($first_ressource, 0755, true); } if (!is_dir($first_ressource)) { backup_events($ID, "initialization", "{$first_ressource} no such directory permission denied", __LINE__); send_email_events("Backup Task {$ID}:: resource: {$first_ressource} no such directory", "Aborting backup", "backup"); return false; } backup_events($ID, "initialization", "INFO,{$first_ressource} success"); $GLOBALS["PCOPY"] = true; } if ($GLOBALS["ONLY_TESTS"]) { if ($GLOBALS["RESOURCE_MOUNTED"]) { writelogs(date('m-d H:i:s') . " " . "[TASK {$ID}]:umount {$mount_path}", __FUNCTION__, __FILE__, __LINE__); exec("umount -l {$mount_path}"); } writelogs(date('m-d H:i:s') . " " . "[TASK {$ID}]: terminated...", __FUNCTION__, __FILE__, __LINE__); return; } if ($GLOBALS["ONNLY_MOUNT"]) { return $mount_path; } if ($container == "daily") { backup_events($ID, "initialization", "INFO, Daily container", __LINE__); $DirectoryContainer = "backup." . date('Y-m-d') . "/{$servername}"; $GLOBAL["BACKUP_MEMORY_SQL"]["CONTAINER"] = $DirectoryContainer; $mount_path_final = $mount_path . $mounted_path_sep . $DirectoryContainer; } else { backup_events($ID, "initialization", "INFO, Weekly container", __LINE__); $DirectoryContainer = "backup." . date('Y-W') . "/{$servername}"; $GLOBAL["BACKUP_MEMORY_SQL"]["CONTAINER"] = $DirectoryContainer; $mount_path_final = $mount_path . $mounted_path_sep . $DirectoryContainer; } if ($GLOBALS["DEBUG"]) { $cmd_verb = " --verbose"; writelogs(date('m-d H:i:s') . " " . "[TASK {$ID}]: Verbose mode detected", __FUNCTION__, __FILE__, __LINE__); } @mkdir($mount_path_final, 0755, true); if (!$GLOBALS["NO_STANDARD_BACKUP"]) { $GLOBALS["MOUNTED_PATH_FINAL"] = $mount_path_final; $WhatToBackup_ar = null; $BACKUP_WWW_ALREADY_DONE = array(); $BACKUP_WEBGET_ALREADY_DONE = array(); $BACKUP_INSTANCES_ALREADY_DONE = array(); while (list($num, $WhatToBackup) = each($ressources)) { if (is_array($WhatToBackup)) { $WhatToBackup_ar = implode(",", $WhatToBackup); backup_events($ID, "initialization", "INFO, WhatToBackup Array = {$WhatToBackup_ar}", __LINE__); continue; } if ($WhatToBackup == "all") { backup_events($ID, "initialization", "INFO, Backup starting Running macro all cyrus, mysql, LDAP, Artica...", __LINE__); send_email_events("Backup Task {$ID}:: Backup starting Running macro all ", "Backup is running", "backup"); if ($users->cyrus_imapd_installed) { backup_events($ID, "initialization", "INFO, cyrus-imapd mailboxes processing"); backup_cyrus($ID); } backup_events($ID, "initialization", "INFO, LDAP Database processing", __LINE__); backup_ldap($ID); backup_events($ID, "initialization", "INFO, MySQL Database processing", __LINE__); backup_mysql($ID, 0); backup_events($ID, "initialization", "INFO, Restarting MySQL service...", __LINE__); mysql_admin_mysql(0, "Restarting mysql service.", null, __FILE__, __LINE__); shell_exec("/etc/init.d/mysql restart"); backup_events($ID, "initialization", "INFO, Artica settings processing", __LINE__); backup_artica($ID); if ($users->ZARAFA_INSTALLED) { if ($sock->GET_INFO("ZarafaStoreOutside") == 1) { backup_events($ID, "initialization", "INFO, Zarafa external attachments processing..."); backup_ZarafaOutside($ID); } } backup_events($ID, "initialization", "continue to next process", __LINE__); continue; } if (preg_match("#MYSQLINSTANCE:([0-9]+)#", $WhatToBackup, $re)) { $instance_id = $re[1]; backup_events($ID, "initialization", "INFO, Backup starting backup MySQL instance Number:{$instance_id}", __LINE__); if ($instance_id > 0) { if (!isset($BACKUP_INSTANCES_ALREADY_DONE[$instance_id])) { backup_mysql($ID, $instance_id); $BACKUP_INSTANCES_ALREADY_DONE[$instance_id] = true; } } continue; } if (preg_match("#FREEWEB:(.+)#", $WhatToBackup, $re)) { $sitename = $re[1]; backup_events($ID, "initialization", "INFO, Backup starting backup Website {$sitename}", __LINE__); if (!isset($BACKUP_WWW_ALREADY_DONE[$sitename])) { backup_freewebs($ID, $sitename); $BACKUP_INSTANCES_ALREADY_DONE[$sitename] = true; } continue; } if (preg_match("#WEBGET:(.+)#", $WhatToBackup, $re)) { if (!isset($BACKUP_WEBGET_ALREADY_DONE[$re[1]])) { $arr = unserialize(base64_decode($re[1])); if (!is_array($arr)) { backup_events($ID, "initialization", "ERROR, WEBGET `{$re[1]}` is not an array...", __LINE__); continue; } backup_events($ID, "initialization", "INFO, Backup remote Artica FreeWebs Website {$arr["RemoteArticaSite"]} from source {$arr["RemoteArticaServer"]}", __LINE__); backup_webget($ID, $arr); } continue; } backup_events($ID, "initialization", "INFO, `{$WhatToBackup}` could not understood", __LINE__); } } $sql = "SELECT * FROM backup_folders WHERE taskid={$ID}"; $results = $q->QUERY_SQL($sql, "artica_backup"); if (!$q->ok) { if (strpos($q->mysql_error, "gone away")) { backup_events($ID, "personal", "ERROR, mysql {$q->mysql_error} Restarting MySQL server (Patch p.20130807)", __LINE__); mysql_admin_mysql(0, "Restarting mysql service.", $q->mysql_error, __FILE__, __LINE__); shell_exec("/etc/init.d/mysql restart"); $q = new mysql(); $results = $q->QUERY_SQL($sql, "artica_backup"); } } if (!$q->ok) { backup_events($ID, "personal", "ERROR, mysql {$q->mysql_error}", __LINE__); return; } while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) { $dd1 = time(); if ($ligne["recursive"] == 1) { $recursive = " --recursive"; } else { $recursive = null; } $path = trim(base64_decode($ligne["path"])); if (!is_dir($path)) { backup_events($ID, "personal", "ERROR, [{$path}] no such file or directory", __LINE__); continue; } backup_events($ID, "personal", "INFO, Backup starting for {$path}", __LINE__); send_email_events("Backup Task {$ID}:: Backup starting {$path}", "Backup is running for path {$path}", "backup"); backup_mkdir($path); $results = backup_copy($path, $path, $ID); $calculate = distanceOfTimeInWords($dd1, time()); backup_events($ID, "personal", "INFO, Backup finish for {$path}\n{$results} {$calculate}", __LINE__); } writelogs(date('m-d H:i:s') . " " . "[TASK {$ID}]: Calculate directory size on {$mount_path_final}", __FUNCTION__, __FILE__, __LINE__); $du = $unix->find_program("du"); $dut1 = time(); $nice = $unix->EXEC_NICE(); $cmd = "{$nice}{$du} -s {$mount_path_final}"; exec($cmd, $du_results); $calculate = distanceOfTimeInWords($dut1, time()); $BackupSize = 0; if (preg_match("#^([0-9]+)\\s+#", @implode("", $du_results), $re)) { $BackupSize = $re[1]; backup_events($ID, "initialization", "INFO, backup size {$BackupSize} bytes time:{$calculate}", __LINE__); } if ($GLOBALS["CAN_CLEAN_CONTAINERS"]) { backup_events($ID, "initialization", "INFO, cleaning containers....", __LINE__); CleanContainers($ID, $mount_path_final); } else { backup_events($ID, "initialization", "INFO, cannot clean containers, check protocols....", __LINE__); } $GLOBAL["BACKUP_MEMORY_SQL"]["mount_path_final"] = $mount_path_final; $zmd5 = md5("{$GLOBAL["BACKUP_MEMORY_SQL"]["CONTAINER"]}{$GLOBALS["MYSERVERNAME"]}"); $cnx_params = addslashes(base64_encode(serialize($GLOBAL["BACKUP_MEMORY_SQL"]))); $sql = "INSERT IGNORE INTO backup_storages (`taskid`,`size`,`cnx_params`,`zmd5`) VALUES('{$ID}','{$BackupSize}','{$cnx_params}','{$zmd5}')"; $q->QUERY_SQL($sql, "artica_backup"); $sql = "UPDATE backup_storages SET `size`='{$BackupSize}' WHERE `zmd5`='{$zmd5}'"; $q->QUERY_SQL($sql, "artica_backup"); if (!$q->ok) { backup_events($ID, "initialization", "ERROR, {$q->mysql_error}", __LINE__); } if (!$GLOBALS["NO_UMOUNT"]) { writelogs(date('m-d H:i:s') . " " . "[TASK {$ID}]:umount {$mount_path_final}", __FUNCTION__, __FILE__, __LINE__); if (preg_match("#^\\/opt\\/artica\\/mounts\\/backup\\/[0-9]+(.+)#", $mount_path_final, $re)) { $mount_path_final = str_replace($re[1], "", $mount_path_final); writelogs(date('m-d H:i:s') . " " . "[TASK {$ID}]:translated to {$mount_path_final}", __FUNCTION__, __FILE__, __LINE__); } backup_events($ID, "initialization", "INFO, umount {$mount_path_final}", __LINE__); writelogs(date('m-d H:i:s') . " " . "[TASK {$ID}]:umount {$mount_path_final}", __FUNCTION__, __FILE__, __LINE__); exec("umount -l {$mount_path_final} 2>&1", $resultsUmount); if (count($resultsUmount) > 0) { writelogs(date('m-d H:i:s') . " " . "[TASK {$ID}]:umount : ----- \n" . @implode("\n", $resultsUmount) . "\n", __FUNCTION__, __FILE__, __LINE__); } } $date_end = time(); $calculate = distanceOfTimeInWords($date_start, $date_end); backup_events($ID, "TIME", "INFO, Time: {$calculate} ({$mount_path_final})", __LINE__); backup_events($ID, "initialization", "INFO, Backup task terminated", __LINE__); send_email_events("Backup Task {$ID}:: Backup stopping", "Backup is stopped", "backup"); shell_exec(LOCATE_PHP5_BIN2() . " " . dirname(__FILE__) . "/exec.cleanfiles.php"); }
function backup($ID) { $date_start = time(); $GLOBALS["RESOURCE_MOUNTED"] = true; $sql = "SELECT * FROM backup_schedules WHERE ID='{$ID}'"; $mount_path = "/opt/artica/mounts/backup/{$ID}"; $q = new mysql(); $unix = new unix(); $users = new usersMenus(); $servername = $users->fqdn; $servername = str_replace('.(none)', "", $servername); $servername = str_replace(')', "", $servername); $servername = str_replace('(', "", $servername); $ligne = @mysql_fetch_array($q->QUERY_SQL($sql, "artica_backup")); if (!$q->ok) { send_email_events("Backup Task {$ID}:: Mysql database error !", "Aborting backup", "backup"); backup_events($ID, "initialization", "ERROR, Mysql database error"); return false; } if (!$GLOBALS["ONNLY_MOUNT"]) { $pid = $ligne["pid"]; if ($unix->process_exists($pid)) { send_email_events("Backup Task {$ID}:: Already instance {$pid} running", "Aborting backup", "backup"); backup_events($ID, "initialization", "ERROR, Already instance {$pid} running"); return false; } } $sql = "UPDATE backup_schedules set pid='" . getmypid() . "' WHERE ID='{$ID}'"; $q->QUERY_SQL($sql, "artica_backup"); $ressources = unserialize(base64_decode($ligne["datasbackup"])); if (count($ressources) == 0) { backup_events($ID, "initialization", "ERROR,No source specified"); send_email_events("Backup Task {$ID}:: No source specified", "Aborting backup", "backup"); return false; } if ($ressources["OPTIONS"]["STOP_IMAP"] == 1) { $GLOBALS["NO_STOP_CYRUS"] = " --no-cyrus-stop"; } $backup = new backup_protocols(); $resource_type = $ligne["resource_type"]; $pattern = $ligne["pattern"]; $first_ressource = $backup->extractFirsRessource($ligne["pattern"]); $container = $ligne["container"]; backup_events($ID, "initialization", "resource: {$resource_type} -> {$first_ressource}"); if ($resource_type == null) { backup_events($ID, "initialization", "ERROR,No resource specified"); send_email_events("Backup Task {$ID}:: No resource specified !", "Aborting backup", "backup"); return false; } if ($resource_type == "smb") { $mounted_path_sep = "/"; if (!mount_smb($pattern, $ID, true)) { backup_events($ID, "initialization", "ERROR,{$first_ressource} unable to mount"); send_email_events("Backup Task {$ID}:: resource: {$first_ressource} unable to mount", "Aborting backup", "backup"); return false; } } if ($resource_type == "usb") { $mounted_path_sep = "/"; if (!mount_usb($pattern, $ID, true)) { backup_events($ID, "initialization", "ERROR,{$first_ressource} unable to mount"); send_email_events("Backup Task {$ID}:: resource: {$first_ressource} unable to mount", "Aborting backup", "backup"); return false; } } if ($resource_type == "rsync") { $mounted_path_sep = null; $mount_path = null; $GLOBALS["RESOURCE_MOUNTED"] = false; $GLOBALS["USE_RSYNC"] = true; $GLOBALS["NO_UMOUNT"] = true; if (!mount_rsync($pattern, $ID, true)) { backup_events($ID, "initialization", "ERROR,{$first_ressource} unable to connect"); send_email_events("Backup Task {$ID}:: resource: {$first_ressource} unable to connect", "Aborting backup", "backup"); return false; } else { backup_events($ID, "initialization", "INFO,{$first_ressource} connect success"); } } if ($GLOBALS["ONLY_TESTS"]) { writelogs(date('m-d H:i:s') . " " . "[TASK {$ID}]:umount {$first_ressource}", __FUNCTION__, __FILE__, __LINE__); if ($GLOBALS["RESOURCE_MOUNTED"]) { exec("umount -l {$mount_path}"); } return; } if ($GLOBALS["ONNLY_MOUNT"]) { return $mount_path; } if ($container == "daily") { backup_events($ID, "initialization", "INFO, Daily container"); $mount_path_final = $mount_path . $mounted_path_sep . "backup." . date('Y-m-d') . "/{$servername}"; } else { backup_events($ID, "initialization", "INFO, Weekly container"); $mount_path_final = $mount_path . $mounted_path_sep . "backup." . date('Y-W') . "/{$servername}"; } if ($GLOBALS["DEBUG"]) { $cmd_verb = " --verbose"; writelogs(date('m-d H:i:s') . " " . "[TASK {$ID}]: Verbose mode detected", __FUNCTION__, __FILE__, __LINE__); } if (!$GLOBALS["NO_STANDARD_BACKUP"]) { $GLOBALS["MOUNTED_PATH_FINAL"] = $mount_path_final; while (list($num, $WhatToBackup) = each($ressources)) { if (is_array($WhatToBackup)) { $WhatToBackup_ar = implode(",", $WhatToBackup); } backup_events($ID, "initialization", "INFO, WhatToBackup ({$WhatToBackup}) {$WhatToBackup_ar}"); if ($WhatToBackup == "all") { backup_events($ID, "initialization", "INFO, Backup starting Running macro all cyrus, mysql, LDAP, Artica..."); send_email_events("Backup Task {$ID}:: Backup starting Running macro all ", "Backup is running", "backup"); if ($users->cyrus_imapd_installed) { backup_events($ID, "initialization", "INFO, cyrus-imapd mailboxes processing"); backup_cyrus($ID); } backup_events($ID, "initialization", "INFO, LDAP Database processing"); backup_ldap($ID); backup_events($ID, "initialization", "INFO, Mysql Database processing"); backup_mysql($ID); backup_events($ID, "initialization", "INFO, Artica settings processing"); backup_artica($ID); backup_events($ID, "initialization", "continue to next process"); continue; } } } else { backup_events($ID, "initialization", "INFO, Skipping standard macros"); } $sql = "SELECT * FROM backup_folders WHERE taskid={$ID}"; $results = $q->QUERY_SQL($sql, "artica_backup"); if (!$q->ok) { backup_events($ID, "personal", "ERROR, mysql {$q->mysql_error}"); return; } while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) { if ($ligne["recursive"] == 1) { $recursive = " --recursive"; } else { $recursive = null; } $path = trim(base64_decode($ligne["path"])); if (!is_dir($path)) { backup_events($ID, "personal", "ERROR, [{$path}] no such file or directory"); continue; } backup_events($ID, "personal", "INFO, Backup starting for {$path}"); send_email_events("Backup Task {$ID}:: Backup starting {$path}", "Backup is running for path {$path}", "backup"); backup_mkdir($path); $results = backup_copy($path, $path, $ID); backup_events($ID, "personal", "INFO, Backup finish for {$path}\n{$results}"); } if (!$GLOBALS["NO_UMOUNT"]) { backup_events($ID, "initialization", "INFO, umount {$mount_path}"); writelogs(date('m-d H:i:s') . " " . "[TASK {$ID}]:umount {$mount_path}", __FUNCTION__, __FILE__, __LINE__); exec("umount -l {$mount_path}"); } $date_end = time(); $calculate = distanceOfTimeInWords($date_start, $date_end); backup_events($ID, "TIME", "INFO, Time: {$calculate} ({$source_path})"); backup_events($ID, "initialization", "INFO, Backup task terminated"); send_email_events("Backup Task {$ID}:: Backup stopping", "Backup is stopped", "backup"); shell_exec(LOCATE_PHP5_BIN2() . " " . dirname(__FILE__) . "/exec.cleanfiles.php"); }