drush jenkins terminal screenshot

Backup Your Drupal 8 Database to S3 with Drush & Jenkins

There are many different ways to handle offsite database backups for your Drupal sites. From host provider automations to contrib modules like Backup and Migrate and everywhere in between. This week, I was looking to automate this process on a Drupal 8 site. Since Backup and Migrate is being rewritten from the ground up for Drupal 8, I decided to whip up a custom shell script using Drush.

I knew I wanted my backups to not only be automated, but to be uploaded somewhere offsite. Since we already had access to an S3 account, I decided to use that as my offsite location. After doing a bit of Googling, I discovered s3cmd, a rather nifty command line tool for interacting with Amazon S3. From their README.md:

S3cmd (s3cmd) is a free command line tool and client for uploading, retrieving and managing data in Amazon S3 and other cloud storage service providers that use the S3 protocol, such as Google Cloud Storage or DreamHost DreamObjects. It is best suited for power users who are familiar with command line programs. It is also ideal for batch scripts and automated backup to S3, triggered from cron, etc.

It works like a charm and basically does all of the heavy lifting needed to interact with S3 files. After installing and setting it up on my Drupal 8 project's server, I was able to easily upload a file like so: s3cmd put someDatabase.sql.gz s3://myBucket/someDatabase.sql.gz.

With that bit sorted, it was really just a matter of tying it together with Drush's sql-dump command. Here's the script I ended up with:

  
    # Switch to the docroot.
    cd /var/www/yourProject/docroot/

    # Backup the database.
    drush sql-dump --gzip --result-file=/home/yourJenkinsUser/db-backups/yourProject-`date +%F-%T`.sql.gz

    # Switch to the backups directory.
    cd /home/yourJenkinsUser/db-backups/

    # Store the recently created db's filename as a variable.
    database=$(ls -t | head -n1)

    # Upload to Amazon S3, using s3cmd (https://github.com/s3tools/s3cmd).
    s3cmd put $database s3://yourBucketName/$database

    # Delete databases older than 10 days.
    find /home/yourJenkinsUser/db-backups/ -mtime +10 -type f -delete
  

With the script working, I created a simple Jenkins job to run it nightly, (with Slack notifications of course) and voilà: automated offsite database backups with Jenkins and Drush!

Drupal Drupal 8 Jenkins Drush
Chris Free Headshot

Chris splits his time between helping Mark and Dave run Chromatic and providing technical consulting to our largest clients.