Skip to content

Instantly share code, notes, and snippets.

@zedalaye
Last active October 27, 2015 23:16
Show Gist options
  • Save zedalaye/03cefd9ce7339824c610 to your computer and use it in GitHub Desktop.
Save zedalaye/03cefd9ce7339824c610 to your computer and use it in GitHub Desktop.
How to migrate your PostgreSQL Database from Shelly Cloud to Scalingo
1) Make sure you have these tools installed and configured
* Ruby
* The ShellyCloud CLI
* The Scalingo CLI
* tar, gunzip
* The PostgreSQL client tools (pg_restore)
2) Make sure your database on scalingo side is empty (delete your postgresql addon and recreate it)
BEWARE : YOU WILL LOOSE ALL YOUR DATA ! So make backups before deleting your PG Addon !
2) Start the scalingo db-tunnel with : $ scalingo db-tunnel SCALINGO_POSTGRESQL_URL&
(Tip: if you have not forget the "&" at the end of the command, press enter after you see
"You can access your database on '127.0.0.1:xxxxx'" to get the prompt back,
if you forget to type the "&", type CTRL-Z and then "bg" to send the process in background)
3) Copy the script above, make it executable (chmod +x shelly2scalingo.rb)
4) Run it !
$ shelly2scalingo.rb xxxxx <your shelly cloud name> <your scalingo app name>
5) Kill the scalingo tunnel ("ps aux | grep scalingo" and then kill corresponding process id)
#!/usr/bin/env ruby
require 'uri'
if ARGV.length == 3
port = ARGV[0].to_i
cloud = ARGV[1]
app = ARGV[2]
else
puts "Syntax: #{__FILE__} <tunnel port> <shelly cloud name> <scalingo app name>"
puts "Tip: Start tunnel with $ scalingo db-tunnel SCALINGO_POSTGRESQL_URL &"
exit
end
env = `scalingo --app #{app} env`
url = env.split("\n").grep(/^SCALINGO_POSTGRESQL_URL=/)[0].split('=')[1]
uri = URI.parse(url)
db_name = uri.path[1..-1]
puts `shelly --cloud #{cloud} backup get`
f = Dir["*.postgresql.tar"].first
exit unless f
puts "Processing #{f}"
`tar xvf #{f}`
`mv #{cloud}.postgresql/databases/PostgreSQL/#{cloud}.sql.gz #{cloud}.dump.gz`
`rm -rf #{cloud}.postgresql`
`gunzip #{cloud}.dump.gz`
File.unlink(f)
puts `PGPASSWORD=#{uri.password} pg_restore -U #{uri.user} -h 127.0.0.1 -p #{port} -d #{db_name} -O #{cloud}.dump`
File.unlink("#{cloud}.dump")
puts "DONE."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment