123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- <?php
- backup_migrate_include('sources.filesource');
- class backup_migrate_files_destination_archivesource extends backup_migrate_destination_filesource {
- var $supported_ops = array('source');
- function type_name() {
- return t("Site Archive Source");
- }
-
- function sources() {
- $out = array();
- $out['archive'] = backup_migrate_create_destination('archive', array('machine_name' => 'archive', 'location' => '.', 'name' => t('Entire Site (code, files & DB)'), 'show_in_list' => FALSE));
- return $out;
- }
-
- function file_types() {
- return array(
- "sitearchive" => array(
- "extension" => "sitearchive.tar",
- "filemime" => "application/x-tar",
- "backup" => TRUE,
- "restore" => FALSE,
- ),
- );
- }
-
- function backup_settings_default() {
- $out = parent::backup_settings_default();
- $excludes = explode("\n", $out['exclude_filepaths']);
- foreach ($excludes as $i => $exclude) {
- $excludes[$i] = 'public://' . $exclude;
- }
- $excludes[] = conf_path() . '/settings.php';
- $excludes[] = file_directory_temp();
- return array(
- 'exclude_filepaths' => implode("\n", $excludes),
- );
- }
-
- function _backup_to_file_php($file, $settings) {
- if ($this->check_libs()) {
- $excluded_paths = empty($settings->filters['exclude_filepaths']) ? '' : $settings->filters['exclude_filepaths'];
- $exclude = $this->get_excluded_paths($excluded_paths);
- $files = $this->get_files_to_backup($this->get_location(), $settings, $exclude, realpath('.') . '/');
- if ($files) {
- $manifest = $this->generate_manifest();
- $db = $this->get_db();
- $file->push_type('sitearchive');
- $gz = new Archive_Tar($file->filepath(), false);
- $gz->addModify(array($manifest), $file->name .'/', dirname($manifest));
- $gz->addModify($files, $file->name .'/docroot', $this->get_location());
- $gz->addModify($db, $file->name . '/', dirname($db));
- unlink($manifest);
- rmdir(dirname($manifest));
- unlink($db);
- rmdir(dirname($db));
- return $file;
- }
- backup_migrate_backup_fail('No files available.', array(), $settings);
- return FALSE;
- }
- return FALSE;
- }
-
- function _backup_to_file_cli($file, $settings) {
- if (!empty($settings->filters['use_cli']) && function_exists('backup_migrate_exec') && function_exists('escapeshellarg')) {
- $excluded_paths = empty($settings->filters['exclude_filepaths']) ? '' : $settings->filters['exclude_filepaths'];
- foreach ($this->get_excluded_paths($excluded_paths) as $path) {
- $exclude[] = '--exclude=' . escapeshellarg($path);
- }
- $exclude = implode(' ', $exclude);
-
- $temp = backup_migrate_temp_directory();
- $manifest = $this->generate_manifest();
- $db = $this->get_db();
- rename($db, $temp . '/database.sql');
- rename($manifest, $temp . '/MANIFEST.ini');
- $file->push_type('sitearchive');
- $link = $temp . '/docroot';
- $input = realpath($this->get_location());
- backup_migrate_exec("ln -s %input %link; tar --dereference -C %temp -rf %output $exclude .", array('%output' => $file->filepath(), '%input' => $input, '%temp' => $temp, '%link' => $link));
- return $file;
- }
- return FALSE;
- }
-
- function generate_manifest() {
- $info = array(
- 'Global' => array(
- 'datestamp' => time(),
- 'formatversion' => '2011-07-02',
- 'generator' => 'Backup and Migrate (http://drupal.org/project/backup_migrate)',
- 'generatorversion' => BACKUP_MIGRATE_VERSION,
- ),
- 'Site 0' => array(
- 'version' => VERSION,
- 'name' => variable_get('site_name', ''),
- 'docroot' => 'docroot',
- 'sitedir' => 'docroot/' . conf_path(),
- 'database-file-default' => 'database.sql',
- 'database-file-driver' => 'mysql',
- ),
- );
- if ($private = variable_get('file_private_path', FALSE)) {
- $info['Site 0']['files-private'] = 'docroot/' . $private;
- }
- $info['Site 0']['files-public'] = 'docroot/' . variable_get('file_public_path', FALSE);
- $ini = $this->_array_to_ini($info);
- $tmpdir = backup_migrate_temp_directory();
- $filepath = $tmpdir . '/MANIFEST.ini';
- file_put_contents($filepath, $ini);
- return $filepath;
- }
-
- function get_db() {
- backup_migrate_include('destinations', 'files', 'filters', 'profiles');
- $file = new backup_file();
- $settings = _backup_migrate_profile_saved_default_profile();
- $settings->source_id = 'db';
- $settings->filters['compression'] = 'none';
- $file = backup_migrate_filters_backup($file, $settings);
-
- $tmpdir = backup_migrate_temp_directory();
- $filepath = $tmpdir . '/database.sql';
- rename($file->filepath(), $filepath);
- return $filepath;
- }
-
- function _restore_from_file_php($file, &$settings) {
- $success = false;
- if ($this->check_libs()) {
- $from = $file->pop_type();
- $temp = backup_migrate_temp_directory();
- $tar = new Archive_Tar($from->filepath());
- $tar->extractModify($temp, $file->name);
-
- $manifest = $this->read_manifest($temp);
-
- $site = $manifest['Site 0'];
- $docroot = $temp . '/' . $site['docroot'];
- $sqlfile = $temp . '/' . $site['database-file-default'];
- $filepath = NULL;
- if (isset($site['files-private'])) {
- $filepath = $temp . '/' . $site['files-private'];
- }
- else if (isset($site['files-public'])) {
- $filepath = $temp . '/' . $site['files-public'];
- }
-
- if ($filepath && file_exists($filepath)) {
- _backup_migrate_move_files($filepath, file_directory_path());
- }
- else {
- _backup_migrate_message('Files were not restored because the archive did not seem to contain a files directory or was in a format that Backup and Migrate couldn\'t read', array(), 'warning');
- }
-
- if ($sqlfile && file_exists($sqlfile)) {
- $db_settings = drupal_clone($settings);
- $db_settings->source_id = 'db';
- $file = new backup_file(array('filepath' => $sqlfile));
- $success = backup_migrate_filters_restore($file, $db_settings);
- }
- else {
- _backup_migrate_message('The database was not restored because the archive did not seem to contain a database backup or was in a format that Backup and Migrate couldn\'t read', array(), 'warning');
- }
- if ($docroot) {
- _backup_migrate_message('Backup and Migrate cannot restore the php code of the site for security reasons. You will have to copy the code to the server by hand if you wish to restore the full site.', array(), 'warning');
- }
- return $success && $file;
- }
- return FALSE;
- }
-
- function _restore_from_file_cli($file, &$settings) {
-
- return FALSE;
- }
-
- function read_manifest($directory) {
-
- $defaults = array(
- 'docroot' => 'docroot',
- 'database-file-default' => 'database.sql',
- 'database-file-driver' => 'mysql',
- );
- $out = $this->_ini_to_array($directory . '/MANIFEST.ini');
-
- $out['Site 0'] = isset($out['Site 0']) ? $out['Site 0'] : array();
- $out['Site 0'] += $defaults;
- return $out;
- }
-
- function _array_to_ini($sections) {
- $content = "";
- foreach ($sections as $section => $data) {
- $content .= '['. $section .']' . "\n";
- foreach ($data as $key => $val) {
- $content .= $key . " = \"". $val ."\"\n";
- }
- $content .= "\n";
- }
- return $content;
- }
-
- function _ini_to_array($path) {
- return parse_ini_file($path, TRUE);
- }
- }
|