Skip to content

Commit

Permalink
Add script to clone heroku repos & data
Browse files Browse the repository at this point in the history
  • Loading branch information
therealadam committed Nov 25, 2022
1 parent a1c79e8 commit 3c0cd2a
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions bin/clone-heroku
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env ruby

require "json"

def heroku_cli_missing?
result = system("heroku auth:whoami >/dev/null", exception: true)

!result
end

def fetch_heroku_apps
IO.popen("heroku apps --json") do |io|
json = io.read
io.close
JSON.parse(json)
end
end

def clone_code(name, git_url)
cmd = "git clone #{git_url} #{name}"

system(cmd)
end

def fetch_backup(name)
capture = "heroku pg:backups:capture --app=#{name}"
download = "heroku pg:backups:download --app=#{name}"
move = "mv latest.dump #{name}/pg-data.dump"
cmd = [capture, download, move].join(" && ")

system(cmd)
end

if __FILE__ == $0
if heroku_cli_missing?
puts "Could not find `heroku` CLI "
puts "$ brew install heroku"
exit -1
end

apps = fetch_heroku_apps
apps.each do |app|
name, git_url = app.values_at("name", "git_url")
puts "app: #{name}"

clone_code(name, git_url)
fetch_backup(name)
end

exit 0
end

0 comments on commit 3c0cd2a

Please sign in to comment.