function add_items() { foreach ($this->b['items'] as $i) { switch ($i['type']) { case 'file': // substitute vars $i['path'] = backup__($i['path']); // Does the file exist? if (!file_exists($i['path'])) { // It could be a wildcard? $glob = glob($i['path']); if (!$glob) { break; } // Ahha! Wildcards! That's OK then. $dest = $dest = $this->b['_tmpdir'] . dirname($i['path']); if (!is_dir($dest)) { mkdir($dest, 0755, true); } } else { $dest = $this->b['_tmpdir'] . $i['path']; if (!is_dir(dirname($dest))) { mkdir(dirname($dest), 0755, true); } } //copy file $cmd = fpbx_which('cp') . " " . $i['path'] . " {$dest}"; exec($cmd); unset($cmd); break; case 'dir': //subsitute variable if nesesary $i['path'] = backup__($i['path']); // Match wildcards. $dirs = glob($i['path'], \GLOB_ONLYDIR); if (!isset($dirs[0])) { backup_log("Skipping requested directory '" . $i['path'] . "' as it does not exist."); break; } foreach ($dirs as $path) { // Create destination directory structure $dest = $this->b['_tmpdir'] . $path; if (!is_dir($dest)) { mkdir($dest, 0755, true); } // Where are we copying from? Note we explicitly use realpath here, // as there could be any number of symlinks leading to the CONTENTS. // But we want to just back the contents up, and pretend those links // don't exist. $src = realpath($path); // Build our list of extra excludes (if any). // Standard exclusions: // 1 - node_modules - compiled for the local machine, // and are easily regenerated. $excludes = " --exclude='node_modules' "; // 2 - *tgz and *gpg - previously downloaded files, and can be redownloaded. // Note this ALSO excludes backups so we don't put a backup inside a backup. $excludes .= "--exclude='*tgz' --exclude='*gpg' "; if ($i['exclude']) { if (!is_array($i['exclude'])) { $xArr = explode("\n", $i['exclude']); } else { $xArr = $i['exclude']; } foreach ($xArr as $x) { // Replace any __vars__ if given. $x = backup__($x); // Does it start with a slash? Treat that as // a full path with a filter, instead of just // an exclude. if ($x[0] === "/") { $excludes .= " --filter='-/ {$x}'"; } else { // It's a normal exclude $excludes .= " --exclude='{$x}'"; } } } // Use rsync to mirror $src to $dest. // Note we ensure we add the trailing slash to tell rsync to copy the // contents, not the targets (if the target is a link, for exmaple) // Note we do NOT use 'a', as we don't want special files // backed up (the -D or --special option is included in 'a') // backup_log("Backing up $src"); $cmd = fpbx_which('rsync') . "{$excludes} -rlptgov {$src}/ {$dest}/"; exec($cmd); unset($cmd); } break; case 'mysql': //build command $s = str_replace('server-', '', $i['path']); $sql_file = $this->b['_tmpdir'] . '/' . 'mysql-' . $s . '.sql'; $cmd[] = fpbx_which('mysqldump'); $cmd[] = '--host=' . backup__($this->s[$s]['host']); $cmd[] = '--port=' . backup__($this->s[$s]['port']); $cmd[] = '--user='******'user']); $cmd[] = '--password='******'password']); $cmd[] = backup__($this->s[$s]['dbname']); if ($i['exclude']) { foreach ($i['exclude'] as $x) { $cmd[] = '--ignore-table=' . backup__($this->s[$s]['dbname']) . '.' . backup__($x); } } $cmd[] = ' --opt --skip-comments --skip-extended-insert --lock-tables=false --skip-add-locks --compatible=no_table_options '; // Need to grep out leading /* comments and SET commands as they create problems // restoring using the PEAR $db class // $cmd[] = ' | '; $cmd[] = fpbx_which('grep'); $cmd[] = "-v '^\\/\\*\\|^SET'"; $cmd[] = ' > ' . $sql_file; exec(implode(' ', $cmd), $file, $status); unset($cmd, $file); // remove file and log error information if it failed. // if ($status !== 0) { unlink($sql_file); $error_string = sprintf(_("Backup failed dumping SQL database [%s] to file [%s], " . "you have a corrupted backup from server [%s]."), backup__($this->s[$s]['dbname']), $sql_file, backup__($this->s[$s]['host'])); backup_log($error_string); freepbx_log(FPBX_LOG_FATAL, $error_string); } break; case 'astdb': $hard_exclude = array('RG', 'BLKVM', 'FM', 'dundi'); $exclude = array_merge($i['exclude'], $hard_exclude); $astdb = astdb_get($exclude); file_put_contents($this->b['_tmpdir'] . '/astdb', serialize($astdb)); break; } } }
$b = new FreePBX\modules\Backup\Backup($r['bu'], $r['s']); $b->b['_ctime'] = $r['b']->b['_ctime']; $b->b['_file'] = $r['b']->b['_file']; $b->b['_dirname'] = $r['b']->b['_dirname']; backup_clear_log(); $b->init(); $b->run_hooks('pre-backup'); $b->add_items(); $b->build_manifest(); $b->save_manifest('local'); $b->create_backup_file(true); exit; } elseif (isset($vars['astdb']) && $vars['astdb']) { switch ($vars['astdb']) { case 'dump': echo astdb_get(array('RG', 'BLKVM', 'FM', 'dundi')); break; case 'restore': if (is_file($vars['data'])) { $vars['data'] = file_get_contents($vars['data']); } astdb_put(unserialize($vars['data']), array('RINGGROUP', 'BLKVM', 'FM', 'dundi')); break; } } else { show_opts(); } exit; function show_opts() { $e[] = 'backup.php';