Backup and restore Drupal sites with Drush
Submitted by abhishek on Thu, 11/08/2012 – 17:10
Drush is a really great tool! It provides two very useful commands to perform backup and restore operations on Drupal sites. Previously we had to create and restore sql dump and files tarball manually. With new Drush it’s really easy.
Please check you Drush version. This text was written for Drush 4.5.
12 | $ drush --version drush version 4.5 |
The simplest way to make backup is to run Drush command inside drupal root.
123 | $ cd /var/www/example .com $ drush archive-dump --destination= /var/backup/example .com. tar .gz Archive saved to /var/backup/example .com. tar .gz [ok] |
If you have a record with root key in your aliases.drushrc.php file you don’t need enter Drupal root directory.
123 | $aliases [ 'example' ] = array ( 'root' => '/var/www/example.com' , ); |
1 | $ drush @example arb --destination= /var/backup/example .com. tar .gz |
Let’s look inside out tarball.
12345678 | $ cd /var/backup && tar xf example.com. tar .gz && ls -lah total 3.3M drwxr-xr-x 3 dmytro dmytro 4.0K Dec 4 07:27 . drwxr-xr-x 12 dmytro dmytro 4.0K Dec 4 07:15 .. -rw-r--r-- 1 dmytro dmytro 2.8M Dec 4 07:19 example.com. tar .gz drwxr-xr-x 9 dmytro dmytro 4.0K Dec 4 07:11 example.com -rw-r--r-- 1 dmytro dmytro 525K Dec 4 07:19 example.sql -rw-r--r-- 1 dmytro dmytro 295 Dec 4 07:19 MANIFEST.ini |
Great! We have here all files from the Drupal root path, database dump and MANIFEST.ini. Here listing of MANIFEST.ini:
12345678910111213 | $ cat MANIFEST.ini [Global] datestamp = "1322975948" formatversion = "1.0" generator = "Drush archive-dump" generatorversion = "4.5" [default] docroot = "/var/www/example.com" sitedir = "sites/default" files-public = "sites/default/files" database-default- file = "./example.sql" database-default-driver = "mysql" |
As we see, MANIFEST.ini includes really useful information about archive. So, we can have a good night’s sleep, we have our backup. Now let’s try to restore it.
12345 | $ drush archive-restore /var/backup/example .com. tar .gz --destination= /var/www/example .com Destination directory /var/www/example .com. [error] Unable to restore files to /var/www/example .com [error] unlink( /tmp/drush_tmp_1323093421/example .com /sites/default/default .settings.php): Permission denied drush.inc:1565 |
Why so many errors? There are some problems in our command:
- Destination directory is already exists. Drush can’t overwrite it.
- Current system user has no permissions for some operations.
- Database is already exists. MySQL user from settings.php file has no permissions to drop database and create new.
Let’s look at the next command. It will correctly restore the archive.
123 | $ sudo drush archive-restore /var/backup/example .com. tar .gz \ --destination= /var/www/example .com \ --db- su =root --db- su -pw=password --overwrite |
- –db-su – account to use when creating the new database.
- –db-su-pw – password for the “db-su” account.
- –overwrite – allows Drush to overwrite any files in the destination.
Last command is to long. No problem! We can create site alias with command specific settings. Let’s add next lines to our aliases.drushrc.php.
12345678910 | $aliases [ 'example' ] = array ( 'command-specific' => array ( 'archive-restore' => array ( 'destination' => '/var/www/example.com' , 'db-su' => 'root' , 'db-su-pw' => 'password' , 'overwrite' => TRUE, ), ), ); |
Now we have a very neat command to restore the site from the archive.
1 | $ sudo drush @example arr /var/backup/example .com. tar .g |