Beispiel #1
0
 private function maintenance($type, $data, $handle = '')
 {
     if (!isset($this->b['delete_time']) && !isset($this->b['delete_amount'])) {
         return true;
     }
     $delete = $dir = $files = array();
     //get file list
     switch ($type) {
         case 'local':
             $dir = scandir(backup__($data['path']) . '/' . $this->b['_dirname']);
             break;
         case 'ftp':
             $dir = ftp_nlist($handle, '.');
             break;
         case 'ssh':
             $cmd[] = fpbx_which('ssh');
             $cmd[] = '-o StrictHostKeyChecking=no -i';
             $cmd[] = $data['key'];
             $cmd[] = $data['user'] . '\\@' . $data['host'];
             $cmd[] = '-p ' . $data['port'];
             $cmd[] = 'ls -1 ' . $data['path'] . '/' . $this->b['_dirname'];
             exec(implode(' ', $cmd), $dir);
             unset($cmd);
             break;
         case 'awss3':
             $contents = $handle->getBucket($data['bucket']);
             foreach ($contents as $file) {
                 $dir[] = $file['name'];
             }
             break;
     }
     //sanitize file list
     foreach ($dir as $file) {
         //dont include the current backup or special items
         if (in_array($file, array('.', '..', $this->b['_file'])) || !preg_match("/\\d+-\\d+-\\d+(?:-[0-9.]+(?:alpha|beta|rc|RC)?(?:\\d+(?:\\.[^\\.]+)*))?-\\d+.tgz/", $file)) {
             continue;
         }
         $f = explode('-', $file);
         //remove file sufix
         $files[$f[2]] = $file;
     }
     //sort file list based on backup creation time
     ksort($files, SORT_NUMERIC);
     //create delete list based on creation time
     if (isset($this->b['delete_time']) && $this->b['delete_time']) {
         $cut_line = strtotime($this->b['delete_time'] . ' ' . $this->b['delete_time_type'] . ' ago');
         foreach ($files as $epoch => $file) {
             if ($epoch < $cut_line) {
                 $delete[$epoch] = $file;
             }
         }
     }
     //create delete list based on quantity of files
     if (isset($this->b['delete_amount']) && $this->b['delete_amount']) {
         for ($i = 0; $i < $this->b['delete_amount']; $i++) {
             array_pop($files);
         }
         $delete = array_merge($files, $delete);
     }
     //now delete the actual files
     foreach ($delete as $key => $file) {
         switch ($type) {
             case 'local':
                 unlink(backup__($data['path']) . '/' . $this->b['_dirname'] . '/' . $file);
                 unset($delete[$key]);
                 break;
             case 'ftp':
                 ftp_delete($handle, $file);
                 unset($delete[$key]);
                 break;
             case 'awss3':
                 $handle->deleteObject($data['bucket'], baseName($file));
                 break;
             case 'ssh':
                 $cmd[] = fpbx_which('ssh');
                 $cmd[] = '-o StrictHostKeyChecking=no -i';
                 $cmd[] = $data['key'];
                 $cmd[] = $data['user'] . '\\@' . $data['host'];
                 $cmd[] = '-p ' . $data['port'];
                 $cmd[] = 'rm ' . $data['path'] . '/' . '/' . $this->b['_dirname'] . '/' . $file;
                 exec(implode(' ', $cmd));
                 unset($delete[$key]);
                 unset($cmd);
                 break;
         }
     }
 }
Beispiel #2
0
 function callback(&$var)
 {
     $var = backup__($var);
 }
Beispiel #3
0
/**
 * make sure backup file is local, download it and make it local if necessary
 */
