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}`
@hcarver
Copy link
Author

hcarver commented Jul 10, 2024

Sounds like your source and target databases might be different versions. If that's the case, I'd suggest separating the migration and the version upgrade into two different steps.

@knagode
Copy link

knagode commented Jul 10, 2024

Interesting idea. Thank you ;)

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