I need to restore complete database in a regular basis (weekly). I am using pure PHP + system call to mysqldump and mysql to backup and restore full database. The restore process takes 2 minutes. What would be the correct procedure in order to keep the site in maintenance instead of getting a Drupal database error? Is there some sequence of table restoration?

For example, I use UPDATE variable SET value = 's:1:\"1\";' WHERE name= 'maintenance_mode';" just before the restoration, but obviously as there is a DROP TABLE if exists maintenance_mode in the SQL backup file, the site will show an error instead of the maintenance screen for a moment, and maybe in more occasions if there are incongruences among the different tables as they are updated.

I was thinking of controlling the process with the .htaccess, forwarding all traffic to a static HTML maintenance page.

Update: I have created my own module as there are many other tables which are outside Drupal and the product has to be specifically integrated with the requirements of the customer. So I cannot use the Backup and Migrate module (as suggested in one of the answers).

  • Using drush, you can backup and restore your code and mysql db. For backup, drush archive-dump For restore, drush archive-restore For more help, click here [drushcommands.com/drush-7x/archive/archive-dump]
    – latikas
    Commented Jul 16, 2015 at 12:27
  • I am not sure if using drush will help to solve these problems. I tried Backup&Restore module and the results was successful. But indeed, my own method does good results if you don't delete all tables before the restoration. If you let the process go step by step, you will see some error for just a few seconds, nothing else. And with Backup&Restore I was able to see these errors too,for example, url_aliases not found, likely 'cause I was reloading the page just when that table was being updated. I think the best approach is to put in maintenance mode, and alter the table of variables the last.
    – Cesar
    Commented Jul 17, 2015 at 7:49

5 Answers 5


Add a maintenance-page--offline.tpl.php page to your theme , and whenever the connection to DB is lost drupal will show this page instead of an error , but designing this page you cant use formal variables which are used in theme like $title or $styles and ... cause these are stored in DB and in your situation there is no access to that , so you must add them using php or just implement some strings .

Check this page for more help : Theming the Drupal maintenance page

  • Thanks for the suggestion, but it doesn't work. Still a very ugly database error is displayed.
    – Cesar
    Commented Jul 16, 2015 at 10:58
  • @Cesar you did not follow all directions here drupal.org/node/195435 you can 100% remove database error message by just have hand written text in the maintanence-page.html file. It should just be a static HTML file so there is no database calls.
    – tenken
    Commented Jul 16, 2015 at 15:18

Check the documentation for theming the maintenance page.

You can set up a maintenance page template specifically for when the database is unavailable, template : maintenance-page--offline.tpl.php

And a plain maintenance page (template : maintenance-page.tpl.php)

Setup your maintenance templates and then do your restore with the mysql command.

Drupal should be able to handle all request during the maintenance process gracefully.

Have a read of the docs, set the maintenance templates up and have a go.

And do say how it went. It'd be interesting to read your feedback.

  • I cannot understand where to put this page. As the theme is stored in the database, and I have emptied the complete database, how can Drupal know where to look? I have copied those tpl files in my theme folder, and the database error is displayed .PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'semaphore' doesn't exist: SELECT expire, value FROM {semaphore} WHERE name = :name; Array ( [:name] => variable_init ) in lock_may_be_available()
    – Cesar
    Commented Jul 16, 2015 at 10:56
  • You can specify the default theme in the settings file - then your correct template should be used.
    – tecjam
    Commented Jul 16, 2015 at 11:39
  • Thanks. I finally followed all steps from the documentation, and even had to look in other pages. Please see my answer in which I describe all steps.
    – Cesar
    Commented Jul 16, 2015 at 11:54

Consider using module Backup and Migrate as a possible alternative. From the modules project page:

Back up and restore your Drupal MySQL database, code, and files or migrate a site between environments. Backup and Migrate supports gzip, bzip and zip compression as well as automatic scheduled backups.

With Backup and Migrate you can dump some or all of your database tables to a file download or save to a file on the server or offsite, and to restore from an uploaded or previously saved database dump. You can choose which tables and what data to backup and cache data is excluded by default.

Apart from the benefits you get from the module, I think it's worth trying to see if it may server as a work around ...

  • I have created my own module as there are many other tables which are outside Drupal and the product has to be specifically integrated with the requeriments of the customer. So I cannot use the module, thanks for the suggestion anyway.
    – Cesar
    Commented Jul 16, 2015 at 11:00

After reading the answers and the documentation, I found that these steps have to be followed:

  1. Change the permissions of the settings.php file in site/default, so it is writable. From 0444 to 0777.
  2. Add or uncomment the following lines in the settings file: $conf['error_level'] = ERROR_REPORTING_HIDE; $conf['maintenance_theme'] = 'bartik';
  3. In bartik theme (or any other that you define in settings), located in themes/bartik, copy the files which are in the folder themes/bartik/templates, maintenance-page.tpl.php and page.tpl.php renamed to maintenance-page--offline.tpl.php.
  4. Configure the maintenance-page--offline.tpl.php that you have copied to themes/bartik so it has no PHP errors, which means defining all the variables used in the file, or removing the variables not needed. If it contains errors it will not be rendered.

Thanks to all for the suggestions.


After making a complete test of the process, I find that the solutions proposed do not work in this case. The offline maintenance mode maybe is conceived for the situation when the database is not reachable or the connection is lost. But when I run the complete process of restoring the database, I first see the especial customized page, after several seconds a complete blank page, after some more seconds a "The website encountered an unexpected error. Please try again later." error and finally, when the mysql restore is complete, the working site. So this promising solution proposed by some users has failed completely.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.