function backup_mt ($deviceid) { unset($ftp); sql('LOCK TABLES backup_mt WRITE, log WRITE, backup_mt_files WRITE;'); $ergebnis = sql('SELECT device,user,pass FROM backup_mt WHERE id="'.$deviceid.'";'); $t = mysql_fetch_assoc($ergebnis); $device = $t['device']; $user = $t['user']; $pass = $t['pass']; if ($device == "") die('No device found for deviceid '.$deviceid); $ftp = ftp_connect($device); if (!$ftp) { sqllog(2,"MT Backup: could not connect to Device ".$device); } else { if (!ftp_login($ftp,$user,$pass)) { sqllog (2,"MT Backup: Could not login to ".$host." with user ".$user); } else { if (!ftp_pasv($ftp,TRUE)) sqllog (2,"MT Backup: could not enter passive mode for device ".$device); $files = ftp_nlist($ftp,"."); # # und jetzt holen wir uns die neuen Files # $found_backup = 0; $found_rsc = 0; $errors = 0; $timestamp = time(); foreach($files as $key) { if (preg_match("/(.rsc)|(.backup)$/",$key)) { $tmpname = tempnam("backup_mt","MTB"); if (!ftp_get($ftp,$tmpname,$key,FTP_BINARY)) { sqllog (3,"MT Backup: could not read ".$key." from .".$device); $errors ++; } else { if (preg_match("/.backup$/",$key)) $found_backup++; if (preg_match("/.rsc$/",$key)) $found_rsc++; # # File in Datenbank sichern und Temp-File lšschen # $tmpfile = fopen($tmpname, "r"); $data = fread($tmpfile, filesize($tmpname)); sql('INSERT INTO backup_mt_files (deviceid,timestamp,filename,filecontent) VALUES ('.$deviceid.','.$timestamp.',\''.$key.'\',\''.addslashes($data).'\');'); fclose($tmpfile); unlink($tmpname); } } } # # alte Backups aufrŠumen die letzten 30 Backups erhalten # $ergebnis = sql('SELECT DISTINCT timestamp FROM backup_mt_files WHERE deviceid="'.$deviceid.'" ORDER BY timestamp DESC LIMIT 30,99999;'); while ($t = mysql_fetch_assoc($ergebnis)) { sql('DELETE FROM backup_mt_files WHERE deviceid="'.$deviceid.'" AND timestamp="'.$t['timestamp'].'";'); } # # Backup sollte fertig sein, Status aktualisieren und aufrŠumen # sql('OPTIMIZE TABLE backup_mt_files;'); sql('UPDATE backup_mt SET lastbackup="'.$timestamp.'",status_backup="'.$found_backup.'",status_rsc="'.$found_rsc.'" WHERE id="'.$deviceid.'";'); } } $ergebnis = sql('UNLOCK TABLES;'); }