sources.filesource.inc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /**
  3. * @file
  4. * A destination type for saving locally to the server.
  5. */
  6. /**
  7. * A destination type for saving locally to the server.
  8. *
  9. * @ingroup backup_migrate_destinations
  10. */
  11. class backup_migrate_destination_filesource extends backup_migrate_source {
  12. var $supported_ops = array('restore', 'configure', 'delete', 'source');
  13. function type_name() {
  14. return t("Files Directory");
  15. }
  16. /**
  17. * Declare the current files directory as a backup source..
  18. */
  19. function sources() {
  20. $out = array();
  21. $out['files'] = backup_migrate_create_destination('filesource', array('machine_name' => 'files', 'location' => 'public://', 'name' => t('Public Files Directory'), 'show_in_list' => FALSE));
  22. if (variable_get('file_private_path', FALSE)) {
  23. $out['files_private'] = backup_migrate_create_destination('filesource', array('machine_name' => 'files', 'location' => 'private://', 'name' => t('Private Files Directory'), 'show_in_list' => FALSE));
  24. }
  25. return $out;
  26. }
  27. /**
  28. * Get the form for the settings for the files destination.
  29. */
  30. function edit_form() {
  31. $form = parent::edit_form();
  32. $form['location'] = array(
  33. "#type" => "textfield",
  34. "#title" => t("Directory path"),
  35. "#default_value" => $this->get_location(),
  36. "#required" => TRUE,
  37. "#description" => t('Enter the path to the directory to save the backups to. Use a relative path to pick a path relative to your Drupal root directory. The web server must be able to write to this path.'),
  38. );
  39. return $form;
  40. }
  41. /**
  42. * Return a list of backup filetypes.
  43. */
  44. function file_types() {
  45. return array(
  46. "tar" => array(
  47. "extension" => "tar",
  48. "filemime" => "application/x-tar",
  49. "backup" => TRUE,
  50. "restore" => TRUE,
  51. ),
  52. );
  53. }
  54. /**
  55. * Get the form for the settings for this destination.
  56. *
  57. * Return the default directories whose data can be ignored. These directories contain
  58. * info which can be easily reproducted. Also exclude the backup and migrate folder
  59. * to prevent exponential bloat.
  60. */
  61. function backup_settings_default() {
  62. return array(
  63. 'exclude_filepaths' => "backup_migrate\nstyles\ncss\njs\nctools\nless",
  64. );
  65. }
  66. /**
  67. * Get the form for the backup settings for this destination.
  68. */
  69. function backup_settings_form($settings) {
  70. $form['exclude_filepaths'] = array(
  71. "#type" => "textarea",
  72. "#multiple" => TRUE,
  73. "#title" => t("Exclude the following files or directories"),
  74. "#default_value" => $settings['exclude_filepaths'],
  75. "#description" => t("A list of files or directories to be excluded from backups. Add one path per line relative to the directory being backed up."),
  76. );
  77. return $form;
  78. }
  79. /**
  80. * Backup from this source.
  81. */
  82. function backup_to_file($file, $settings) {
  83. if ($out = $this->_backup_to_file_cli($file, $settings)) {
  84. return $out;
  85. }
  86. else {
  87. return $this->_backup_to_file_php($file, $settings);
  88. }
  89. }
  90. /**
  91. * Backup from this source.
  92. */
  93. function _backup_to_file_php($file, $settings) {
  94. if ($this->check_libs()) {
  95. $excluded_paths = empty($settings->filters['exclude_filepaths']) ? '' : $settings->filters['exclude_filepaths'];
  96. $files = $this->get_files_to_backup($this->get_realpath(), $settings, $this->get_excluded_paths($excluded_paths), DRUPAL_ROOT . '/');
  97. if ($files) {
  98. $file->push_type('tar');
  99. $gz = new Archive_Tar($file->filepath(), false);
  100. $gz->addModify($files, '', $this->get_realpath());
  101. return $file;
  102. }
  103. backup_migrate_backup_fail('No files available.', array(), $settings);
  104. return FALSE;
  105. }
  106. return FALSE;
  107. }
  108. /**
  109. * Backup from this source.
  110. */
  111. function _backup_to_file_cli($file, $settings) {
  112. if (!empty($settings->filters['use_cli']) && function_exists('backup_migrate_exec') && function_exists('escapeshellarg')) {
  113. $excluded_paths = empty($settings->filters['exclude_filepaths']) ? '' : $settings->filters['exclude_filepaths'];
  114. $exclude = array();
  115. foreach ($this->get_excluded_paths($excluded_paths) as $path) {
  116. $exclude[] = '--exclude=' . escapeshellarg($path);
  117. }
  118. $exclude = implode(' ', $exclude);
  119. // Create a symlink in a temp directory so we can rename the file in the archive.
  120. $temp = backup_migrate_temp_directory();
  121. $file->push_type('tar');
  122. backup_migrate_exec("tar --dereference -C %input -rf %output $exclude .", array('%output' => $file->filepath(), '%input' => $this->get_realpath(), '%temp' => $temp));
  123. return $file;
  124. }
  125. return FALSE;
  126. }
  127. /**
  128. * Restore to this source.
  129. */
  130. function restore_from_file($file, &$settings) {
  131. if ($out = $this->_restore_from_file_cli($file, $settings)) {
  132. return $out;
  133. }
  134. else {
  135. return $this->_restore_from_file_php($file, $settings);
  136. }
  137. }
  138. /**
  139. * Restore to this source.
  140. */
  141. function _restore_from_file_php($file, &$settings) {
  142. if ($this->check_libs()) {
  143. $from = $file->pop_type();
  144. $temp = backup_migrate_temp_directory();
  145. $tar = new Archive_Tar($from->filepath());
  146. $tar->extractModify($temp, $file->name);
  147. // Older B&M Files format included a base 'files' directory.
  148. if (file_exists($temp .'/files')) {
  149. $temp = $temp . '/files';
  150. }
  151. if (file_exists($temp .'/'. $file->name .'/files')) {
  152. $temp = $temp . '/files';
  153. }
  154. // Move the files from the temp directory.
  155. _backup_migrate_move_files($temp, $this->get_realpath());
  156. return $file;
  157. }
  158. return FALSE;
  159. }
  160. /**
  161. * Restore to this source.
  162. */
  163. function _restore_from_file_cli($file, &$settings) {
  164. if (!empty($settings->filters['use_cli']) && function_exists('backup_migrate_exec')) {
  165. $temp = backup_migrate_temp_directory();
  166. backup_migrate_exec("tar -C %temp -xf %input", array('%input' => $file->filepath(), '%temp' => $temp));
  167. // Older B&M Files format included a base 'files' directory.
  168. if (file_exists($temp .'/files')) {
  169. $temp = $temp . '/files';
  170. }
  171. if (file_exists($temp .'/'. $file->name .'/files')) {
  172. $temp = $temp . '/files';
  173. }
  174. // Move the files from the temp directory.
  175. backup_migrate_exec("mv -rf %temp/* %output", array('%output' => $this->get_realpath(), '%temp' => $temp));
  176. return $file;
  177. }
  178. return FALSE;
  179. }
  180. /**
  181. * Get a list of files to backup from the given set if dirs. Exclude any that match the array $exclude.
  182. */
  183. function get_files_to_backup($dir, $settings, $exclude = array(), $base_dir = '') {
  184. $out = $errors = array();
  185. if (!file_exists($dir)) {
  186. backup_migrate_backup_fail('Directory %dir does not exist.', array('%dir' => $dir), $settings);
  187. return FALSE;
  188. }
  189. if ($handle = @opendir($dir)) {
  190. while (($file = readdir($handle)) !== FALSE) {
  191. if ($file != '.' && $file != '..' && !in_array($file, $exclude)) {
  192. $real = realpath($dir . '/' . $file);
  193. $path = str_replace($base_dir, '', $real);
  194. // If the path is not excluded.
  195. if (!in_array($path, $exclude)) {
  196. if (is_dir($real)) {
  197. $subdir = $this->get_files_to_backup($real, $settings, $exclude, $base_dir);
  198. // If there was an error reading the subdirectory then abort the backup.
  199. if ($subdir === FALSE) {
  200. closedir($handle);
  201. return FALSE;
  202. }
  203. // If the directory is empty, add an empty directory.
  204. if (count($subdir) == 0) {
  205. $out[] = $path;
  206. }
  207. $out = array_merge($out, $subdir);
  208. }
  209. else {
  210. if (is_readable($real)) {
  211. $out[] = $path;
  212. }
  213. else {
  214. $errors[] = $dir . '/' . $file;
  215. }
  216. }
  217. }
  218. }
  219. }
  220. closedir($handle);
  221. }
  222. else {
  223. backup_migrate_backup_fail('Could not open directory %dir', array('%dir' => $dir), $settings);
  224. return FALSE;
  225. }
  226. // Alert the user to any errors there might have been.
  227. if ($errors) {
  228. if (count($errors < 5)) {
  229. $filesmsg = t('The following files: !files', array('!files' => theme('item_list', array('items' => $errors))));
  230. }
  231. else {
  232. $filesmsg = t('!count files', array('!count' => count($errors)));
  233. }
  234. if (empty($settings->filters['ignore_errors'])) {
  235. 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');
  236. $out = FALSE;
  237. }
  238. else {
  239. backup_migrate_backup_fail('!files could not be read and were skipped', array('!files' => $filesmsg), 'error');
  240. }
  241. }
  242. return $out;
  243. }
  244. /**
  245. * Break the excpluded paths string into a usable list of paths.
  246. */
  247. function get_excluded_paths($paths) {
  248. $out = explode("\n", $paths);
  249. foreach ($out as $key => $val) {
  250. $out[$key] = trim($val, "/ \t\r\n");
  251. }
  252. return $out;
  253. }
  254. /**
  255. * Check that the required libraries are installed.
  256. */
  257. function check_libs() {
  258. $result = true;
  259. // Drupal 7 has Archive_Tar built in so there should be no need to include anything here.
  260. return $result;
  261. }
  262. /**
  263. * Get the file location.
  264. */
  265. function get_realpath() {
  266. return drupal_realpath($this->get_location());
  267. }
  268. }