function backup() { $sock = new sockets(); $unix = new unix(); $pidTime = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time"; $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid"; $pid = $unix->get_pid_from_file($pidfile); if ($unix->process_exists($pid, basename(__FILE__))) { $time = $unix->PROCCESS_TIME_MIN($pid); echo "Starting......: " . date("H:i:s") . " [INIT]: nginx Already Artica task running PID {$pid} since {$time}mn\n"; return; } $time = $unix->file_time_min($pidTime); if ($time < 60) { return; } @file_put_contents($pidfile, getmypid()); @unlink($pidTime); @file_put_contents($pidTime, getmypid()); $SargOutputDir = $sock->GET_INFO("SargOutputDir"); if ($SargOutputDir == null) { $SargOutputDir = "/var/www/html/squid-reports"; } $BackupSargUseNas = $sock->GET_INFO("BackupSargUseNas"); if (!is_numeric($BackupSargUseNas)) { $BackupSargUseNas = 0; } $nice = EXEC_NICE(); $mount = new mount("/var/log/sarg-exec.log"); if ($BackupSargUseNas == 0) { return; } $BackupSargNASIpaddr = $sock->GET_INFO("BackupSargNASIpaddr"); $BackupSargNASFolder = $sock->GET_INFO("BackupSargNASFolder"); $BackupSargNASUser = $sock->GET_INFO("BackupSargNASUser"); $BackupSargNASPassword = $sock->GET_INFO("BackupSargNASPassword"); $mountPoint = "/mnt/BackupSargUseNas"; if (!$mount->smb_mount($mountPoint, $BackupSargNASIpaddr, $BackupSargNASUser, $BackupSargNASPassword, $BackupSargNASFolder)) { sarg_admin_events("SARG: Unable to connect to NAS storage system: {$BackupSargNASUser}@{$BackupSargNASIpaddr}", __FUNCTION__, __FILE__, __LINE__, "sarg"); return; } $BackupDir = "{$mountPoint}/sarg"; @mkdir("{$BackupDir}", 0755); if (!is_dir($BackupDir)) { if ($GLOBALS["VERBOSE"]) { echo "Fatal {$BackupDir} permission denied\n"; } sarg_admin_events("Fatal {$BackupDir} permission denied", __FUNCTION__, __FILE__, __LINE__, "sarg"); $mount->umount($mountPoint); return false; } $t = time(); @file_put_contents("{$BackupDir}/{$t}", time()); if (!is_file("{$BackupDir}/{$t}")) { sarg_admin_events("Fatal {$BackupDir} permission denied", __FUNCTION__, __FILE__, __LINE__, "sarg"); $mount->umount($mountPoint); return false; } @unlink("{$BackupDir}/{$t}"); $cp = $unix->find_program("cp"); shell_exec(trim("{$nice} {$cp} -dpR {$SargOutputDir}/* {$BackupDir}/")); $mount->umount($mountPoint); sarg_admin_events("Copy to {$BackupSargNASIpaddr}/{$BackupSargNASFolder} done", __FUNCTION__, __FILE__, __LINE__, "sarg"); }
function BackupToNas($directory) { if (!is_dir($directory)) { return; } $syslog = new mysql_storelogs(); $sock = new sockets(); $users = new usersMenus(); $unix = new unix(); $myHostname = $unix->hostname_g(); $DirSuffix = basename($directory); $mount = new mount("/var/log/artica-postfix/logrotate.debug"); $BackupSquidLogsNASIpaddr = $sock->GET_INFO("BackupSquidLogsNASIpaddr"); $BackupSquidLogsNASFolder = $sock->GET_INFO("BackupSquidLogsNASFolder"); $BackupSquidLogsNASUser = $sock->GET_INFO("BackupSquidLogsNASUser"); $BackupSquidLogsNASPassword = $sock->GET_INFO("BackupSquidLogsNASPassword"); $BackupSquidLogsNASRetry = $sock->GET_INFO("BackupSquidLogsNASRetry"); if (!is_numeric($BackupSquidLogsNASRetry)) { $BackupSquidLogsNASRetry = 0; } $mount = new mount("/var/log/artica-postfix/logrotate.debug"); $BackupSquidLogsNASIpaddr = $sock->GET_INFO("BackupSquidLogsNASIpaddr"); $BackupSquidLogsNASFolder = $sock->GET_INFO("BackupSquidLogsNASFolder"); $BackupSquidLogsNASUser = $sock->GET_INFO("BackupSquidLogsNASUser"); $BackupSquidLogsNASPassword = $sock->GET_INFO("BackupSquidLogsNASPassword"); $BackupSquidLogsNASRetry = $sock->GET_INFO("BackupSquidLogsNASRetry"); if (!is_numeric($BackupSquidLogsNASRetry)) { $BackupSquidLogsNASRetry = 0; } $mv = $unix->find_program("mv"); if ($BackupSquidLogsNASIpaddr == null) { $this->events("Backup via NAS is disabled, skip", __FUNCTION__, __FILE__, __LINE__, "logrotate"); return false; } $mountPoint = "/mnt/BackupSquidLogsUseNas"; if (!$mount->smb_mount($mountPoint, $BackupSquidLogsNASIpaddr, $BackupSquidLogsNASUser, $BackupSquidLogsNASPassword, $BackupSquidLogsNASFolder)) { $syslog->events("Unable to connect to NAS storage system (1): {$BackupSquidLogsNASUser}@{$BackupSquidLogsNASIpaddr}", __FUNCTION__, __FILE__, __LINE__, "logrotate"); if ($BackupSquidLogsNASRetry == 0) { return; } sleep(3); $mount = new mount("/var/log/artica-postfix/logrotate.debug"); if (!$mount->smb_mount($mountPoint, $BackupSquidLogsNASIpaddr, $BackupSquidLogsNASUser, $BackupSquidLogsNASPassword, $BackupSquidLogsNASFolder)) { $syslog->events("Unable to connect to NAS storage system (1): {$BackupSquidLogsNASUser}@{$BackupSquidLogsNASIpaddr}", __FUNCTION__, __FILE__, __LINE__, "logrotate"); return; } } $syslog->events("Hostname={$myHostname} Suffix = {$DirSuffix} {$BackupSquidLogsNASIpaddr}/{$BackupSquidLogsNASFolder}", __FUNCTION__, __LINE__); $BackupMaxDaysDir = "{$mountPoint}/artica-backup-syslog"; @mkdir("{$BackupMaxDaysDir}", 0755, true); if (!is_dir($BackupMaxDaysDir)) { $syslog->events("Fatal {$BackupMaxDaysDir} permission denied", __FUNCTION__, __LINE__); if ($GLOBALS["VERBOSE"]) { echo "Fatal {$BackupMaxDaysDir} permission denied\n"; } squid_admin_mysql(0, "SYSLOG: FATAL {$BackupMaxDaysDir} permission denied", null, __FILE__, __LINE__); $mount->umount($mountPoint); return false; } $t = time(); @file_put_contents("{$BackupMaxDaysDir}/{$t}", time()); if (!is_file("{$BackupMaxDaysDir}/{$t}")) { $syslog->events("Fatal {$BackupMaxDaysDir} permission denied ({$BackupMaxDaysDir}/{$t}) test failed", __FUNCTION__, __LINE__); squid_admin_mysql(0, "SYSLOG: FATAL {$BackupMaxDaysDir} permission denied", null, __FILE__, __LINE__); $mount->umount($mountPoint); return false; } @unlink("{$BackupMaxDaysDir}/{$t}"); exec("{$mv} --force {$directory} --target-directory={$BackupMaxDaysDir}/ 2>&1", $results); while (list($index, $line) = each($results)) { $syslog->events("{$line}", __FUNCTION__, __LINE__); } analyze_destination_directory($BackupMaxDaysDir . "/proxy"); $mount->umount($mountPoint); return true; }
function restore() { $sock = new sockets(); $unix = new unix(); $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid"; if ($GLOBALS["VERBOSE"]) { echo "PID: {$pidfile}\n"; } $pid = $unix->get_pid_from_file($pidfile); if ($unix->process_exists($pid)) { $TTL = $unix->PROCESS_TTL($pid); if ($TTL < 240) { return; } $kill = $unix->find_program("kill"); unix_system_kill_force($pid); } @file_put_contents($pidfile, getmypid()); $hostname = $unix->hostname_g(); progress(10, "{mounting}"); if (!restore_TestNas()) { system_admin_events("Mounting NAS filesystem report false", __FUNCTION__, __FILE__, __LINE__); progress(100, "{failed}"); return; } $BackupArticaRestoreNASIpaddr = $sock->GET_INFO("BackupArticaRestoreNASIpaddr"); $BackupArticaRestoreNASFolder = $sock->GET_INFO("BackupArticaRestoreNASFolder"); $BackupArticaRestoreNASUser = $sock->GET_INFO("BackupArticaRestoreNASUser"); $BackupArticaRestoreNASPassword = $sock->GET_INFO("BackupArticaRestoreNASPassword"); $BackupArticaRestoreNASFolderSource = $sock->GET_INFO("BackupArticaRestoreNASFolderSource"); $BackupArticaRestoreNetwork = $sock->GET_INFO("BackupArticaRestoreNetwork"); $mountPoint = "/mnt/BackupArticaRestoreNAS"; $BackupArticaRestoreNASFolderSource = str_replace("\\", "/", $BackupArticaRestoreNASFolderSource); $sourceDir = "{$mountPoint}/{$BackupArticaRestoreNASFolderSource}"; $sourceDir = str_replace("//", "/", $sourceDir); if (!is_file("{$sourceDir}/BKVERSION.txt")) { progress(100, "{failed} BKVERSION.txt no such file"); $mount = new mount("/var/log/artica-postfix/backup.debug"); if ($mount->ismounted($mountPoint)) { $mount->umount($mountPoint); } return; } $time = trim(@file_get_contents("{$sourceDir}/BKVERSION.txt")); progress(15, "{backup} " . date("Y-m-d H:i:s")); progress(20, "{restoring_ldap_database}, {please_wait}..."); Restore_ldap($sourceDir); progress(40, "{restoring_artica_settings}, {please_wait}..."); restore_artica_settings($sourceDir); progress(50, "{restoring_artica_databases}, {please_wait}..."); restore_artica_backup($sourceDir); progress(60, "{restoring_artica_databases}, {please_wait}..."); restore_ocsweb($sourceDir); progress(80, "{restoring_artica_databases}, {please_wait}..."); restore_squidlogs($sourceDir); progress(82, "{restoring} PowerDNS, {please_wait}..."); restore_powerdns($sourceDir); progress(90, "{reconfigure_server}, {please_wait}..."); $squidbin = $unix->LOCATE_SQUID_BIN(); $php = $unix->LOCATE_PHP5_BIN(); if (is_file($squidbin)) { shell_exec("{$php} /usr/share/artica-postfix/exec.squid.php --build --force"); } progress(100, "{success}"); $mount = new mount("/var/log/artica-postfix/backup.debug"); if ($mount->ismounted($mountPoint)) { $mount->umount($mountPoint); } if ($BackupArticaRestoreNetwork == 1) { $unix->THREAD_COMMAND_SET("{$php} /usr/share/artica-postfix/exec.virtuals-ip.php --build"); } return; }
function tests_nas() { $sock = new sockets(); $BackupSquidStatsUseNas = $sock->GET_INFO("BackupSquidStatsUseNas"); $MySQLSyslogType = $sock->GET_INFO("MySQLSyslogType"); $EnableSyslogDB = $sock->GET_INFO("EnableSyslogDB"); if (!is_numeric($EnableSyslogDB)) { $EnableSyslogDB = 0; } if (!is_numeric($MySQLSyslogType)) { $MySQLSyslogType = 1; } if (!is_numeric($BackupSquidStatsUseNas)) { $BackupSquidStatsUseNas = 0; } $users = new usersMenus(); $mount = new mount("/var/log/artica-postfix/logrotate.debug"); if ($BackupSquidStatsUseNas == 0) { echo "Backup using NAS is not enabled\n"; return; } $BackupSquidStatsNASIpaddr = $sock->GET_INFO("BackupSquidStatsNASIpaddr"); $BackupSquidStatsNASFolder = $sock->GET_INFO("BackupSquidStatsNASFolder"); $BackupSquidStatsNASUser = $sock->GET_INFO("BackupSquidStatsNASUser"); $BackupSquidStatsNASPassword = $sock->GET_INFO("BackupSquidStatsNASPassword"); $BackupSquidStatsNASRetry = $sock->GET_INFO("BackupSquidStatsNASRetry"); if (!is_numeric($BackupSquidStatsNASRetry)) { $BackupSquidStatsNASRetry = 0; } $failed = "***********************\n** FAILED **\n***********************\n"; $success = "***********************\n******* SUCCESS *******\n***********************\n"; $mountPoint = "/mnt/BackupSquidStatsUseNas"; if (!$mount->smb_mount($mountPoint, $BackupSquidStatsNASIpaddr, $BackupSquidStatsNASUser, $BackupSquidStatsNASPassword, $BackupSquidStatsNASFolder)) { if ($BackupSquidStatsNASRetry == 1) { sleep(3); $mount = new mount("/var/log/artica-postfix/logrotate.debug"); if (!$mount->smb_mount($mountPoint, $BackupSquidStatsNASIpaddr, $BackupSquidStatsNASUser, $BackupSquidStatsNASPassword, $BackupSquidStatsNASFolder)) { echo "{$failed}\nUnable to connect to NAS storage system: {$BackupSquidStatsNASUser}@{$BackupSquidStatsNASIpaddr}\n"; echo @implode("\n", $GLOBALS["MOUNT_EVENTS"]); return; } } else { echo "{$failed}\nUnable to connect to NAS storage system: {$BackupSquidStatsNASUser}@{$BackupSquidStatsNASIpaddr}\n"; echo @implode("\n", $GLOBALS["MOUNT_EVENTS"]); return; } } $BackupMaxDaysDir = "{$mountPoint}/backup-statistics/{$users->hostname}"; @mkdir($BackupMaxDaysDir, 0755, true); if (!is_dir($BackupMaxDaysDir)) { echo "{$failed}{$BackupSquidStatsNASUser}@{$BackupSquidStatsNASIpaddr}/{$BackupSquidStatsNASFolder}/backup-statistics/{$users->hostname} permission denied.\n"; $mount->umount($mountPoint); return; } $t = time(); @file_put_contents("{$BackupMaxDaysDir}/{$t}", "#"); if (!is_file("{$BackupMaxDaysDir}/{$t}")) { echo "{$failed}{$BackupSquidStatsNASUser}@{$BackupSquidStatsNASIpaddr}/{$BackupSquidStatsNASFolder}/backup-statistics/{$users->hostname}/* permission denied.\n"; $mount->umount($mountPoint); return; } @unlink("{$BackupMaxDaysDir}/{$t}"); $mount->umount($mountPoint); echo "{$success}"; }
function exec_webdav() { $mount = new mount(); $unix = new unix(); $mount_point = $GLOBALS["MOUNT_POINT"]; @mkdir($mount_point, 0755, true); $server = $GLOBALS["CyrusBackupNas"]["WEBDAV_SERVER"]; $username = $GLOBALS["CyrusBackupNas"]["WEBDAV_USER"]; $password = $GLOBALS["CyrusBackupNas"]["WEBDAV_PASSWORD"]; $path = $GLOBALS["CyrusBackupNas"]["WEBDAV_DIR"]; if ($mount->ismounted($mount_point)) { $mount->umount($mount_point); } if ($GLOBALS["VERBOSE"]) { echo "davfs:WEBDAV_DIR...: {$path}\n"; } if ($GLOBALS["VERBOSE"]) { echo "davfs:WEBDAV_SERVER: {$server}\n"; } if (!$mount->davfs_mount($mount_point, $server, $username, $password, $path)) { if ($GLOBALS["VERBOSE"]) { echo $mount->events_compile() . "\n"; } cyrus_admin_mysql(0, "Unable to connect to {$server}", $mount->events_compile(), __FILE__, __LINE__); return; } $path = $mount->davfs_path($mount_point, $server, $username, $password, $path); if ($GLOBALS["VERBOSE"]) { echo "davfs_path: {$path}\n"; } $hostname = $unix->hostname_g(); $GLOBALS["DIRBYTES"] = date("YmdH"); $GLOBALS["MOUNTED_PATH__BACKUPDIR"] = "{$GLOBALS["MOUNT_POINT"]}/{$path}/{$hostname}"; $GLOBALS["MOUNTED_PATH_FINAL"] = "{$GLOBALS["MOUNT_POINT"]}/{$path}/{$hostname}/{$GLOBALS["DIRBYTES"]}"; $GLOBALS["MOUNTED_PATH_FINAL"] = str_replace("//", "/", $GLOBALS["MOUNTED_PATH_FINAL"]); if ($GLOBALS["VERBOSE"]) { echo "MOUNTED_PATH_FINAL: {$GLOBALS["MOUNTED_PATH_FINAL"]}\n"; } if (!is_dir($GLOBALS["MOUNTED_PATH_FINAL"])) { if ($GLOBALS["VERBOSE"]) { echo "Create -> {$GLOBALS["MOUNTED_PATH_FINAL"]}\n"; } @mkdir($GLOBALS["MOUNTED_PATH_FINAL"], 0755, true); if (!is_dir($GLOBALS["MOUNTED_PATH_FINAL"])) { cyrus_admin_mysql(0, "Unable to backup: Permission denied on WebDAV resource {$server}", "For creating directory {$GLOBALS["MOUNTED_PATH_FINAL"]}", __FILE__, __LINE__); killNas(); return; } } $t = time(); @touch("{$GLOBALS["MOUNTED_PATH_FINAL"]}/{$t}"); if (!is_file("{$GLOBALS["MOUNTED_PATH_FINAL"]}/{$t}")) { cyrus_admin_mysql(0, "Unable to backup: Permission denied on WebDAV", "resource {{$GLOBALS["MOUNTED_PATH_FINAL"]}/{$t}}", __FILE__, __LINE__); killNas(); return; } @unlink("{$GLOBALS["MOUNTED_PATH_FINAL"]}/{$t}"); if ($GLOBALS["VERBOSE"]) { echo "backup_ldap():\n"; } backup_ldap(); if ($GLOBALS["VERBOSE"]) { echo "backup_cyrus():\n"; } backup_cyrus(); remove_containers(); killNas(); }
function ftp_backup($WordpressBackupParams) { $sock = new sockets(); $mount = new mount(); $unix = new unix(); $rm = $unix->find_program("rm"); $FTP_ENABLE = intval($WordpressBackupParams["FTP_ENABLE"]); if ($FTP_ENABLE == 0) { echo "FTP disbabled\n"; return; } $FTP_SERVER = $WordpressBackupParams["FTP_ENABLE"]; $FTP_USER = $WordpressBackupParams["FTP_USER"]; $FTP_PASS = $WordpressBackupParams["FTP_PASS"]; $FTP_SERVER = $WordpressBackupParams["FTP_SERVER"]; $mntDir = "/home/artica/mnt-wordpress-" . time(); @mkdir($mntDir, 0755, true); if (!$mount->ftp_mount($mntDir, $FTP_SERVER, $FTP_USER, $FTP_PASS)) { apache_admin_mysql(0, "Unable to mount FTP {$FTP_USER}@{$FTP_SERVER}", null, __FILE__, __LINE__); return; } $FTPDir = "{$mntDir}/" . $unix->hostname_g() . "/wordpress-backup"; echo "Starting copy... in {$FTPDir}\n"; if ($GLOBALS["VERBOSE"]) { echo "Checks {$FTPDir}\n"; } if (!is_dir($FTPDir)) { if ($GLOBALS["VERBOSE"]) { echo "{$FTPDir} no such directory\n"; } @mkdir($FTPDir, 0755, true); } if (!is_dir($FTPDir)) { apache_admin_mysql(0, "Fatal FTP {$FTP_USER}@{$FTP_SERVER} {$FTPDir} permission denied", null, __FILE__, __LINE__); $mount->umount($mntDir); @rmdir($mntDir); return; } $directories_servernames = $unix->dirdir($WordpressBackupParams["DEST"]); $cp = $unix->find_program("cp"); while (list($directory, $ext) = each($directories_servernames)) { $dirRoot = basename($directory); $TargetDirectory = "{$FTPDir}/{$dirRoot}"; if (!is_dir($TargetDirectory)) { if ($GLOBALS["VERBOSE"]) { echo "Create directory {$TargetDirectory}\n"; } @mkdir($TargetDirectory, 0755, true); } if (!is_dir($TargetDirectory)) { apache_admin_mysql(0, "Fatal FTP {$FTP_USER}@{$FTP_SERVER} {$TargetDirectory} permission denied", __FILE__, __LINE__); continue; } if ($GLOBALS["VERBOSE"]) { echo "Scaning {$directory}\n"; } $directories_conteners = $unix->dirdir($directory); while (list($directoryTime, $ext) = each($directories_conteners)) { $dirRootTime = basename($directoryTime); $TargetDirectory = "{$FTPDir}/{$dirRoot}/{$dirRootTime}"; @mkdir($TargetDirectory, 0755, true); if (!is_dir($TargetDirectory)) { apache_admin_mysql(0, "Fatal FTP {$FTP_USER}@{$FTP_SERVER} {$TargetDirectory} permission denied", __FILE__, __LINE__); continue; } if (!is_file("{$directoryTime}/database.gz")) { apache_admin_mysql(0, "Fatal {$directoryTime}/database.gz no such file, skip", null, __FILE__, __LINE__); continue; } $t = time(); $results = array(); if ($GLOBALS["VERBOSE"]) { echo "Copy {$directoryTime}/* -> {$TargetDirectory}\n"; } exec("{$cp} -rf {$directoryTime}/* {$TargetDirectory}/ 2>&1", $results); while (list($a, $b) = each($results)) { if (preg_match("#cannot#i", $b)) { apache_admin_mysql(0, "Fatal Copy error {$b}, skip", $b, __FILE__, __LINE__); } } $took = $unix->distanceOfTimeInWords($t, time()); if (!is_file("{$TargetDirectory}/database.gz")) { apache_admin_mysql(0, "Fatal {$TargetDirectory}/database.gz permission denied, skip", null, __FILE__, __LINE__); continue; } shell_exec("{$rm} -rf {$directoryTime}"); } } $mount->umount($mntDir); @rmdir($mntDir); return; }
function CleanMysqlDatabase($PURGE_ALL = false) { $filter = null; $users = new usersMenus(); $unix = new unix(); $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid"; $timefile = "/etc/artica-postfix/pids/logrotate." . __FUNCTION__ . ".time"; $pid = @file_get_contents("{$pidfile}"); if ($unix->process_exists($pid, basename(__FILE__))) { system_admin_events("Already executed PID {$pid}", __FUNCTION__, __FILE__, __LINE__, "logrotate"); die; } @file_put_contents($pidfile, getmypid()); $time = $unix->file_time_min($timefile); if (!$PURGE_ALL) { if (!$GLOBALS["FORCE"]) { if ($time < 15) { events("No less than 15mn or delete {$timefile} file to force..."); system_admin_events("No less than 15mn or delete {$timefile} file", __FUNCTION__, __FILE__, __LINE__, "logrotate"); return; } } } @unlink($timefile); @file_put_contents($timefile, time()); $sock = new sockets(); $MySQLSyslogType = $sock->GET_INFO("MySQLSyslogType"); $EnableSyslogDB = $sock->GET_INFO("EnableSyslogDB"); if (!is_numeric($EnableSyslogDB)) { $EnableSyslogDB = 0; } if (!is_numeric($MySQLSyslogType)) { $MySQLSyslogType = 1; } if ($MySQLSyslogType == 0) { $MySQLSyslogType = 4; } $LogRotatePath = $sock->GET_INFO("LogRotatePath"); if ($LogRotatePath == null) { $LogRotatePath = "/home/logrotate"; } $TuningParameters = unserialize(base64_decode($sock->GET_INFO("MySQLSyslogParams"))); if ($EnableSyslogDB == 1) { if ($MySQLSyslogType == 2) { events("Is a client of remote MySQL server , aborting"); if ($GLOBALS["VERBOSE"]) { echo "Is a client of remote MySQL server , aborting\n"; } return; } } $LogRotateCompress = 1; $LogRotatePath = $sock->GET_INFO("LogRotatePath"); $SystemLogsPath = $sock->GET_INFO("SystemLogsPath"); $BackupMaxDays = $sock->GET_INFO("BackupMaxDays"); $BackupMaxDaysDir = $sock->GET_INFO("BackupMaxDaysDir"); $BackupSquidLogsUseNas = $sock->GET_INFO("BackupSquidLogsUseNas"); if ($SystemLogsPath == null) { $SystemLogsPath = "/var/log"; } $MySQLSyslogType = $sock->GET_INFO("MySQLSyslogType"); $EnableSyslogDB = $sock->GET_INFO("EnableSyslogDB"); if (!is_numeric($EnableSyslogDB)) { $EnableSyslogDB = 0; } if (!is_numeric($MySQLSyslogType)) { $MySQLSyslogType = 1; } if (!is_numeric($BackupSquidLogsUseNas)) { $BackupSquidLogsUseNas = 0; } if ($EnableSyslogDB == 1) { if ($MySQLSyslogType != 1) { return; } } if (!is_numeric($BackupMaxDays)) { $BackupMaxDays = 30; } if ($LogRotatePath == null) { $LogRotatePath = "/home/logrotate"; } if ($BackupMaxDaysDir == null) { $BackupMaxDaysDir = "/home/logrotate_backup"; } $mount = new mount("/var/log/artica-postfix/logrotate.debug"); if ($BackupSquidLogsUseNas == 1) { $BackupSquidLogsNASIpaddr = $sock->GET_INFO("BackupSquidLogsNASIpaddr"); $BackupSquidLogsNASFolder = $sock->GET_INFO("BackupSquidLogsNASFolder"); $BackupSquidLogsNASUser = $sock->GET_INFO("BackupSquidLogsNASUser"); $BackupSquidLogsNASPassword = $sock->GET_INFO("BackupSquidLogsNASPassword"); $BackupSquidLogsNASRetry = $sock->GET_INFO("BackupSquidLogsNASRetry"); if (!is_numeric($BackupSquidLogsNASRetry)) { $BackupSquidLogsNASRetry = 0; } $mountPoint = "/mnt/BackupSquidLogsUseNas"; if (!$mount->smb_mount($mountPoint, $BackupSquidLogsNASIpaddr, $BackupSquidLogsNASUser, $BackupSquidLogsNASPassword, $BackupSquidLogsNASFolder)) { events("Unable to connect to NAS storage system (1): {$BackupSquidLogsNASUser}@{$BackupSquidLogsNASIpaddr}"); if ($BackupSquidLogsNASRetry == 0) { return; } sleep(3); $mount = new mount("/var/log/artica-postfix/logrotate.debug"); if (!$mount->smb_mount($mountPoint, $BackupSquidLogsNASIpaddr, $BackupSquidLogsNASUser, $BackupSquidLogsNASPassword, $BackupSquidLogsNASFolder)) { events("Unable to connect to NAS storage system (2): {$BackupSquidLogsNASUser}@{$BackupSquidLogsNASIpaddr}"); return; } } $BackupMaxDaysDir = "{$mountPoint}/artica-backup-syslog/{$users->hostname}"; } @mkdir("{$BackupMaxDaysDir}", 0755, true); if (!is_dir($BackupMaxDaysDir)) { if ($GLOBALS["VERBOSE"]) { echo "Fatal {$BackupMaxDaysDir} permission denied\n"; } events("Fatal {$BackupMaxDaysDir} permission denied"); squid_admin_notifs("SYSLOG: FATAL {$BackupMaxDaysDir} permission denied", __FUNCTION__, __FILE__, __LINE__); system_admin_events($q->mysql_error, __FUNCTION__, __FILE__, __LINE__, "logrotate"); if ($BackupSquidLogsUseNas == 1) { $mount->umount($mountPoint); } return false; } $t = time(); @file_put_contents("{$BackupMaxDaysDir}/{$t}", time()); if (!is_file("{$BackupMaxDaysDir}/{$t}")) { events("Fatal {$BackupMaxDaysDir} permission denied"); if ($GLOBALS["VERBOSE"]) { echo "Fatal {$BackupMaxDaysDir} permission denied\n"; } squid_admin_notifs("SYSLOG: FATAL {$BackupMaxDaysDir} permission denied", __FUNCTION__, __FILE__, __LINE__); system_admin_events($q->mysql_error, __FUNCTION__, __FILE__, __LINE__, "logrotate"); if ($BackupSquidLogsUseNas == 1) { $mount->umount($mountPoint); } return false; } @unlink("{$BackupMaxDaysDir}/{$t}"); if ($BackupSquidLogsUseNas == 1) { if (is_dir("/home/logrotate_backup")) { $files = $unix->DirFiles("/home/logrotate_backup"); events("Scanning the old storage systems.. " . count($files) . " file(s)"); while (list($basename, $none) = each($files)) { $filepath = "/home/logrotate_backup/{$basename}"; if ($GLOBALS["VERBOSE"]) { echo "Checking \"{$filepath}\"\n"; } $size = @filesize($filepath); if ($size < 20) { events("Removing {$filepath}"); @unlink($filepath); continue; } if (!@copy($filepath, "{$BackupMaxDaysDir}/{$basename}")) { events("copy Failed {$filepath} to \"{$BackupMaxDaysDir}/{$basename}\" permission denied..."); continue; } events("Move {$filepath} to {$BackupSquidLogsNASIpaddr} success..."); @unlink($filepath); } } } if ($PURGE_ALL == false) { $filter = "WHERE filetime<DATE_SUB(NOW(),INTERVAL {$BackupMaxDays} DAY)"; } if ($EnableSyslogDB == 1) { $q = new mysql_storelogs(); $sql = "SELECT `filename`,`hostname`,`storeid` FROM `files_info` {$filter}"; if ($GLOBALS["VERBOSE"]) { echo "{$sql}\n"; } $results = $q->QUERY_SQL($sql); if (!$q->ok) { system_admin_events($q->mysql_error, __FUNCTION__, __FILE__, __LINE__); return; } $Count = mysql_num_rows($results); $c = 0; while ($ligne = mysql_fetch_assoc($results)) { $c++; if ($GLOBALS["VERBOSE"]) { echo "{$c}/{$Count} ******** {$ligne["filename"]} {$ligne["storeid"]} *********\n"; } if (!$q->ExtractFile("{$BackupMaxDaysDir}/{$ligne["hostname"]}.{$ligne["filename"]}", $ligne["storeid"])) { if ($GLOBALS["VERBOSE"]) { echo "{$c}/{$Count} ******** {$ligne["filename"]} ExtractFile() = FALSE !!! *********\n"; } continue; } if ($GLOBALS["VERBOSE"]) { echo "{$c}/{$Count} DelteItem({$ligne["storeid"]}) *********\n"; } $q->DelteItem($ligne["storeid"]); if ($GLOBALS["VERBOSE"]) { echo "********* EVENTS NOW --->\n"; } $q->events("{$ligne["filename"]} saved into {$BackupMaxDaysDir}"); if ($GLOBALS["VERBOSE"]) { echo "\n\n###### {$c}/{$Count} Continue to Next ##########\n"; } } $sql = "SELECT `filename`,`hostname`,`storeid` FROM `accesslogs` {$filter}"; if ($GLOBALS["VERBOSE"]) { echo "{$sql}\n"; } $results = $q->QUERY_SQL($sql); if (!$q->ok) { system_admin_events($q->mysql_error, __FUNCTION__, __FILE__, __LINE__); return; } $Count = mysql_num_rows($results); $c = 0; while ($ligne = mysql_fetch_assoc($results)) { $c++; if (!$q->ExtractAccessFile("{$BackupMaxDaysDir}/{$ligne["hostname"]}.{$ligne["filename"]}", $ligne["storeid"])) { continue; } $q->DelteAccessItem($ligne["storeid"]); $q->events("{$ligne["filename"]} saved into {$BackupMaxDaysDir}"); } if ($BackupSquidLogsUseNas == 1) { $mount->umount($mountPoint); } return; } $q = new mysql_syslog(); $sql = "SELECT `filename`,`taskid`,`filesize`,`filetime` FROM `store` {$filter}"; $results = $q->QUERY_SQL($sql); if ($GLOBALS["VERBOSE"]) { echo "{$sql} ({$q->mysql_error}) " . mysql_num_rows($results) . " file(s)\n"; } if (!$q->ok) { system_admin_events($q->mysql_error, __FUNCTION__, __FILE__, __LINE__); return; } while ($ligne = mysql_fetch_assoc($results)) { if ($GLOBALS["VERBOSE"]) { echo "Processing {$ligne["filename"]}\n"; } if (!ExtractFileFromDatabase($ligne["filename"], $BackupMaxDaysDir)) { events("Unable to extract {$ligne["filename"]} to {$BackupMaxDaysDir}"); squid_admin_notifs("SYSLOG: Unable to extract {$ligne["filename"]} to {$BackupMaxDaysDir}", __FUNCTION__, __FILE__, __LINE__); if ($BackupSquidLogsUseNas == 1) { $mount->umount($mountPoint); } return false; } else { events("Success extracting {$ligne["filename"]} to {$BackupMaxDaysDir}"); } } if ($BackupSquidLogsUseNas == 1) { $mount->umount($mountPoint); } }
function killNas() { $sock = new sockets(); $mount = new mount($GLOBALS["LOGFILE"]); $mountPoint = "/mnt/SquidImportLogs"; if ($mount->ismounted($mountPoint)) { $mount->umount($mountPoint); } }
function ftp_backup() { $sock = new sockets(); $mount = new mount(); $unix = new unix(); $ZarafaBackupParams = unserialize(base64_decode($sock->GET_INFO("ZarafaBackupParams"))); $FTP_ENABLE = intval($ZarafaBackupParams["FTP_ENABLE"]); if ($FTP_ENABLE == 0) { echo "FTP disbabled\n"; return; } @mkdir("/home/artica/mnt-zarafa-ftp"); $FTP_SERVER = $ZarafaBackupParams["FTP_ENABLE"]; $FTP_USER = $ZarafaBackupParams["FTP_USER"]; $FTP_PASS = $ZarafaBackupParams["FTP_PASS"]; $FTP_SERVER = $ZarafaBackupParams["FTP_SERVER"]; $mntDir = "/home/artica/mnt-zarafa-ftp"; if ($ZarafaBackupParams["DEST"] == null) { $ZarafaBackupParams["DEST"] = "/home/zarafa-backup"; } if (!$mount->ftp_mount($mntDir, $FTP_SERVER, $FTP_USER, $FTP_PASS)) { system_admin_events("Unable to mount FTP {$FTP_USER}@{$FTP_SERVER}", __FUNCTION__, __FILE__, __LINE__, "mailboxes"); return; } $FTPDir = "{$mntDir}/" . $unix->hostname_g() . "/zarafa-backup"; @mkdir($FTPDir, 0755, true); if (!is_dir($FTPDir)) { system_admin_events("Fatal FTP {$FTP_USER}@{$FTP_SERVER} {$FTPDir} permission denied", __FUNCTION__, __FILE__, __LINE__, "mailboxes"); $mount->umount($mntDir); @rmdir($mntDir); return; } echo "Starting copy... in {$FTPDir}\n"; $directories = $unix->dirdir($ZarafaBackupParams["DEST"]); $cp = $unix->find_program("cp"); while (list($directory, $ext) = each($directories)) { if (!is_file("{$directory}/zarafa.gz")) { continue; } $dirRoot = basename($directory); $TargetDirectory = "{$FTPDir}/{$dirRoot}"; if (is_file("{$TargetDirectory}/zarafa.gz")) { continue; } @mkdir($TargetDirectory, 0755, true); if (!is_dir($TargetDirectory)) { system_admin_events("Fatal FTP {$FTP_USER}@{$FTP_SERVER} {$TargetDirectory} permission denied", __FUNCTION__, __FILE__, __LINE__, "mailboxes"); continue; } $results = array(); $t = time(); exec("{$cp} -rfv {$directory}/* {$TargetDirectory}/ 2>&1", $results); $took = $unix->distanceOfTimeInWords($t, time()); system_admin_events("Success backup {$dirRoot} to FTP {$FTP_SERVER} took: {$took}\nTarget:{$TargetDirectory}\n" . @implode("\n", $results), __FUNCTION__, __FILE__, __LINE__, "mailboxes"); } $mount->umount($mntDir); @rmdir($mntDir); return; }
function import($ligne) { $unix = new unix(); $Tmpdir = $unix->TEMP_DIR(); $connection = $ligne["connection"]; $username = $ligne["username"]; $password = $ligne["password"]; $folder = $ligne["folder"]; $filename = $ligne["filename"]; $ID = $ligne["ID"]; $folder = str_replace("\\", "/", $folder); if (strpos($folder, "/") > 0) { $FF = explode("/", $folder); $SharedDir = $FF[0]; unset($FF[0]); $folder = @implode("/", $FF); } $mountpoint = "{$Tmpdir}/{$ligne["ID"]}"; if ($folder != null) { $targetdir = "{$mountpoint}/{$folder}"; } else { $targetdir = $mountpoint; } if (!is_file("{$targetdir}/{$filename}")) { system_admin_events("{$connection}: {$targetdir}/{$filename}, no such file", __FUNCTION__, __FILE__, __LINE__, "import", $GLOBALS["SCHEDULE_ID"]); $mount = new mount(); $mount->umount($mountpoint); return false; } if (is_file("{$targetdir}/{$filename}.scanned")) { $SCANNED = unserialize(@file_get_contents("{$targetdir}/{$filename}.scanned")); } $md5file = md5_file("{$targetdir}/{$filename}"); if ($md5file == $ligne["lastmd5"]) { return true; } $handle = @fopen("{$targetdir}/{$filename}", "r"); if (!$handle) { system_admin_events("{$connection}: {$targetdir}/{$filename}, fatal error", __FUNCTION__, __FILE__, __LINE__, "import", $GLOBALS["SCHEDULE_ID"]); return; } $c = 0; $ldap = new clladp(); $OUS = $ldap->hash_get_ou(true); $defaultgroup = $ligne["ldapgroup"]; if ($defaultgroup > 0) { $gp = new groups($defaultgroup); $DefaultOu = $gp->ou; $defaultGroupName = $gp->groupName; if ($GLOBALS["VERBOSE"]) { echo "Default group: {$defaultgroup}/ {$gp->groupName}/{$DefaultOu}\n"; } } $t1 = time(); $c = 0; $d = 0; while (!feof($handle)) { $line = trim(fgets($handle, 1024)); $line = str_replace("\"", "", $line); if ($line == null) { continue; } if (strpos($line, ";") == 0) { continue; } $SCANMD = md5($line); $tr = explode(";", $line); $account = $tr[0]; $password = $tr[1]; $email = $tr[2]; $groupname = $tr[3]; $organization = $tr[4]; if ($organization == "organization") { continue; } if ($account == "account") { continue; } $Telephon = $tr[5]; $mobile = $tr[6]; $d++; if (isset($SCANNED[$SCANMD])) { continue; } if ($GLOBALS["VERBOSE"]) { echo "--------------- {$d} ----------------- {$SCANMD}\n"; } if ($organization == null) { if ($defaultgroup == 0) { continue; } } if ($organization == null) { if ($DefaultOu != null) { $organization = $DefaultOu; } } if ($groupname == null) { $groupname = $defaultGroupName; } if ($groupname == null) { if ($GLOBALS["VERBOSE"]) { echo "{$organization} NO GroupName !!\n"; } continue; } if (!isset($OUS[$organization])) { if (!$ldap->AddOrganization($organization)) { system_admin_events("fatal error failed to create entry {$organization}", __FUNCTION__, __FILE__, __LINE__, "import", $GLOBALS["SCHEDULE_ID"]); return; } $OUS = $ldap->hash_get_ou(true); } if (!isset($GLOBALS["GROUPS"][$organization])) { $GLOBALS["GROUPS"][$organization] = $ldap->hash_groups($organization); } if (!isset($GLOBALS["GROUPS"][$organization][$groupname])) { $gp = new groups(); $gp->ou = $organization; $gp->groupName = $groupname; if (!$gp->add_new_group($groupname, $organization)) { system_admin_events("fatal error failed to create entry {$groupname}/{$organization}", __FUNCTION__, __FILE__, __LINE__, "import", $GLOBALS["SCHEDULE_ID"]); return; } $GLOBALS["GROUPS"][$organization] = $ldap->hash_groups($organization); } if (!isset($GLOBALS["GROUPS"][$organization][$groupname]["gid"])) { if ($GLOBALS["VERBOSE"]) { echo "{$groupname}/{$organization} NO GID!!\n"; } continue; } $gid = $GLOBALS["GROUPS"][$organization][$groupname]["gid"]; if ($password == null) { $password = $account; } $UPDATE = FALSE; $user = new user($account); if (!is_numeric($user->uidNumber)) { $UPDATE = true; } if ($email != null) { if ($user->mail != $email) { if ($GLOBALS["VERBOSE"]) { echo "mail {$email}\n"; } $user->mail = $email; $UPDATE = true; } } if ($password != null) { if ($user->password != $password) { if ($GLOBALS["VERBOSE"]) { echo "password {$password}\n"; } $user->password = $password; $UPDATE = true; } } if ($Telephon != null) { if ($user->telephoneNumber != $Telephon) { if ($GLOBALS["VERBOSE"]) { echo "telephoneNumber {$Telephon}\n"; } $user->telephoneNumber = $Telephon; $UPDATE = true; } } if ($mobile != null) { if ($user->mobile != $mobile) { if ($GLOBALS["VERBOSE"]) { echo "mobile {$mobile}\n"; } $user->mobile = $mobile; $UPDATE = true; } } if ($user->ou != $organization) { if ($GLOBALS["VERBOSE"]) { echo "ou {$organization}\n"; } $user->ou = $organization; $UPDATE = true; } $user->group_id = $gid; if ($UPDATE) { $c++; if (!$user->SaveUser()) { continue; } } $SCANNED[$SCANMD] = time(); @file_put_contents("{$targetdir}/{$filename}.scanned", serialize($SCANNED)); if ($d > 500) { if (system_is_overloaded()) { system_admin_events("{$connection}: Overloaded system, aborting task", __FUNCTION__, __FILE__, __LINE__); return false; } $distance = $unix->distanceOfTimeInWords($t1, time(), true); system_admin_events("{$connection}: {$c} lines processed in {$distance}", __FUNCTION__, __FILE__, __LINE__); $d = 0; } } $date = date("Y-m-d H:i:s"); $q = new mysql(); $q->QUERY_SQL("UPDATE `texttoldap` SET `lastmd5`='{$md5file}',`lastscan`='{$date}' WHERE ID='{$ID}'", "artica_backup"); $distance = $unix->distanceOfTimeInWords($t1, time(), true); system_admin_events("{$connection}: {$c} lines processed in {$distance}", __FUNCTION__, __FILE__, __LINE__); $umount = $unix->find_program("umount"); shell_exec("{$umount} -l {$mountpoint}"); return true; }