Skip to content

Instantly share code, notes, and snippets.

@jvenezia
Last active August 16, 2019 03:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jvenezia/b39f7ac4f6067436e043 to your computer and use it in GitHub Desktop.
Save jvenezia/b39f7ac4f6067436e043 to your computer and use it in GitHub Desktop.
Migrating an Heroku Application to another

Migrating an Heroku Application to another

The usecase here is to migrate an Heroku Application to Heroku Europe, and minimise downtime.

The heroku documentation on this subject is quite complete.
It is recommanded to read it if you are planning a migration.
https://devcenter.heroku.com/articles/app-migration

Process

In this guide, sourceapp is the original (source) app and targetapp is the migrated (target) app.

Prepare the new environment

  • Fork sourceapp
heroku fork -a sourceapp targetapp --region eu

If you don't need to change region, just skip the --region eu option.

  • Check that your Heroku add-ons and their plans are correctly forked.
  • Check the targetapp environement variables. You may have to change some of them if you have variables specific to your environment. Variables from Heroku add-ons should have migrate correctly.
  • If you use the Heroku Websolr add-on, you have to set it manually.
    Go to the Websolr dashboard:
heroku addons:open websolr

And set the index type according to your needs. Then update the schema.xml file if you need so.

If you are using the sunspot gem, reindex solr to check if everything works.

heroku run rake sunspot:solr:reindex -a targetapp
  • Now everything is ready, check that the targetapp is going well!

  • If you are using Heroku Scheduler, you will have to migrate your tasks manually in the targetapp. Be sure to do this migration at the right moment, to avoid killing a task, or running a task on both applications.

Migrate your application

  • Enable maintenance mode on both applications
heroku maintenance:on -a sourceapp
heroku maintenance:on -a targetapp
  • Update your DNS settings from your DNS provider, and move Domains in the Heroku dashboard from the sourceapp to the targetapp.

Note that you may have to lower your DNS TTL before, to minimize downtime when changing your records.

  • Backup the sourceapp database
  heroku pgbackups:capture -a sourceapp
  • Load this backup in the targetapp
heroku pgbackups:restore DATABASE `heroku pgbackups:url -a sourceapp` -a targetapp -c targetapp

Note that the DATABASE option may change according to your settings. The -c options skips the confirmation prompt.

  • If you need to, use the targetapp database in the sourceapp application. It ensures you that if some people access the sourceapp for any reason, they will use the database of the new application.
heroku config:set DATABASE_URL="`heroku config -a targetapp |grep DATABASE_URL |awk '{print $2}'`" -a sourceapp

This command line sets the sourceapp DATABASE_URL environment variable with the targetapps value.

  • Restart an disable maintenance of both apps.
heroku restart -a sourceapp
heroku restart -a targetapp
heroku maintenance:off -a sourceapp
heroku maintenance:off -a targetapp

And you're done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment