Sunday, 06 January 2008

We all know that we should backup a drupal site before performing an upgrade. Drupal.org documentation even explains how to create a backup on http://drupal.org/upgrade/backing-your-site-command-line

I just finished a small bash script which automates this process with a simple usage. From

  1. /var/www

we can now do
  1.  
  2. ./backup.sh urlofwebsite mybackup

This will perform all the operations documented in the bookpage above, and clean up any left-over files afterwards. The result is that in /var/www a new directory
  1. www.krimson.be-backup
is created containing a copy of the www.krimson.be and an extra file
  1. backup.sql.tar.gz

which contains (could you guess?), a gzipped dump of its database.

I've copy pasted the script below. Note, if you do not provide a second argument, a timestamp will be used to 'tag' the backup.

The file is also attached. To use it, Right-click on the attachment, Select Save As, Browse to your

  1. /var/www/

directory and save it as
  1. backup.sh
.
Afterwards you will still need to do a
  1. chmod +x /var/www/backup.sh

to make it executable.

I will try (read: bump me a few times) to commit the script , or pass it over to 'drumm'.
Good luck!

  1. #!/bin/bash
  2.  
  3. # A simple script to create a backup of a drupal site (including it's database)
  4. if [ -z "$1" ]; then
  5. echo "usage: $0 sub.example.com backup-name" >&2
  6. exit 1
  7. fi
  8.  
  9. if [ ! -r "$1" ]; then
  10. echo "$1 is not readable" >&2
  11. exit 1
  12. fi
  13.  
  14. #TODO : figure out howto retrieve the version of drupal
  15.  
  16. if [ -z "$2" ]; then
  17. tag=$(date +%y%m%d_%H%M)
  18. echo "No tag provided, using" $tag "as tag" >&2
  19. else
  20. tag=$2
  21. fi
  22. output=$1-$tag
  23. echo "Back Up '"$1"' to '"$output"'"
  24.  
  25. cd $1
  26. wget -O drupalsqldump.sh "http://cvs.drupal.org/viewvc.py/drupal/contributions/sandbox/drumm/tools/drupalsqldump.sh?view=co"
  27. chmod +x drupalsqldump.sh
  28. #TODO .. use pipe to convert sqldump to tar.gz in single step
  29. ./drupalsqldump.sh sites/default/settings.php > backup.sql
  30. tar -pczf backup.sql.tar.gz backup.sql
  31. rm backup.sql
  32. rm drupalsqldump.sh
  33. cd ..
  34. cp -rf $1 $output
  35. rm $1/backup.sql.tar.gz

Written byRoel De Meester

This Open Source Evangelist is extremely serious. How serious you ask? He obtained a Masters in Quantum Physics (aka stuff only he understands), bailed out of his PhD studies to obtain a Masters in IT instead and finally ended up saying 'yes' when Dries 'Drupal' Buytaert asked him to maintain DRUPAL.be. Known for his communication skills and no-nonsense style (make it work - and keep it running) he comes up with solutions for problems the client has never even thought of. He also provides all Krew members with shoulder taps and weekly massages. After all, Drupal is about sharing, isn't it?

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <h4> <br>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.