05/02/2022

2022/02/05#p1
0 comments: click to read or leave your own

Our web host went down for a couple of hours last night, you couldn't even get to their own site so it must have been a serious issue. This got me worrying and wondering.

I have a full backup of the site files both locally on my Mac and in a private repository on GitHub but, were anything to happen with the database, I would currently rely on their nightly backups should I need to recover anything.

But what if they went down and couldn't come back up? What if the backups were corrupted or went missing? 14 years of data would be gone, just like that. What if I could automatically backup the database to online storage such as Dropbox?

I couldn't get to sleep so, in the early hours, a quick search found this and I set about creating my own solution for automated backups.

My host has disabled the exec() command so I couldn't just cut and paste the solution, resorting to cron jobs for mysqldump and compressing the sql file. As my database is small I also removed the functions for uploading the compressed file in chunks as they're not needed.

I created a Dropbox app to access the API but, despite things being considerably simpler than the original example, errors were being thrown meaning I had to rewrite parts of the code to get it to work.

And it does!

I now have three cron jobs that run in sequence:

  • dump the database to a .sql file
  • compress that to a .tgz file
  • run the php to upload the compressed file to Dropbox

Now, even if the host has a catastrophic failure or data is unrecoverable from their backups, I'll be able to restore the site to within the last couple of posts which I'll be able to recreate from the RSS feed if needed.

cancel media
Leave a reply



Cancel comment

You can also:

Reply by email Reply by email Send a webmention Send a Webmention

2022/02/05#p2
0 comments: click to read or leave your own

Next up will be learning to use OAuth so that I can have short-lived access tokens for extra security.

cancel media
Leave a reply



Cancel comment

You can also:

Reply by email Reply by email Send a webmention Send a Webmention

2022/02/05#p3
0 comments: click to read or leave your own

Well, that was fun!

I was trying to do all of the Oauth for Dropbox via code and just couldn't get it working. By chance, I then stumbled across a quick demo explaining that it's easier to get your refresh_token from the command line as it will remain the same and be reused.

Once I had this it was easy enough to access the Dropbox token endpoint via code each time to get the refreshed, short-lived access token. After the backup file is uploaded I then immediately revoke the token to make things even more secure.

👍

cancel media
Leave a reply



Cancel comment

You can also:

Reply by email Reply by email Send a webmention Send a Webmention

2022/02/05#p4
0 comments: click to read or leave your own

On a related note, this is really handy to convert command line curl to PHP.

cancel media
Leave a reply



Cancel comment

You can also:

Reply by email Reply by email Send a webmention Send a Webmention

Colin Walker Colin Walker colin@colinwalker.blog