snapshot
Recipes
GitHub Action

GitHub Action

Scheduled snapshot restoration

Using Snaplet with GitHub Actions is a powerful combination to automate your development infrastructure requirements, like keeping a staging database automatically updated.

In this tutorial we'll show you how to create a GitHub Actions Workflow to capture a snapshot from your source database, and restore it to your target database at 4am every morning.

In order to restore snapshots you'll need:

  1. Setup a source database in Snaplet
  2. You've run the snaplet setup command in your GitHub repository, which created a .snaplet/config.json file. This file associates your repository with your snapshots.
  3. A Snaplet CLI access token (opens in a new tab).
  4. A target database with superuser priviledges and a connection string to that database.

Create a GitHub Actions Workflow

This GitHub Actions Workflow will capture a new snapshot in the cloud at 4am every day. It installs the Snaplet CLI and runs the snaplet snapshot create -y command.

  1. Create a .github/workflows directory in your repository on GitHub if this directory does not already exist.
  2. In the .github/workflows directory, create a file named snaplet-restore.yml.
  3. Copy the following yaml into snaplet-restore.yml:
.github/workflows/snaplet-restore.yml

name: Snaplet Restore
on:
workflow_dispatch:
schedule:
- cron: '0 4 * * 1-5' # At 04:00 on every day-of-week from Monday through Friday.
jobs:
snaplet-restore-snapshot:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Snaplet CLI
run: curl -sL https://app.snaplet.dev/get-cli/ | bash
- name: Restore Snapshot
run: snaplet snapshot create -y
env:
SNAPLET_DATABASE_URL: ${{ secrets.SNAPLET_DATA_TARGET_DB_URL }}

This workflow runs every morning at 4am, and can also be manually triggered (opens in a new tab)

It checks out the repository, in order to access the projectId from .snaplet/config.json, then it installs the Snaplet CLI and runs the snaplet snapshot create command.

Use snaplet snapshot restore -y instead to restore the last captured snapshot rather than creating a new one.

Adding Secrets to GitHub

We're almost done. The last step is to securely add SNAPLET_DATA_TARGET_DB_URL secrets to our GitHub repository.

  1. On GitHub.com, navigate to the main page of your GitHub repository.
  2. Under your repository name, click on Settings.
  3. In the left sidebar, click Secrets.
  4. Click New repository secret.
  5. Type a name for your secret in the Name input box and enter the value for your secret.
  6. Click Add Secret.