You are here

function backup_migrate_destination_filesource::get_files_to_backup in Backup and Migrate 8.3

Same name and namespace in other branches
  1. 6.3 includes/sources.filesource.inc \backup_migrate_destination_filesource::get_files_to_backup()
  2. 7.3 includes/sources.filesource.inc \backup_migrate_destination_filesource::get_files_to_backup()

Get a list of files to backup from the given set if dirs. Exclude any that match the array $exclude.

2 calls to backup_migrate_destination_filesource::get_files_to_backup()
backup_migrate_destination_filesource::_backup_to_file_php in includes/sources.filesource.inc
Backup from this source.
backup_migrate_files_destination_archivesource::_backup_to_file_php in includes/sources.archivesource.inc
Backup from this source.

File

includes/sources.filesource.inc, line 209
A destination type for saving locally to the server.

Class

backup_migrate_destination_filesource
A destination type for saving locally to the server.

Code

function get_files_to_backup($dir, $settings, $exclude = array(), $base_dir = '') {
  $out = $errors = array();
  if (!file_exists($dir)) {
    backup_migrate_backup_fail('Directory %dir does not exist.', array(
      '%dir' => $dir,
    ), $settings);
    return FALSE;
  }
  if ($handle = @opendir($dir)) {
    while (($file = readdir($handle)) !== FALSE) {
      if ($file != '.' && $file != '..' && !in_array($file, $exclude)) {
        $real = realpath($dir . '/' . $file);
        $path = str_replace($base_dir, '', $real);

        // If the path is not excluded.
        if (!in_array($path, $exclude)) {
          if (is_dir($real)) {
            $subdir = $this
              ->get_files_to_backup($real, $settings, $exclude, $base_dir);

            // If there was an error reading the subdirectory then abort the backup.
            if ($subdir === FALSE) {
              closedir($handle);
              return FALSE;
            }

            // If the directory is empty, add an empty directory.
            if (count($subdir) == 0) {
              $out[] = $real;
            }
            $out = array_merge($out, $subdir);
          }
          else {
            if (is_readable($real)) {
              $out[] = $real;
            }
            else {
              $errors[] = $dir . '/' . $file;
            }
          }
        }
      }
    }
    closedir($handle);
  }
  else {
    backup_migrate_backup_fail('Could not open directory %dir', array(
      '%dir' => $dir,
    ), $settings);
    return FALSE;
  }

  // Alert the user to any errors there might have been.
  if ($errors) {
    if (count($errors < 5)) {
      $filesmsg = t('The following files: !files', array(
        '!files' => theme('item_list', array(
          'items' => $errors,
        )),
      ));
    }
    else {
      $filesmsg = t('!count files', array(
        '!count' => count($errors),
      ));
    }
    if (empty($settings->filters['ignore_errors'])) {
      backup_migrate_backup_fail('The backup could not be completed because !files could not be read. If you want to skip unreadable files use the \'Ignore Errors\' setting under \'Advanced Options\' in \'Advanced Backup\' or in your schedule settings profile.', array(
        '!files' => $filesmsg,
      ), 'error');
      $out = FALSE;
    }
    else {
      backup_migrate_backup_fail('!files could not be read and were skipped', array(
        '!files' => $filesmsg,
      ), 'error');
    }
  }
  return $out;
}