Backup your Bluehost server remotely with Ansible

tl;dr I created a simple Ansible playbook to dump databases and folders from a Bluehost shared hosting, ideal for automating backups using a cron job, you can get the source code for this playbook here.

After all the incidents with the Heartbleed bug and security issues, I  decided to audit my servers to spot security flaws, some days ago I started redirecting all the system and application logs to a simple service to centralise all your logs and create alerts, search, store them, etc. After that I started thinking in a better way to secure my information and automate backups of multiple sites and databases I have a across multiple hosting providers.

One of those providers is Bluehost, a very popular shared hosting service. If you have some experience with shared hosting you will know there are things that simply are not possible to do in contrast with a VPS or dedicated server. Nevertheless there is a lot you can do if your provider let you access via ssh. In this post I’ll be showing you how to automate backups using Ansible a simple but powerful tool for managing and automate infrastructure. If you don’t have or dont know how to access Bluehost via ssh, read this guide.

The Bash way

The first thing that came to my mind before Ansible was a bash script a simple script that would be triggered by a cron job in my home server and use mysql-dump via SCP to dump and copy databases backups into my home server and  then rsync to maintain files and folders synced.

The method was fairly simple, but after being working with Ansible for some months I really wanted to create something simpler and cleaner, so after translating that bash script into Ansible  I ended up with this playbook:

If you are curious about the bash script, here is it (it doesn’t have the rsync part for folders, just mysql dump):

Finally if you want to schedule your backups, you can easily trigger this playbook from a cron job like this (put this in the server that will store the backups):

00 1 * * 0    cd /path/to/playbook; ansible-playbook -i hosts bluehostBackup.yml --tags databases folders

Here you can find the full repository:

Let me know what you think in the comments below, and if you have a better ideas to improve it.


Also read...