| 1 |
<?php
|
| 2 |
/**
|
| 3 |
* This module is used to make a backup of the current instance of Drupal.
|
| 4 |
* A good example of when this module is useful is when you are hosting a
|
| 5 |
* Drupal powered site for end users that aren't familliar with a UNIX shell,
|
| 6 |
* but would like to be able to back up the website over the web.
|
| 7 |
*
|
| 8 |
*/
|
| 9 |
|
| 10 |
/**
|
| 11 |
* @todo
|
| 12 |
* - Code to view backups (in library)
|
| 13 |
* - Code to download backups
|
| 14 |
* - Code to delete backups
|
| 15 |
* - Configuration form:
|
| 16 |
* - Periodic backups via cron?
|
| 17 |
* - Max number of backup files via cron
|
| 18 |
* - Command-line script
|
| 19 |
*
|
| 20 |
*/
|
| 21 |
|
| 22 |
/**
|
| 23 |
* Implementation of hook_help().
|
| 24 |
*/
|
| 25 |
function backup_help($section = "") {
|
| 26 |
switch ($section) {
|
| 27 |
case 'admin/modules#description':
|
| 28 |
return t("Backs up your Drupal installation.");
|
| 29 |
}
|
| 30 |
}
|
| 31 |
|
| 32 |
|
| 33 |
/**
|
| 34 |
* Implementation of book_perm().
|
| 35 |
*/
|
| 36 |
function backup_perm() {
|
| 37 |
return(array("make backups"));
|
| 38 |
}
|
| 39 |
|
| 40 |
|
| 41 |
/**
|
| 42 |
* Display the menu items for this module.
|
| 43 |
*
|
| 44 |
*/
|
| 45 |
function backup_menu($may_cache) {
|
| 46 |
|
| 47 |
$retval = array();
|
| 48 |
|
| 49 |
//
|
| 50 |
// To re-create the menu after you make changes here, load the
|
| 51 |
// /admin/build/menu page.
|
| 52 |
//
|
| 53 |
if ($may_cache) {
|
| 54 |
$menu_type = MENU_CALLBACK |MENU_VISIBLE_IN_TREE
|
| 55 |
|MENU_MODIFIABLE_BY_ADMIN;
|
| 56 |
|
| 57 |
$retval[] = array(
|
| 58 |
"path" => "admin/content/backup",
|
| 59 |
"title" => t("backup"),
|
| 60 |
"access" => user_access('make backups'),
|
| 61 |
"callback" => "backup_page",
|
| 62 |
"description" => "Back up your Drupal installation.",
|
| 63 |
"type" => $menu_type,
|
| 64 |
);
|
| 65 |
|
| 66 |
}
|
| 67 |
|
| 68 |
return($retval);
|
| 69 |
|
| 70 |
} // End of backup_menu()
|
| 71 |
|
| 72 |
|
| 73 |
/**
|
| 74 |
* This function is used to create our backup form.
|
| 75 |
*
|
| 76 |
* @return array Associative array of all the form elements.
|
| 77 |
*/
|
| 78 |
function backup_form() {
|
| 79 |
|
| 80 |
$form = array();
|
| 81 |
|
| 82 |
//
|
| 83 |
// Javascript to process the click on the form, followed by out
|
| 84 |
// button to start the backup.
|
| 85 |
//
|
| 86 |
$javascript = "this.value=\""
|
| 87 |
. t("Creating the backup. Please stand by.") . "\"; "
|
| 88 |
. "this.disabled=1; ";
|
| 89 |
|
| 90 |
$form["backup"]["#type"] = "submit";
|
| 91 |
$form["backup"]["#value"] = t("Backup this Drupal installation.");
|
| 92 |
$form["backup"]["#attributes"] = array();
|
| 93 |
$form["backup"]["#attributes"]["onClick"] = $javascript;
|
| 94 |
$form["backup"]["#weight"] = 0;
|
| 95 |
|
| 96 |
$form["backup_text"]["#type"] = "markup";
|
| 97 |
$form["backup_test"]["#value"] = "<p>"
|
| 98 |
. t("NOTE: It may take some time for the backup to complete.");
|
| 99 |
|
| 100 |
//
|
| 101 |
// Debugging info, which includes our path and programs.
|
| 102 |
//
|
| 103 |
$form["debugging"]["#type"] = "fieldset";
|
| 104 |
$form["debugging"]["#title"] = t("Debugging Information");
|
| 105 |
$form["debugging"]["#collapsible"] = TRUE;
|
| 106 |
$form["debugging"]["#collapsed"] = TRUE;
|
| 107 |
$form["debugging"]["#weight"] = -1;
|
| 108 |
|
| 109 |
$content .= backup_get_path();
|
| 110 |
$form["debugging"]["path"]["#type"] = "fieldset";
|
| 111 |
$form["debugging"]["path"]["#title"] = t("System Path");
|
| 112 |
$form["debugging"]["path"]["#collapsible"] = TRUE;
|
| 113 |
$form["debugging"]["path"]["#description"] = $content;
|
| 114 |
|
| 115 |
$programs = backup_get_programs();
|
| 116 |
$content = backup_get_programs_html($programs, $error);
|
| 117 |
$form["debugging"]["programs"]["#type"] = "fieldset";
|
| 118 |
$form["debugging"]["programs"]["#title"] = t("Programs that will be used");
|
| 119 |
$form["debugging"]["programs"]["#collapsible"] = TRUE;
|
| 120 |
$form["debugging"]["programs"]["#description"] = $content;
|
| 121 |
|
| 122 |
|
| 123 |
//
|
| 124 |
// If an error was found, we want to uncollapse these elements so
|
| 125 |
// the user can see what went wrong.
|
| 126 |
//
|
| 127 |
if (!empty($error)) {
|
| 128 |
$form["debugging"]["#collapsed"] = FALSE;
|
| 129 |
$form["backup"]["#disabled"] = TRUE;
|
| 130 |
}
|
| 131 |
|
| 132 |
return($form);
|
| 133 |
|
| 134 |
} // End of backup_form()
|
| 135 |
|
| 136 |
|
| 137 |
/**
|
| 138 |
* Process our submitted form.
|
| 139 |
*
|
| 140 |
* @param string $form_id The unique ID of our form.
|
| 141 |
*
|
| 142 |
* @param array $form_values Associative array of form values.
|
| 143 |
*/
|
| 144 |
function backup_form_submit($form_id, $form_values) {
|
| 145 |
|
| 146 |
include("backup_lib.php");
|
| 147 |
|
| 148 |
$db_file = "";
|
| 149 |
|
| 150 |
//
|
| 151 |
// Backup our database
|
| 152 |
//
|
| 153 |
$error = backup_database($db_file);
|
| 154 |
if (!empty($error)) {
|
| 155 |
$error = "backup_database(): " . $error;
|
| 156 |
drupal_set_message($error, "error");
|
| 157 |
return(null);
|
| 158 |
}
|
| 159 |
|
| 160 |
drupal_set_message("Database backed up successfully.");
|
| 161 |
|
| 162 |
//
|
| 163 |
// Backup our filesystem
|
| 164 |
//
|
| 165 |
$error = backup_files($db_file, $backup_file);
|
| 166 |
if (!empty($error)) {
|
| 167 |
$error = "backup_files(): " . $error;
|
| 168 |
drupal_set_message($error, "error");
|
| 169 |
return(null);
|
| 170 |
}
|
| 171 |
|
| 172 |
drupal_set_message("Files backed up successfully.");
|
| 173 |
|
| 174 |
$message = "Click this filename to download: "
|
| 175 |
. "<a href=\"/". $backup_file . "\">" . $backup_file . "</a>";
|
| 176 |
drupal_set_message($message);
|
| 177 |
|
| 178 |
$message = "NOTE: File management has not been built into this module "
|
| 179 |
. "yet. You *must* use an FTP/SFTP client to delete old backups from "
|
| 180 |
. "your website. "
|
| 181 |
. "Watch for updates at <a href=\"http://drupal.org/node/120593\">"
|
| 182 |
. "http://drupal.org/node/120593</a>"
|
| 183 |
;
|
| 184 |
drupal_set_message($message, "error");
|
| 185 |
|
| 186 |
//
|
| 187 |
// Redirect back to our page.
|
| 188 |
//
|
| 189 |
drupal_goto("admin/content/backup");
|
| 190 |
|
| 191 |
} // End of backup_form_submit()
|
| 192 |
|
| 193 |
|
| 194 |
/**
|
| 195 |
* This function is our menu item callback. It is the main part of our module.
|
| 196 |
*/
|
| 197 |
function backup_page() {
|
| 198 |
|
| 199 |
$content = "";
|
| 200 |
|
| 201 |
$form_html .= drupal_get_form("backup_form");
|
| 202 |
|
| 203 |
$content .= $form_html;
|
| 204 |
|
| 205 |
//
|
| 206 |
// Send our content to the user
|
| 207 |
//
|
| 208 |
return($content);
|
| 209 |
|
| 210 |
} // End of backup_page()
|
| 211 |
|
| 212 |
|
| 213 |
/**
|
| 214 |
* Walk through the path and find the location of a specific file.
|
| 215 |
*
|
| 216 |
* @return mixed The full pathname to the file, or null on failure.
|
| 217 |
*/
|
| 218 |
function backup_search_path($file) {
|
| 219 |
|
| 220 |
$retval = '';
|
| 221 |
|
| 222 |
$path = getenv('PATH');
|
| 223 |
$path = explode(':', $path);
|
| 224 |
|
| 225 |
foreach ($path as $key => $value) {
|
| 226 |
$file2 = $value . '/' . $file;
|
| 227 |
if (is_file($file2)) {
|
| 228 |
$retval = $file2;
|
| 229 |
break;
|
| 230 |
}
|
| 231 |
}
|
| 232 |
|
| 233 |
return($retval);
|
| 234 |
|
| 235 |
} // End of backup_search_path()
|
| 236 |
|
| 237 |
|
| 238 |
/**
|
| 239 |
* This function gets a list of programs that will be used to perform the
|
| 240 |
* backup.
|
| 241 |
*
|
| 242 |
* @return array An associative array where the key is the file/directory
|
| 243 |
* and the value is the path to the program, or null if it couldn't be
|
| 244 |
* found.
|
| 245 |
*/
|
| 246 |
function backup_get_programs() {
|
| 247 |
|
| 248 |
// Init
|
| 249 |
$retval = array();
|
| 250 |
|
| 251 |
//
|
| 252 |
// List of programs to search for.
|
| 253 |
//
|
| 254 |
$binaries = array(
|
| 255 |
"mysqldump",
|
| 256 |
"tar",
|
| 257 |
"gzip",
|
| 258 |
//"TEST2"
|
| 259 |
);
|
| 260 |
|
| 261 |
//
|
| 262 |
// Loop through our programs and search for each
|
| 263 |
//
|
| 264 |
foreach ($binaries as $key => $value) {
|
| 265 |
|
| 266 |
$file = backup_search_path($value);
|
| 267 |
|
| 268 |
if (!empty($file)) {
|
| 269 |
$retval[$value] = $file;
|
| 270 |
} else {
|
| 271 |
$retval[$value] = "";
|
| 272 |
}
|
| 273 |
|
| 274 |
}
|
| 275 |
|
| 276 |
return($retval);
|
| 277 |
|
| 278 |
} // End of backup_get_programs()
|
| 279 |
|
| 280 |
|
| 281 |
/**
|
| 282 |
* This function turns the list of programs into content.
|
| 283 |
*
|
| 284 |
* @param array $files Associative array of files from backup_get_programs()
|
| 285 |
*
|
| 286 |
* @param string boolean This is set to true if there was a missing file
|
| 287 |
* that prevents us from running the program.
|
| 288 |
*
|
| 289 |
* @return string Content string to be added on to the content variable.
|
| 290 |
*/
|
| 291 |
function backup_get_programs_html(&$files, &$error) {
|
| 292 |
|
| 293 |
// Init
|
| 294 |
$retval = "";
|
| 295 |
$error = false;
|
| 296 |
|
| 297 |
$retval .= '<ul>';
|
| 298 |
|
| 299 |
foreach ($files as $key => $value) {
|
| 300 |
|
| 301 |
if (empty($value)) {
|
| 302 |
form_set_error("", t("System program \"%file%\" not found in "
|
| 303 |
. "\$PATH. Please check with your sysadmin for further "
|
| 304 |
. "assistance. ",
|
| 305 |
array("%file%" => $key)
|
| 306 |
));
|
| 307 |
$retval .= t("<li>%key% program NOT FOUND</li>",
|
| 308 |
array("%key%" => $key)
|
| 309 |
);
|
| 310 |
$error = true;
|
| 311 |
|
| 312 |
} else {
|
| 313 |
$retval .= t("<li>%key% program found at: %value%</li>",
|
| 314 |
array(
|
| 315 |
"%key%" => $key,
|
| 316 |
"%value%" => $value,
|
| 317 |
));
|
| 318 |
|
| 319 |
}
|
| 320 |
|
| 321 |
}
|
| 322 |
|
| 323 |
$retval .= "</ul>";
|
| 324 |
|
| 325 |
return($retval);
|
| 326 |
|
| 327 |
} // End of backup_get_programs_html()
|
| 328 |
|
| 329 |
|
| 330 |
/**
|
| 331 |
* This function shows the path that is being searched.
|
| 332 |
*
|
| 333 |
* @return A string with the path that is being searched.
|
| 334 |
*/
|
| 335 |
function backup_get_path() {
|
| 336 |
|
| 337 |
$retval = '';
|
| 338 |
|
| 339 |
$path = getenv('PATH');
|
| 340 |
//
|
| 341 |
// Add in spaces so that the text will wrap.
|
| 342 |
//
|
| 343 |
$path = str_replace(':', ': ', $path);
|
| 344 |
|
| 345 |
$retval = $path;
|
| 346 |
|
| 347 |
return($retval);
|
| 348 |
|
| 349 |
} // End of backup_get_path()
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
?>
|