If you are using Lar­avel Forge for you serv­er pro­vi­sion­ing and man­age­ment you might have noticed that there is a built-in back­up fea­ture that makes it easy to take data­base back­ups for your web­sites and apps.

Screenshot 2020 08 21 at 17 07 02 image

We use these Forge back­ups specif­i­cal­ly to pre­vent against bad deployments. 

In the past we’ve come into sit­u­a­tions where a new deploy­ment has bro­ken our Craft install leav­ing us unable to restore via the Craft com­mand line. In these sit­u­a­tions it’s good to be able to quick­ly revert to an ear­li­er back­up via the Forge dash­board as opposed to stress­ing out over mysql or psql restore commands.

Bear in mind we keep sep­a­rate, long-term back­ups of both the data­base and asset fold­ers using craft-remote-backup

One approach here is to either keep a set amount of hourly back­ups or just take a man­u­al back­up as part of our deploy­ment process.

This isn’t ide­al . We might for­get to man­u­al­ly back­up or our hourly back­ups might not be suf­fi­cient if we’ve been mak­ing a lot of changes to the site before deployment.

A bet­ter approach would be to auto­mat­i­cal­ly cre­ate a new Forge back­up when deploying.

Auto­mat­ic Deploy­ment Backups

Luck­i­ly, Forge has an API we can hook into to do exact­ly that. Let’s use this API along­side our deploy­ment script to cre­ate an auto­mat­ic data­base back­up on every new deployment.

Cre­ate API Access Token

First, cre­ate a new Forge API access token. Go to your avatar in the top right and hit Account fol­lowed by API:

Screenshot 2020 08 21 at 17 13 44 image

Give your token a name and hit cre­ate. Make sure to take down the token now, as you won’t be able to see it again.

Cre­ate Back­up Configuration

Now cre­ate a back­up con­fig­u­ra­tion. Go to your serv­er and hit Backup in the side­bar. Enter your back­up details. 

Screenshot 2020 08 21 at 17 00 25 image

For fre­quen­cy select Cus­tom”. Unfor­tu­nate­ly you have to give Forge some sort of cron sched­ule. In oth­er words, there’s no way to say don’t back­up unless I do it man­u­al­ly”. So we usu­al­ly cre­ate a cron sched­ule of 0 0 * * 0 which is once a week. 

Cre­ate Your API Request

Now we need to gen­er­ate the curl request that we’ll make to the API.

We need:

  • Serv­er ID
  • Back­up ID
  • API access token
  • API end­point

You can find the serv­er ID down the bot­tom of the dashboard:

Screenshot 2020 08 21 at 16 49 06 image

And you’ll find the back­up ID on the back­up page:

Screenshot 2020 08 21 at 17 08 19 image

The API end­point we want to use is the run back­up con­fig­u­ra­tion” endpoint.

So let’s use all this to cre­ate a sim­ple one line script on our server:

#!/bin/bash

curl -X POST -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer [API ACCESS TOKEN]" https://forge.laravel.com/api/v1/servers/[SERVER ID]/backup-configs/[BACKUP ID]

~/deployment-backup.sh

Now add this to your deploy­ment script in Forge:

Screenshot 2020 08 21 at 17 03 22 image

And you’re good to go. Now every time you make a new deploy­ment with Forge you’ll have a fresh back­up of your data­base to revert to if things go wrong.

If you made it this far, you may also like

Craft Remote Backup

Tech chat default image

Avoid that perilous sinking feeling as you wonder "did I backup?". Install our new plugin, Craft Remote Backup