Giter Club home page Giter Club logo

heroku-tarsnap-backups's Introduction

Heroku Tarsnap Backups

Heroku has built-in backups for Postgres. That's fine if you accidentally delete some data and want to restore it. But what if you forget to update your credit card and Heroku deletes all your data? Or, what if Heroku screw up somehow?

This app lets you make off-site backups of Heroku apps to Tarsnap. It runs as a "sidecar" app alongside another Heroku app. You connect any Postgres databases or Bucketeer S3 buckets you want backed up, then the app will automatically back up everything inside them. It also rotates the backups so you don't spend loads of money storing old ones.

Installing

Clone this repository, then run:

$ heroku create your-app-backup

(Replacing your-app-backup with the name of the app.)

Run these commands to set up the app:

$ heroku stack:set container
$ heroku addons:create scheduler:standard

Deploy the app to Heroku:

$ git push heroku master

Heroku assumes apps have a long-running service. This app only runs with a schedule, so we can turn that off to save money:

$ heroku scale web=0

Configuring

First, set a config variable with the name of the app. This should probably be the same name as the Heroku app, and will be used in the name of the backups on Tarsnap:

$ heroku config:set APP_NAME=your-app-backup

Next, create a Tarsnap account and add some credit.

You then need to generate a Tarsnap key with the Tarsnap client. On Mac, run brew install tarsnap. On other platforms, see these instructions. Then, substituting in your app name and Tarsnap account:

$ tarsnap-keygen --keyfile tarsnap.key --user [email protected] --machine your-app-backup

This will create a tarsnap.key file. Keep this key safe. If you lose this file, you can't recover any of your backups.

Next, upload this key to your Heroku app:

$ heroku config:add TARSNAP_KEY="$(cat tarsnap.key)"

(Note: Even though this key is on Heroku, you must still keep a local copy too. If Heroku have some kind of catastrophic failure, you won't be able to recover your key, therefore you won't be able to recover your backups. This defeats the whole point of having an off-site backup!)

Connecting up your app

You now need to connect the app you want to back up to the sidecar backup app you have created. Heroku lets you connect addons to multiple apps, so that is how we are going to achieve this.

These commands assume the app you want to back up is called your-app and the sidecar backup app is your-app-backup. Substitute as appropriate.

To connect Heroku Postgres:

$ heroku addons:attach -a your-app-backup your-app::DATABASE

To connect Bucketeer S3 buckets:

$ heroku addons:attach -a your-app-backup your-app::BUCKETEER

Testing

To test everything works, run:

$ heroku run bin/backup

It should back up everything you attached. To check it has actually created, run:

$ heroku run bin/list

You should see an archive listed there.

Setting up a schedule

Set up the schedule for your backups:

$ heroku addons:open scheduler

Then, in the web interface, click "Add new job". In the command, enter bin/backup. For the schedule, select either hourly or daily depending on how many backups you want to make, then whatever time you want. Click "Save".

That's it! Your Heroku app is now being backed up.

Restoring backups

You'll need to use the key file you created when setting up the app.

First, list the available archives:

$ tarsnap --keyfile tarsnap.key --list-archives

Then, you can save a tarball of an archive with this command:

$ tarsnap --keyfile tarsnap.key -r -f your-app-backup/some-archive-name > restore.tar

(Note: A bin/restore script does exist in the Heroku app to simplify this, but unfortunately heroku run mangles stdout so we can't get a tarball.)

Configuration reference

TARSNAPPER_DELTAS

Default: 1h 1d 7d 30d 360d 18000d

This option allows you to customise how many backups are kept when they are rotated. The default means keep hourly backups for a day, daily backups for a week, weekly for a month, monthly for a year, yearly forever. See the Tarsnapper docs for more details.

heroku-tarsnap-backups's People

Contributors

bfirsh avatar sebschlo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

heroku-tarsnap-backups's Issues

Was not able to run backup due to error: pg_dump: aborting because of server version mismatch

$ heroku run bin/backup
Running bin/backup on ⬢ [redacted]… up, run.1293 (Free)
>>> Building local Tarsnap cache...
Directory tarsnap-cache created for "--cachedir tarsnap-cache"
Phase 1: Verifying metadata validity
Phase 2: Verifying metadata/metaindex consistency
Phase 3: Reading chunk list
Phase 4: Verifying archive completeness
  Archive 1/21...
  Archive 2/21...
  Archive 3/21...
  Archive 4/21...
  Archive 5/21...
  Archive 6/21...
  Archive 7/21...
  Archive 8/21...
  Archive 9/21...
  Archive 10/21...
  Archive 11/21...
  Archive 12/21...
  Archive 13/21...
  Archive 14/21...
  Archive 15/21...
  Archive 16/21...
  Archive 17/21...
  Archive 18/21...
  Archive 19/21...
  Archive 20/21...
  Archive 21/21...
Phase 5: Identifying unreferenced chunks

>>> Dumping Postgres DB at [redacted]
pg_dump: server version: 13.5 (Ubuntu 13.5-2.pgdg20.04+1); pg_dump version: 10.12
pg_dump: aborting because of server version mismatch

Once more heroku has updated postgres and this stopped working

I'm getting the following error.

pg_dump: error: server version: 14.7 (Ubuntu 14.7-1.pgdg20.04+1); pg_dump version: 13.6
pg_dump: error: aborting because of server version mismatch

In the past, I submitted a PR to update the alpine version of the docker image to one that would have the right version of postgres and pgdump. However, postgres is not even included in the more recent Alpine distributions. So I started looking at using another image, perhaps a debian based one, but I'm seeing that it's non trivial to install tarsnap on debian because you have to add a key and then add an apt repo to your system https://www.tarsnap.com/pkg-deb.html

@bfirsh I'm happy to do this work, but I wanted to get your opinion on the best path forward here, whether to proceed with a debian image, or use something else. Thanks!

Restore command should restore to connected app

The restore command doesn't really work at the moment. What it should do is restore the latest backup to the current connected app, for the use-case where you want to bootstrap an app from backups.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.