Last active
October 11, 2017 01:11
-
-
Save edipofederle/d6082739030b6644cf4329e885204ed5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Simple code to dump a postgres database and upload it to S3. | |
;; Author: Edipo L Federle <[email protected]> | |
;; | |
;; Usage: | |
;; | |
;; Build the jar: lein uberjar | |
;; Run: java -jar target/pg-backup-s3-0.1.0-SNAPSHOT-standalone.jar "db-name" | |
;; | |
;; | |
;; Improvements Needs: | |
;; | |
;; * Extract S3 keys to config file (YML,JSON, clj?) | |
;; * Make possible use of others buckets | |
;; * Make pg_dump command dynamic (more options) | |
;; * Notifications | |
;; * Schedules (CRON) | |
;; * File Compression | |
;; * Rotate Uploaded backups | |
;; | |
(ns pg-backup-s3.core | |
(import java.lang.Runtime) | |
(require [aws.sdk.s3 :as s3] | |
[clojure.java.io :as io]) | |
(:gen-class)) | |
;; Amazon S3 keys | |
(def cred {:access-key "SECRET", | |
:secret-key "SECRET"}) | |
(def now (new java.util.Date)) | |
(def temp-file | |
(-> (java.io.File. | |
(str (System/getProperty "user.home")) "dbtemp.sql") .getAbsolutePath)) | |
(defn cmd-simple | |
"Run a single command. Don't worry about stdout or errors" | |
[command] | |
(. (Runtime/getRuntime) exec command)) | |
(defn sh-seq | |
"Returns a lazy seq of command stdout." | |
[& command+args] | |
(let [process (.exec (Runtime/getRuntime) (apply str (interpose " " command+args)))] | |
(let [reader (clojure.java.io/reader (.getInputStream process) :encoding "UTF-8")] | |
(line-seq reader)))) | |
(defn write-to-file [line] | |
(with-open [wrt (io/writer temp-file)] | |
(.write wrt (str line "\n")))) | |
(defn dump-db | |
[db-name] | |
(let [ content (sh-seq (str "pg_dump --column-inserts --data-only " db-name))] | |
(->> content | |
(interpose \newline) | |
(apply str) | |
(write-to-file)))) | |
(defn delete-file-tmp | |
[] | |
(cmd-simple (str "rm " temp-file))) | |
(defn put-s3 | |
"Upload file to S3" | |
[db-name] | |
(s3/put-object cred "deske" (str db-name "-" now ".sql") (slurp temp-file))) | |
(defn -main | |
[& args] | |
(if args | |
(do | |
(let [db-name (first args)] | |
(dump-db db-name) | |
(put-s3 db-name))) | |
(println "CMD LINE HELP"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment