DEV Community

Cover image for Backup Rotation Using S3 Bucket with Backroll
Vladimir Vovk
Vladimir Vovk

Posted on Edited on

Backup Rotation Using S3 Bucket with Backroll

I would like to have more granular database backups for my apps. So if something breaks or goes wrong, I will have enough database copies to restore all the data I need.

At least I want an everyday copy or often. But I also don't want to store a lot of old backup files. If something breaks, I will probably notice that within one or two weeks. So I need more granular, everyday backups for two weeks. And then I am fine with one backup per week (for 8 last weeks) and one or two backups per month.

This is called the Grandfather-Father-Son (GFS) backup rotation scheme. It organizes copies into three main tiers — daily, weekly, and monthly — to balance recent recovery points with long-term archiving while keeping storage costs manageable.

To achieve GFS backup rotation, we can use the backroll command-line utility.

bunx backroll --clean db.dump ./backup
Enter fullscreen mode Exit fullscreen mode

backroll command in terminal

It will copy the db.dump into the ./backups folder. The --clean option will remove the original db.dump file. By default, backroll will keep 14 daily backups, 8 weekly (created on every Monday), and 24 monthly (created on the 1st and 15th day). This behaviour could be adjusted with parameters.

Options:
  -v, --version   print version
  -h, --help      print help
  --keep-daily    how many daily files to keep (default 14)
  --keep-weekly   how many weekly files to keep (default 8)
  --day-of-week   day for weekly backup: 0 - Sun, 1 - Mon, ... (default Mon)
  --keep-monthly  how many monthly files to keep (default 24)
  --day-of-month  day for monthly backup (default 1 and 15)
  --clean         remove original files
  --s3            store backups to S3 compatible service
                  (uses env vars: S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY,
                  S3_BUCKET, S3_REGION, S3_ENDPOINT)

Hint:
  Use array to select several days of week: --day-of-week '[1, 3, 5]'
  Or several days for monthly backups: --day-of-month '[1, 10, 20]'
Enter fullscreen mode Exit fullscreen mode

If the database is running with Docker, we can use the backroll Docker image to "roll" backups. For example, we could have a Postgres database running with Docker Compose /srv/myapp/docker-compose.yml:

services:
  postgres:
    image: postgres:18
    container_name: postgres
    restart: unless-stopped
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=password
      - POSTGRES_DB=myapp
    volumes:
      - postgres_data:/var/lib/postgresql
    networks:
      - internal

volumes:
  postgres_data:

networks:
  internal:
    name: myapp_network
Enter fullscreen mode Exit fullscreen mode

In this case, the database backup Docker Compose config /srv/myapp/backup/docker-compose.yml will look like this:

services:
  backup:
    build: .
    image: ghcr.io/vladimir-vovk/backroll:latest
    container_name: backup
    environment:
      - PGDATABASE=dbname
      - PGHOST=postgres 
      - PGPORT=5432
      - PGUSER=postgres
      - PGPASSWORD=password
      - S3_ACCESS_KEY_ID=s3-access-key
      - S3_SECRET_ACCESS_KEY=s3-secret
      - S3_BUCKET=s3-bucket-name
      - S3_ENDPOINT=s3-endpoint
    networks:
      - myapp_network
    volumes:
      - /srv/myapp/backup:/backup
    entrypoint: ["/bin/sh", "-c"]
    command:
      - |
        pg_dump -Fc > /backup/myapp.dump
        backroll --s3 --clean /backup/myapp.dump /backup

networks:
  myapp_network:
    external: true
Enter fullscreen mode Exit fullscreen mode

Now we can add a cron job with the crontab -e command to run a database backup every day at 23:55:

55 23 * * * docker compose -f /srv/myapp/backup/docker-compose.yml run --rm backup >> /srv/myapp/backup/cron.log 2>&1
Enter fullscreen mode Exit fullscreen mode

That's it! 🎉

Credits

Photo by benjamin lehman on Unsplash

Top comments (0)