I run multiple databases on Fly.io, including MySQL and PostgreSQL databases. Fly.io takes snapshots of these, which is great for restoring your DB in case something goes wrong! However, it doesn’t cover certain scenarios:
- Me after a night out “cleaning up” our company account and removing the whole DB app, thinking it’s not used anymore. Hasn’t happened yet, but it’s not impossible
- The whole Fly.io infrastructure going down in a burning fire. Much less likely, but also not impossible
I like to prepare for the worst case scenario.
I’ve used spatie/laravel-backup before and started implementing that in my apps, as part of the apps themselves:
So in this setup the app itself backs up its own DB.
This works quite well except there’s two disadvantages:
laravel-backuponly works for Laravel apps. I also run some Symfony and Rails apps, I’m left in the dark there.
laravel-backupin every app is kind of a pain. It takes some time to properly configure the S3 credentials, create a scheduler, configure notifications etcetera.
So I wanted to see how easy it would be to make a single app that periodically makes backups of all the databases that are hosted on our Fly account. This way I won’t have to add and configure
laravel-backup on all accounts.
After some playing around with
laravel-backup, I noticed this is quite easy to do. You just have to define a connection in
config('database.connections') for each DB that you want to back up and configure those in
config('backup.source.databases') so you end up with this situation:
- Now you don’t need to add
laravel-backupto every app.
- This also works for other kinds for the DBs backing your Rails or Symfony apps.
If anyone might find this useful, I’ve extracted it to a public repository: GitHub - dejagersh/fly-off-site-backup-manager: Automate creation of off-site backups hosted on Fly.io In the end it’s just a stripped down Laravel app running
- Periodically makes back ups of specified databases
- Writes these to e.g. S3.
- Sends notifications to e.g. Slack on failure/success
- Cleans up backups if they get too old.
Let me know if you have any suggestions or improvements!