function backup_restore_locate_file($id, $path)
{
    global $amp_conf;
    $path = trim($path, '/');
    $path = str_replace(array('..', ':'), '', trim($path, '/'));
    $path = escapeshellcmd($path);
    $s = backup_get_server($id);
    if (!$s) {
        return array('error_msg' => _('Backup Server not found!'));
    }
    //dest is where we gona put backup files pulled infrom other servers
    $dest = $amp_conf['ASTSPOOLDIR'] . '/tmp/' . 'backuptmp-s' . $id . '-' . time() . '-' . basename($path);
    switch ($s['type']) {
        case 'local':
            $s['path'] = backup__($s['path']);
            $path = $s['path'] . '/' . $path;
            break;
        case 'ftp':
            //subsitute variables if nesesary
            $s['host'] = backup__($s['host']);
            $s['port'] = backup__($s['port']);
            $s['user'] = backup__($s['user']);
            $s['password'] = backup__($s['password']);
            $s['path'] = backup__($s['path']);
            $path = ltrim($path, '/');
            $connection = new Connection($s['host'], $s['user'], $s['password'], $s['port'], 90, $s['transfer'] == 'passive');
            try {
                $connection->open();
            } catch (\Exception $e) {
                $this->b['error'] = $e->getMessage();
                backup_log($this->b['error']);
                return;
            }
            $wrapper = new FTPWrapper($connection);
            $permFactory = new PermissionsFactory();
            $ftptype = $wrapper->systype();
            if (strtolower($ftptype) == "unix") {
                $fsFactory = new FilesystemFactory($permFactory);
            } else {
                $fsFactory = new WindowsFilesystemFactory();
            }
            $manager = new FTPFilesystemManager($wrapper, $fsFactory);
            $dlVoter = new DownloaderVoter();
            $dlVoter->addDefaultFTPDownloaders($wrapper);
            $ulVoter = new UploaderVoter();
            $ulVoter->addDefaultFTPUploaders($wrapper);
            $crVoter = new CreatorVoter();
            $crVoter->addDefaultFTPCreators($wrapper, $manager);
            $deVoter = new DeleterVoter();
            $deVoter->addDefaultFTPDeleters($wrapper, $manager);
            $ftp = new FTP($manager, $dlVoter, $ulVoter, $crVoter, $deVoter);
            if (!$ftp) {
                $this->b['error'] = _("Error creating the FTP object");
            }
            $ftpdirs = $ftp->findFilesystems(new \Touki\FTP\Model\Directory($s['path']));
            $file = null;
            foreach ($ftpdirs as $thisdir) {
                if ($ftp->fileExists(new \Touki\FTP\Model\File($thisdir->getRealPath() . '/' . $path))) {
                    $file = $ftp->findFileByName($thisdir->getRealPath() . '/' . $path);
                }
            }
            try {
                $options = array(FTP::NON_BLOCKING => false, FTP::TRANSFER_MODE => FTP_BINARY);
                $ftp->download($dest, $file, $options);
                $path = $dest;
            } catch (\Exception $e) {
                return array('error_msg' => _('Failed to retrieve file from server!'));
            }
            break;
        case 'ssh':
            $s['path'] = backup__($s['path']);
            $s['user'] = backup__($s['user']);
            $s['host'] = backup__($s['host']);
            $cmd[] = fpbx_which('scp');
            $cmd[] = '-o StrictHostKeyChecking=no -i';
            $cmd[] = $s['key'];
            $cmd[] = '-P ' . $s['port'];
            $cmd[] = $s['user'] . '\\@' . $s['host'] . ':' . $s['path'] . '/' . $path;
            $cmd[] = $dest;
            exec(implode(' ', $cmd), $foo, $ret);
            unset($cmd);
            if ($ret === 0) {
                $path = $dest;
            } else {
                return array('error_msg' => _('Failed to retrieve file from server!'));
            }
            break;
        case 'awss3':
            $s['bucket'] = backup__($s['bucket']);
            $s['awsaccesskey'] = backup__($s['awsaccesskey']);
            $s['awssecret'] = backup__($s['awssecret']);
            $awss3 = new S3($s['awsaccesskey'], $s['awssecret']);
            dbug('S3 Path: ' . $path);
            dbug('S3 Dest: ' . $dest);
            if ($awss3->getObject($s['bucket'], $path, $dest) !== false) {
                $path = $dest;
            } else {
                return array('error_msg' => _('Failed to retrieve file from server!'));
            }
            break;
    }
    if (file_exists($path)) {
        return $path;
    } else {
        return array('error_msg' => _('File not found! ' . $path));
    }
}
Beispiel #4
0
            // XXX - need to check for errors here...
        } else {
            // run backup remotely
            $opts = array('bu' => $bu, 's' => $s, 'b' => $b);
            // do not run if there are no items to backup
            if (!$opts['bu']['items']) {
                backup_log(_('No items in backup set. Aborting.'));
                exit;
            }
            backup_log(_('Connecting to remote server...'));
            $cmd[] = fpbx_which('ssh');
            $cmd[] = '-o StrictHostKeyChecking=no -i';
            $cmd[] = backup__($s[$b->b['bu_server']]['key']);
            $cmd[] = '-p';
            $cmd[] = $s[$b->b['bu_server']]['port'];
            $cmd[] = backup__($s[$b->b['bu_server']]['user']) . '\\@' . backup__($s[$b->b['bu_server']]['host']);
            $cmd[] = '\'php -r "';
            // Cant serialize $db
            unset($opts['b']->db);
            unset($opts['b']->cdrdb);
            //var_dump($opts);
            $escape = '$bootstrap_settings["freepbx_auth"] = false;
				$bootstrap_settings["skip_astman"] = true;
				$restrict_mods = true;
				if (!@include_once(getenv("FREEPBX_CONF") ? getenv("FREEPBX_CONF") : "/etc/freepbx.conf")) {
					include_once("/etc/asterisk/freepbx.conf");
				}
				system($amp_conf["AMPBIN"] . "/backup.php --opts=' . base64_encode(serialize($opts)) . '");
				';
            $cmd[] = addcslashes(str_replace(array("\n", "\t"), '', $escape), '"$');
            $cmd[] = '"\'';
Beispiel #5
0
/**
 * make sure backup file is local, download it and make it local if necessary
 */
function backup_restore_locate_file($id, $path)
{
    global $amp_conf;
    $path = trim($path, '/');
    $path = str_replace(array('..', ':'), '', trim($path, '/'));
    $path = escapeshellcmd($path);
    $s = backup_get_server($id);
    if (!$s) {
        return array('error_msg' => _('Backup Server not found!'));
    }
    //dest is where we gona put backup files pulled infrom other servers
    $dest = $amp_conf['ASTSPOOLDIR'] . '/tmp/' . 'backuptmp-s' . $id . '-' . time() . '-' . basename($path);
    switch ($s['type']) {
        case 'local':
            $s['path'] = backup__($s['path']);
            $path = $s['path'] . '/' . $path;
            break;
        case 'ftp':
            //subsitute variables if nesesary
            $s['host'] = backup__($s['host']);
            $s['port'] = backup__($s['port']);
            $s['user'] = backup__($s['user']);
            $s['password'] = backup__($s['password']);
            $s['path'] = backup__($s['path']);
            $ftp = ftp_connect($s['host'], $s['port']);
            if (ftp_login($ftp, $s['user'], $s['password'])) {
                ftp_pasv($ftp, $s['transfer'] == 'passive');
                if (ftp_get($ftp, $dest, $s['path'] . '/' . $path, FTP_BINARY)) {
                    $path = $dest;
                } else {
                    return array('error_msg' => _('Failed to retrieve file from server!'));
                }
                ftp_close($ftp);
            } else {
                dbug('ftp connect error');
            }
            break;
        case 'ssh':
            $s['path'] = backup__($s['path']);
            $s['user'] = backup__($s['user']);
            $s['host'] = backup__($s['host']);
            $cmd[] = fpbx_which('scp');
            $cmd[] = '-o StrictHostKeyChecking=no -i';
            $cmd[] = $s['key'];
            $cmd[] = '-P ' . $s['port'];
            $cmd[] = $s['user'] . '\\@' . $s['host'] . ':' . $s['path'] . '/' . $path;
            $cmd[] = $dest;
            exec(implode(' ', $cmd), $foo, $ret);
            unset($cmd);
            if ($ret === 0) {
                $path = $dest;
            } else {
                return array('error_msg' => _('Failed to retrieve file from server!'));
            }
            break;
        case 'awss3':
            $s['bucket'] = backup__($s['bucket']);
            $s['awsaccesskey'] = backup__($s['awsaccesskey']);
            $s['awssecret'] = backup__($s['awssecret']);
            $awss3 = new S3($s['awsaccesskey'], $s['awssecret']);
            dbug('S3 Path: ' . $path);
            dbug('S3 Dest: ' . $dest);
            if ($awss3->getObject($s['bucket'], $path, $dest) !== false) {
                $path = $dest;
            } else {
                return array('error_msg' => _('Failed to retrieve file from server!'));
            }
            break;
    }
    if (file_exists($path)) {
        return $path;
    } else {
        return array('error_msg' => _('File not found! ' . $path));
    }
}