DEV Community

@kon_yu
@kon_yu

Posted on

Daily backup of production staging using a Docker container for Heroku maintenance that can be deployed to Heroku

Heroku Advent Calendar 2016 Day 6 article. I've been writing Heroku's Advent Calendar for about three years in a row, no matter what.

Introduction.

I was inspired by this article by Crowdworks.

Things to do when running a production service in Heroku & configuration examples

I created a Docker container that can execute the following shell commands, which I did in the above article Adding a preview environment that can synchronize data with production.

  • Copy Heroku Postgres from the Production environment to the Staging environment
  • Mask personal information from the copied DB
  • Synchronize the Puroduction environment bucket in AWS S3 to the Staging environment bucket

I'm deploying this Docker container to Heroku.

As you can see, Heroku has a mechanism to deploy Docker containers, which is not well known, but it is actually made before you know it.
https://devcenter.heroku.com/articles/container-registry-and-runtime

In order to run the backup command in heroku or the CLI in AWS, as in this case
Rather than making an existing build pack (or rather, a build pack that could do something about it, but I haven't looked it up)
I think it's more convenient to create Docker container because it's easier to check the operation on the development machine.

In the case of Docker container, if you can deploy Docker container other than Heroku, you can build it in any cloud environment, even in a suitable internal server, I think it is a good point.

Remote Repository

Github.

https://github.com/konyu/heroku-aws-backup

Docker hub

https://hub.docker.com/r/konyu/heroku-aws-backup/

How to get an API key for AWS and Heroku

Obtain the AWS and Heroku API keys in advance and make a note of them

The URLs for each method are as follows

AWS access key and secret key

http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSGettingStartedGuide/AWSCredentials.html

Heroku api token

https://devcenter.heroku.com/articles/authentication#api-token-storage

How to build in the local environment

git@github.com:konyu/heroku-aws-backup.git
cd heroku-aws-backup
docker build --rm -t heroku-aws-backup .
Enter fullscreen mode Exit fullscreen mode

Local startup method

Start with AWS and Heroku environment variables in --env

docker run -it --rm --env AWS_ACCESS_KEY_ID=xxxxxxx\fnDroid
--env AWS_SECRET_ACCESS_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
--env AWS_DEFAULT_REGION=ap-northeast-1
--env HEROKU_API_KEY=xxxxxxxxxxxxxxxxxxxxx
heroku-aws-backup bash
Enter fullscreen mode Exit fullscreen mode

Backup shell file modifications

If you're using a cloned Github repository as I write here, you can use

There is a sample file for backup in sh/backup.sh, so you can rewrite it to suit your environment.

It would be good to make sh/backup.sh and rewrite the shell file appropriately when you write it in the From of Dockerfile and use it.

https://github.com/konyu/heroku-aws-backup/blob/master/sh/backup.sh.

# sample backup shell
# echo "start daily buckup!!!"

# echo "== stopping worker =="
# heroku ps:scale worker=0 -a APP_BE_BAKED_UP

# echo "== copying database to preview from production =="
# heroku pg:copy ORIGINAL_DB_NAME DB_TO_BE_COPIED --confirm APP_BE_BAKED_UP -a APP_BE_BAKED_UP

# echo "== clearing jobs =="
# heroku run rake jobs:clear -a APP_BE_BAKED_UP

# echo "== copying s3 bucket to staging from production =="
# aws s3 sync --delete --acl public-read s3://ORIGINAL_BUCKET s3://BUCKET_TO_BE_COPIED

# echo "== starting worker =="
# heroku ps:scale worker=1 -a APP_BE_BAKED_UP
Enter fullscreen mode Exit fullscreen mode

Running in Heroku

Install the Heroku cli.

If you don't have Heroku's CLI command installed, you should be able to use the OS in the following URL

https://devcenter.heroku.com/articles/heroku-command-line

Add Heroku container registry

heroku plugins:install heroku-container-registry
Enter fullscreen mode Exit fullscreen mode

Deploy to Heroku

Move to this repository directory

Log in to Heroku

heroku login
> enter your id and password
Enter fullscreen mode Exit fullscreen mode

Create application(in the case of a new one)

heroku create APP_NAME_YOU_WANT
Enter fullscreen mode Exit fullscreen mode

Modify application (in case of modification)

Added git remote to be able to deploy to heroku

heroku git:remote -a APP_NAME_YOU_WANT
Enter fullscreen mode Exit fullscreen mode

Deploying to Heroku

heroku container:push web
Enter fullscreen mode Exit fullscreen mode

Setting environment variables

Setting environment variables
heroku config:set AWS_ACCESS_KEY_ID=xxxxxxx AWS_SECRET_ACCESS_KEY=xxxxxxxxxxxxxxxxxx AWS_DEFAULT_REGION=ap-northeast-1 HEROKU_API_KEY=xxxxx -app APP_NAME_YOU_WANT
Enter fullscreen mode Exit fullscreen mode

Adding a Scheduler

heroku addons:create scheduler:standard -app APP_NAME_YOU_WANT
Enter fullscreen mode Exit fullscreen mode

Add a shell to the Scheduler to run

The shell to run should be written with the full path.

Note that it doesn't work well if you write the path relative to your home directory when you start with heroku run bash -a APP_NAME.

2016-11-18 11 39 35

Top comments (0)