Saturday, January 31, 2009

Backing up Drupal Quickly

I must admit, updating a Drupal instance, even if it's one module is a bit scary. And a rule of thumb is: backup, backup and backup the backup. So I do it.

Below I post a script that I wrote for that purpose (my first script for Linux :D). The folder structure on my server is:

The Script

today=$(date +"%Y-%m-%d")

cd ~/
mkdir $backups/$today


echo -e "TODAY: $today\n"

# 1.
echo "---Backing up Drupal 6.x without uploaded files---"

tar --create --gzip --file=$backups/$today/drupal-$drupal_v-no-files.tgz --exclude='sites/default/files' \
--directory=public_html drupal-6.x

echo -e "DONE\n"

# 2.
echo "---Backing up Drupal 6.x uploaded files---"

tar --create --gzip --file=$backups/$today/files.tgz --directory=public_html/drupal-6.x/sites/default files

echo -e "DONE\n"

echo "---Dumping the database---"

/usr/local/mysql/bin/mysqldump -u YOU -p --default-character-set=utf8 --result-file=$backups/$today/dump.sql DB
tar --create --gzip --file=$backups/$today/dump.sql.tgz --directory=$backups/$today dump.sql

echo -e "DONE\n"

echo "---Copying the settings---"

cp public_html/drupal-6.x/sites/default/settings.php public_html/drupal-6.x/.htaccess $backups/$today

echo -e "DONE\n"

echo "FINISHED. Please move the backup files to a safe location"