Skip to content

Instantly share code, notes, and snippets.

@hcarver
Created October 6, 2016 20:08
Show Gist options
  • Save hcarver/75050b8cf67f5677cc9c67c62def23fa to your computer and use it in GitHub Desktop.
Save hcarver/75050b8cf67f5677cc9c67c62def23fa to your computer and use it in GitHub Desktop.
For migrating between Redis databases on Heroku, when the source database doesn't support the sync Redis command
# For migrating between Redis databases on Heroku, when the source database doesn't support sync
# This is useful for migrating away from Redis Cloud, for example.
require "redis"
# You need to have created a new database first, with
# heroku addons:create heroku-redis:hobby-dev -a $APP
# OR
# heroku addons:create heroku-redis:premium-0 -a $APP
APP = "YOUR_APP".freeze # The Heroku app name
NEW_DB_NAME = "REDIS_URL".freeze # Change this if your new Redis database has a different name
SOURCE_DB = `heroku config:get REDISCLOUD_URL -a #{APP}`.chomp
DEST_DB = `heroku config:get #{NEW_DB_NAME} -a #{APP}`.chomp
source = Redis.new(url: SOURCE_DB)
target = Redis.new(url: DEST_DB)
`heroku ps:scale worker=0 -a #{APP}`
`heroku maintenance:on -a #{APP}`
source.keys("*").each do |key|
data = source.dump key
# puts key, data
target.restore key, 0, data
end
# You might need to change this to be appropriate for you
`heroku config:set REDIS_PROVIDER=#{NEW_DB_NAME} -a #{APP}`
`heroku maintenance:off -a #{APP}`
`heroku ps:scale worker=1 -a #{APP}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment