-
Notifications
You must be signed in to change notification settings - Fork 9
pgbackups
HerokuBackups アドオンを使うと Heroku の Postgres データベースのバックアップを簡単にキャプチャーし、管理できます。
アドオンをインストールします:
:::term
$ heroku addons:add pgbackups
Adding pgbackups to myapp... done
PGBackups にはバックアップの自動化を含むなどのいくつかのプランがあります。 プランのリストは PGBackups Add-on page を参照してください。
These plans allow you to manually capture and manage backups and restores. They differ in how many backups you can manage.
To install, run heroku addons:add pgbackups:basic
These plans also allow manual capture, but also take a backup each night of whichever database is pointed to in your application's DATABASE_URL
config var.
They differ in how long the auto backups are retained. The auto-week plan retains 7 days of backups. The auto-month plan also keeps 7 days, and then 5 weekly backups after.
To install, run heroku addons:add pgbackups:auto-week
Automatic backups cannot be destroyed.
Backups are compressed. The size of a backup will be less than the size of your database.
Capture a backup of your primary database:
:::term
$ heroku pgbackups:capture
HEROKU_POSTGRESQL_BLACK (DATABASE_URL) ----backup---> b251
Capturing... done
Storing... done
If you have multiple databases on your application, you can choose which one to backup by specifying a database:
:::term
$ heroku pgbackups:capture HEROKU_POSTGRESQL_PINK
HEROKU_POSTGRESQL_PINK ----backup---> b252
Capturing... done
Storing... done
You can automatically delete your oldest backup when capturing a new one:
:::term
$ heroku pgbackups:capture --expire
You can get a summary list of all your backups. Manually taken backups start with a 'b', while automatically taken backups start with an 'a'.
:::term
$ heroku pgbackups
ID | Backup Time | Size | Database
-----+---------------------+-------+------------------------
a226 | 2012/02/22 20:02.19 | 5.3KB | DATABASE_URL
a227 | 2012/02/23 20:02.19 | 5.3KB | DATABASE_URL
b251 | 2012/02/24 16:08.02 | 5.3KB | HEROKU_POSTGRESQL_BLACK
b252 | 2012/02/24 16:08.53 | 5.3KB | HEROKU_POSTGRESQL_PINK
You can create a publicly accessible URL for backups, which is available for 10 minutes. This can be used for migrating or exporting your database:
:::term
$ heroku pgbackups:url b004
"http://s3.amazonaws.com/hkpgbackups/[email protected]/b004.dump?AWSAccessKeyId=ABCD1234&Expires=1289261668&Signature=3mMBeKISewgEUDT%2FL5mRz4EYS4M%3D"
You can delete a backup. Depending on your plan you will need to delete obsolete backups manually:
:::term
$ heroku pgbackups:destroy b003
Backup b003 will be permanently deleted Are you sure (y/N)? y
Backup b003 deleted.
You can restore a backup into a database. This is a destructive operation: the restore operation will drop existing data and replace it with the contents of the backup. The contents of the database prior to a restore will not be recoverable. To restore backup b251 into the database at DATABASE_URL:
:::term
$ heroku pgbackups:restore DATABASE b251
HEROKU_POSTGRESQL_BLACK (DATABASE_URL) <---restore--- b251
HEROKU_POSTGRESQL_BLACK
2012/02/24 16:08.02
5.3KB
To restore that backup to the database at HEROKU_POSTGRESQL_PINK_URL instead, the operation would be:
:::term
$ heroku pgbackups:restore HEROKU_POSTGRESQL_PINK b251
HEROKU_POSTGRESQL_PINK <---restore--- b251
HEROKU_POSTGRESQL_BLACK
2012/02/24 16:08.02
5.3KB
If you have an existing PostgreSQL database you'd like to export from elsewhere, dump it in compressed format using the open source pg_dump tool:
:::term
$ PGPASSWORD=mypassword pg_dump -Fc --no-acl --no-owner -h myhost -U myuser mydb > mydb.dump
Upload it somewhere with an HTTP accessible URL. We recommend using Amazon S3 and S3Fox. Create the file with private access and create a temporary authorized URL for the Heroku import. Then cut and paste that URL into your restore command:
:::term
$ heroku pgbackups:restore DATABASE 'http://s3.amazonaws.com/.....mydb.dump?authparameters'
If you'd like to move your database out of the Heroku Postgres service, take a backup and then download it using a variety of tools such as curl
, wget
, or a web browser. For example:
:::term
$ curl -o latest.dump `heroku pgbackups:url`
Note that the backup URLs will expire 10 minutes after they are issued.
You can then load this dump into your local database using the pg_restore tool, just as Heroku does when you initiate a restore:
:::term
$ pg_restore --verbose --clean --no-acl --no-owner -h myhost -U myuser -d mydb latest.dump
You can use pgbackups to transfer data from one app to another, say from your production app to your staging app. To do this, capture a backup on the primary app:
:::term
$ heroku pgbackups:capture --app myapp
Now retrieve the URL of the backup, and import from that backup into the secondary app:
$ heroku pgbackups:restore DATABASE `heroku pgbackups:url --app myapp` --app myapp-staging
Note that the restore is going into the database at DATABASE_URL of myapp-staging, but using the URL of the latest backup from myapp.
PGBackups splits very large backups into multiple files. When this happens, heroku pgbackups:url
will display two or more separate files窶俳ne for each part.
:::term
$ heroku pgbackups:url
"https://s3.amazonaws.com/hkpgbackups/[email protected]/a292.dump.aa?AWSAccessKeyId=ABCetc
https://s3.amazonaws.com/hkpgbackups/[email protected]/a292.dump.ab?AWSAccessKeyId=ABCetc"
These files were created with the unix split
utility, and must be joined together before use.
:::term
$ cat a292.dump.aa a292.dump.ab > a292.dump