Skip to content

Instantly share code, notes, and snippets.

@cimm
Created December 10, 2010 22:56

Revisions

  1. cimm created this gist Dec 10, 2010.
    51 changes: 51 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    #!/usr/bin/env ruby

    app = "your-heroku-app"

    STDOUT.sync = true
    start_time = Time.now

    # Fail if the previous dump did not end well
    if File.exist?("#{app}.dump.backup")
    puts "\e[31mThe previous dump did not end well, check manually.\e[0m"
    exit 1
    end

    # Prepare old dump for rotate
    if File.exist?("#{app}.dump")
    File.rename("#{app}.dump", "#{app}.dump.backup")
    end

    # Ask a new dump and overwrite the oldest one
    print "Creating the new Heroku dump\t"
    `heroku pgbackups:capture --expire --app #{app}`
    if $? != 0
    print "[\e[31mFAILED\e[0m]\n"
    exit 1
    else
    print "[\e[32mDONE\e[0m]\n"
    end


    # Download the dump
    print "Downloading the Heroku dump\t"
    `curl -s -o #{app}.dump \`heroku pgbackups:url --app #{app}\``
    if $? == 0
    print "[\e[32mDONE\e[0m]\n"
    else
    print "[\e[31mFAILED\e[0m]\n"
    exit 1
    end

    # Rotate old dump
    print "Rotating the local dump\t\t"
    if File.exist?("#{app}.dump") && File.exist?("#{app}.dump.backup")
    File.delete("#{app}.dump.backup")
    print "[\e[32mDONE\e[0m]\n"
    else
    print "[\e[36mSKIPPED\e[0m]\n"
    end

    runtime = Time.now - start_time
    puts "New Heroku dump for #{app} downloaded in #{runtime} sec."
    exit