Skip to content

Instantly share code, notes, and snippets.

@pedantix
Created July 31, 2018 19:08
Show Gist options
  • Save pedantix/af2527f3d116c2ef54c946f12ae7c643 to your computer and use it in GitHub Desktop.
Save pedantix/af2527f3d116c2ef54c946f12ae7c643 to your computer and use it in GitHub Desktop.
#!/usr/bin/swift
// Usage Example:
// ./PostgresDbToLocal.swift vgsm-production-api very_good_sports_map_api shaunhubbard
import Foundation
extension Process {
public static func shell(command: String) -> String {
return Process().shell(command: command)
}
@discardableResult
public func shell(command: String) -> String {
launchPath = "/bin/bash"
arguments = ["-c", command]
let outputPipe = Pipe()
standardOutput = outputPipe
launch()
let data = outputPipe.fileHandleForReading.readDataToEndOfFile()
guard let outputData = String(data: data, encoding: String.Encoding.utf8) else { return "" }
return outputData
}
}
guard CommandLine.argc == 4 else {
print("Error pass three arguments for the script")
print(" First: The name of the heroku app to pull the database from")
print(" Second: The name of the database to restore the backup too")
print(" Third: The name of the database user")
exit(1)
}
let herokuAppName = CommandLine.arguments[1]
let databaseName = CommandLine.arguments[2]
let databaseUser = CommandLine.arguments[3]
print("Backing up and restoring the heroku db for \(herokuAppName)")
_ = Process.shell(command:"heroku pg:backups:capture --app \(herokuAppName)")
print("Downloading the backup!")
_ = Process.shell(command: "heroku pg:backups:download --app \(herokuAppName)")
print("Resotoring the database")
_ = Process.shell(command: "pg_restore --verbose --clean --no-acl --no-owner -h localhost -U \(databaseUser) -d \(databaseName) latest.dump")
print("Removing Backup")
_ = Process.shell(command: "rm latest.dump")
exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment