Congrats, you just released your very first CraftCMS site. Great! You're done!
Wait... you forgot to setup backups! Of course, some hosting providers take their own database snapshots; but often they are inaccessible except in case of catastrophic issues. What if you just want to jump back a day or week and know what's going on.
Well fortunately for us, it's not difficult to setup database backups on our own.
--
To setup backups there are three things you'll need: 1) Some type of CRON system; 2) A computing server that will run shell scripts; 3) A storage location.
In my case I'm going to use Rundeck for CRON, a standard Linux server for compute, and Amazon S3 for storage. You can adapt from there, however.
Storage Location
We're going to start at the end and work backwards here. If you're using S3, jump into the AWS Console and create a new bucket for CraftCMS backups.
Be sure to keep it as a private bucket or else anyone could access your database backups of potentially private information.
You may also want to enable Glacier as well to limit your costs. I typically push backups to glacier after a week.
Compute Server
Now let's write your script! I've posted mine here for you to copy if you'd like. The main point here is to SSH into the location and export the database, save it locally, then push it to S3.
If you're using my script - you'll need to update some variables to work for you.
This script leverages the AWS CLI, which greatly simplifies this process. You could do this via API as well, but if you can use the CLI I highly recommend it. You could also store your backups on a mounted network drive wherever your compute server lives as well.
The SSH User & Host will be easy to find in the Platform.sh GUI. Once you SSH in you can echo $PLATFORM_RELATIONSHIPS | base64 --decode | json_pp
to see your database credentials.
CRON Setup
Finally, let's trigger the backups at the frequency you choose. I would recommend once a day as a default, increasing or decreasing depending on publication frequency.
In my case I used Rundeck because I already use it for heaps of other things. However, if you don't have Rundeck installed you could just use the crontab. Either way, just set it up to hit your backup shell script:
## In crontab, each night at midnight:
0 0 * * * sh /var/scripts/craft_backups/my_script.sh
And that's it! You've got stable backups stored in S3 for easy access anytime you need.
Top comments (0)