Backup and migrate module + Dropbox = peace of mind

| Comments

The backup and migrate module is truly sweet. We all know that it is a really good idea to back up data. But if you are like me - you forgot to do it or just don't do it for some lame reason. So backups should be automated. The backup and migrate module does exactly that - it can be configured to run every X hours if you have cron (and you should have) running on your site. The module makes a dump of the database and puts it in a folder in the files folder of the site. Even if you use the public download method, the security is OK - a .htacces file put in the folder by the backup and migrate module and disallows access to the folder's content.

In the unlikely event that my server goes up in flames, I like to have some backups that are not physically on the same server as the site. I really like Dropbox, so I thought I would figure out a way to use that for the database dumps. I found this very nice and simple php script, that will upload files to your dropbox. I used that to write a super simple php file that my cronjob will call once a day. I settled with having daily backups a week back in time, so the filenames are day names. That way monday's file will be overwritten next monday.

Here is what I did:

  1. Enable the backup migrate module
  2. Go to yoursite.com/admin/content/backup_migrate/export and choose gz under compression, put your sitename in Backup file name, and put "D" for dayname under Timestamp format: backup migrate
  3. Download and unzip the latest Dropbox uploader (I used the 1.1.5 version)
  4. Edit the variables capitalized in the php below and save the it to a file called dropbox_backup.php. Put it next to the Dropbox uploader file. The files should be put somewhere outside the drupal installation.
  5. Put a line that in your crontab that calls the php script. It could look like this one, that will run every night at 1 and report to a log file how it went. 00 01 * * * php PATH_TO_SCRIPT/dropbox_backup.php >> PATH_TO_SCRIPT/log.txt And that's it! Easy backup of your database and easy use of the ample storage space that Dropbox offers.
<?php
$site_name = 'SITENAME';
$backup_dir = 'WHERE_YOUR_DB_DUMPS_ARE_PUT_BY_BACKUP_MIGRATE';
$todays_filename = sprintf('%s-%s.sql.gz', $site_name, date('D'));

if (file_exists($backup_dir .'/'. $todays_filename)) {
  // Get todays file from where backup_migrate module puts it
  // and drop it in this folder (just to get it out of the "files"
  // folder in Drupal).
  exec("mv $backup_dir/$todays_filename " . dirname($_SERVER['SCRIPT_FILENAME']));

  // And upload todays file to the Dropbox.
  require 'DropboxUploader.php';
  try {
    $uploader = new DropboxUploader('YOUR_EMAIL', 'YOUR_PASSWORD');
    $uploader->upload(dirname($_SERVER['SCRIPT_NAME']) .'/'. $todays_filename, 'FOLDER_IN_DROPBOX');
    echo date('m/d Y') . ": Uploaded $todays_filename to Dropbox\n";
  }
  catch (Exception $e) {
    echo date('m/d Y') . ": I had a problem uploading $todays_filename to Dropbox\n";
  }
}
?>

Hide Drupal files with .htaccess

| Comments

If one goes to somedrupalsite.com/CHANGELOG.txt - it is normally possible to read the file and see what version of Drupal the site is running. If you don't want everybody to see what version you are running, it is a good idea to cloak the *.txt files in the Drupal install.

Using rewrite rules in .htaccess, the *.txt files in Drupal can be hidden. I use a 403 - forbidden on these files. That is what the [F] means See the Apache documentation on RewriteRule for more options.

# No need for the common visitor to read these files.
# They can just go download their own Drupal. It's
# free and everything.
RewriteRule ^(.*)CHANGELOG\.txt$ - [F]
RewriteRule ^(.*)INSTALL\.mysql\.txt$ - [F]
RewriteRule ^(.*)INSTALL\.txt$ - [F]
RewriteRule ^(.*)MAINTAINERS\.txt$ - [F]
RewriteRule ^(.*)INSTALL\.pgsql\.txt$ - [F]
RewriteRule ^(.*)LICENSE\.txt$ - [F]
RewriteRule ^(.*)UPGRADE\.txt$ - [F]
RewriteRule ^(.*)README\.txt$ - [F]

Put these lines in the bottom of the .htaccess file, but before the </IfModule> "tag". The .htaccess file lives in the root of your Drupal installation.

Using version control?

Metadata folders left by whatever version control system should be hidden too.

# Don't let people pry in version control folders
RewriteRule (^|/)(CVS|\.svn|\.git)/ - [F]

Put the above snippet inside the </IfModule> in .htaccess as well.