Created
April 24, 2017 09:39
-
-
Save dux/bb6401b725c33f4a4ecc5838ff0f1247 to your computer and use it in GitHub Desktop.
Postgre backup script, trigger from another server
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
#!/usr/bin/env ruby | |
require 'dotenv' | |
Dir.chdir '/home/deployer/apps/APP' | |
Dotenv.load | |
DB_HOST = 'user@host' | |
DB_NAME = ENV['DB_NAME'] | |
backup = "tmp/#{DB_NAME}-#{`date +'%Y-%m-%d_%H'`.chomp}.sql" | |
puts backup | |
system "sh '#{DB_HOST}' 'pg_dump --dbname=postgresql://#{DB_USER}:#{DB_PASS}@localhost:5432/#{DB_NAME} > #{DB_NAME}.sql'" | |
system "scp #{DB_HOST}:~/#{DB_NAME}.sql '#{backup}'" | |
system "gzip -f #{backup}" | |
backup_size = (File.size("#{backup}.gz").to_f / 2**20).round(2) | |
puts "Backup size in MB: #{backup_size}" | |
# system "aws s3 cp #{backup}" | |
system "aws s3 cp #{backup}.gz db-backups/#{Time.now.year}/#{Time.now.strftime("%m")}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
alternative simple