function _backup_migrate_restore_file in Backup and Migrate 6
Same name and namespace in other branches
- 5 backup_migrate.module \_backup_migrate_restore_file()
Restore from a previously backed up files. Accepts any file created by the backup function.
2 calls to _backup_migrate_restore_file()
- backup_migrate_restore_confirm_submit in ./
backup_migrate.module - backup_migrate_restore_submit in ./
backup_migrate.module - The restore submit. Do the restore.
File
- ./
backup_migrate.module, line 697 - Create (manually or scheduled) and restore backups of your Drupal MySQL database with an option to exclude table data (f.e. cache_*)
Code
function _backup_migrate_restore_file($filepath, $filename = "", $delete = FALSE) {
if (!$filename) {
$filename = $filepath;
}
$file_is_temp = $delete;
$open_func = "fopen";
$read_func = "fgets";
$close_func = "fclose";
// figure out if the file is compressed by the file extention
if (drupal_substr($filename, -4, 4) == ".sql") {
// No compression.
}
if (drupal_substr($filename, -3, 3) == ".gz") {
if (function_exists("gzopen")) {
$open_func = "gzopen";
$read_func = "gzgets";
$close_func = "gzclose";
}
else {
// GZip compression... not supported.
drupal_set_message(t("This version of PHP does not support gzip comressed files. Please try using an uncompressed sql backup."), 'error');
drupal_goto("admin/content/backup_migrate/restore");
}
}
// BZip compression.
if (drupal_substr($filename, -3, 3) == ".bz") {
if (function_exists("bzopen")) {
$open_func = "fopen";
$read_func = "fgets";
// Decompress the file to a temp file.
$tmp = tempnam(file_directory_temp(), 'tmp_');
if (($dst = fopen($tmp, "w")) && ($src = bzopen($filepath, "r"))) {
while ($data = bzread($src)) {
fwrite($dst, $data);
}
fclose($dst);
bzclose($src);
if ($delete) {
unlink($filepath);
}
$filepath = $tmp;
$delete = TRUE;
}
else {
drupal_set_message(t("Unable to decompress bzip file. Please try using an uncompressed backup."), 'error');
drupal_goto("admin/content/backup_migrate/restore");
}
}
else {
// BZip compression... not supported.
drupal_set_message(t("This version of PHP does not support bzip compressed files. Please try using an uncompressed backup."), 'error');
drupal_goto("admin/content/backup_migrate/restore");
}
}
// Zip compression.
if (drupal_substr($filename, -4, 4) == ".zip") {
if (class_exists('ZipArchive')) {
if ($filepath != $filename) {
rename($filepath, $filepath . ".zip");
$filepath .= ".zip";
}
$tmp = tempnam(file_directory_temp(), 'tmp_');
$zip = new ZipArchive();
if (($dst = fopen($tmp, "w")) && ($src = $zip
->open($filepath))) {
if ($data = $zip
->getFromIndex(0)) {
fwrite($dst, $data);
}
fclose($dst);
$zip
->close();
if ($delete) {
unlink($filepath);
}
$filepath = $tmp;
$delete = TRUE;
}
else {
drupal_set_message(t("Unable to decompress zip file. Please try using an uncompressed backup."), 'error');
drupal_goto("admin/content/backup_migrate/restore");
}
}
else {
// Zip compression... not supported.
drupal_set_message(t("This version of PHP does not support zip comressed files. Please try using an uncompressed backup."), 'error');
drupal_goto("admin/content/backup_migrate/restore");
}
}
// Open the file (with fopen or gzopen depending on file format).
if ($handle = @$open_func($filepath, "r")) {
$num = 0;
// Read one line at a time and run the query.
while ($line = $read_func($handle)) {
$line = trim($line);
if ($line) {
// Use the helper instead of the api function to avoid substitution of '{' etc.
_db_query($line);
$num++;
}
}
// Close the file with fclose/gzclose.
$close_func($handle);
// Delete the file if it is temporary.
if ($delete) {
unlink($filepath);
}
$message = t("Restore complete. %num SQL commands executed.", array(
"%num" => $num,
));
$message .= $file_is_temp ? "" : "(" . l(t("Restore Again..."), "admin/content/backup_migrate/restorefile/" . $filepath) . ")";
drupal_set_message($message);
}
else {
drupal_set_message(t("Unable to open file %file to restore database", array(
"%file" => $filepath,
)), 'error');
}
// Release cron semaphore that was set during scheduled backup.
variable_del('cron_semaphore');
// Delete any temp files we've created.
_backup_migrate_temp_file("", TRUE);
}