Back up your site (command line)

Last modified: March 19, 2009 - 00:48

Back up your Drupal files

Copy the contents of your Drupal directory to a directory outside your website, for example /tmp/drupalbackup.

Option 1

Use a basic copy (and paste) to copy all the files (including the .htaccess file) from your Drupal directory to the backup directory:

cp -rp /path/to/drupal_site /path/to/backup_dir

-rp = recursive and preserve permissions

Option 2

Archive and compress all the files (including the .htaccess file) from your Drupal directory to the backup directory:

  1. Navigate to your backup directory
  2. tar cjf drupalbackup.tar.bz2 /path/to/drupal/

This creates a Bzip compressed archive of all the files in the target directory (your Drupal installation directory). If you want to check to make sure this worked use tar xjf drupalbackup.tar.bz2 to extract the files into a new directory.

Back up your Drupal database with MySQL

Before you begin, it's best to turn off all automated jobs (cron jobs).

mysqldump -u user -p databasename > drupaldatabasebackupfile.sql

...or use one of the options below:

  1. Navigate to your live site directory, and download a special script to your Drupal site folder which will backup your database to a file called 'backup.sql'. Put the backup in your 'backup' directory.
  2. cd drupal_site/
      wget -O drupalsqldump.sh "http://cvs.drupal.org/viewvc.py/drupal/contributions/sandbox/drumm/tools/drupalsqldump.sh?view=co"
      chmod +x drupalsqldump.sh

    Note that the -O above is the capital letter O and not a zero.

    If you are upgrading a test site, the path 'drupal_site' would be the path to your test site.

  3. The following command will look at your Drupal settings file, automatically connect to the database, and make a backup of it.
  • If you are running a version of Drupal that is 4.6 or newer, then enter the following:

    ./drupalsqldump.sh sites/default/settings.php > backup/backup.sql

  • If you are running a version of Drupal 4.5 or older, run the following:

    ./drupalsqldump.sh includes/conf.php > backup/backup.sql

If you are upgrading a test site, the path 'drupal_site' would be the path to your test site.

Remember where you saved this backup.sql in case there is an error during the upgrade.

Backup up with PostgreSQL

pg_dump -U [user] -h [host] [databasename] >dump.sql

or

pg_dump -U [user] -W -h [host] [databasename] -F c >dump.pg_restore.format

The former you can restore by running the script with psql, the latter by using the pg_restore utility.

For huge databases, the second method is nice because it is automatically compressed. Alternatively, pipe to gzip:

pg_dump -U [user] -W -h [host] [databasename] | gzip -c >dump.sql.gz

and to restore: gunzip -c dump.sql.gz | psql [options]

MySQL backup and restore

Kutakizukari - December 15, 2007 - 13:23

To back up a database, use the following command:

CODE:
mysqldump -u username -p databasename > dumpfile.sql

To import a MySQL dumpfile into a database use the following command:

CODE:
mysql -u username -p databasename < dumpfile.sql

In the above examples, replace "username" with the name of the MySQL user that has access to the database. Replace "databasename" with the name of the already-existing database. Replace "dumpfile" with the name of your backup file. When creating a MySQL dump file, I usually like to name the dump file the same name as the database to reduce confusion.

When executing the above commands, you will be prompted for the database user "username's" password.

If you're using MAMP

lizhenry - January 15, 2009 - 00:32

If you're using MAMP then you might need to either add /Applications/MAMP/Library/bin to your path, or edit drupalsqlbackup.sh to give the full path name to the mysqldump command.

------------------------
Liz Henry
liz@bookmaniac.net

Re: Back up your site (command line)

starkos - March 10, 2009 - 18:36

This script performs a dump of the entire MySQL database, including temporary tables, caches, and so forth, producing a much larger file than necessary. It will also mess up the ID for user 0, effectively removing the anonymous user record (I just figured that part out the hard way). I've posted my scripts, which have had a lot of exercise, along with some explanation at http://industriousone.com/drupal-backup-and-restore. Feel free to copy and modify.

hostname

digitalramble - June 26, 2009 - 19:53

Some mysql users will need to add the -h hostname option, if their mysql db is hosted on another server, as is very often the case with webhosting sites...

eg

mysqldump -h hostname -u username -p databasename > drupal_db_backup.sql

And restoration is best done with specific tables, not by shoving the whole thing back in, but I see a previous comment addressed that. It's for this reason I specify a prefix for my drupal installs.

 
 

Drupal is a registered trademark of Dries Buytaert.