Guest User

Untitled

a guest
Mar 26th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require 'net/ftp'
  4.  
  5. SERVER = ""
  6. LOGIN = ""
  7. PASSWORD = ""
  8.  
  9. # First, dump the database.
  10. filename = "database-#{Time.now.strftime("%Y-%m-%d-%H")}.sql"
  11. command = "pg_dump 'postgres://user:password@server:port/db' > #{filename}"
  12. result = system(command)
  13.  
  14. # Then save the dump over ftp.
  15. Net::FTP.open(SERVER, LOGIN, PASSWORD) do |ftp|
  16. ftp.putbinaryfile(File.new(filename))
  17. end
  18.  
  19. # Keep 10 backups on the backup server.
  20. Net::FTP.open(SERVER, LOGIN, PASSWORD) do |ftp|
  21. files = ftp.nlst
  22. files.pop(10)
  23. files.each { |f| ftp.delete(f) }
  24. end
  25.  
  26. # Keep 3 backups locally.
  27. files = Dir.glob("database-*")
  28. files.sort!
  29. files.pop(3)
  30. File.unlink(*files)
Add Comment
Please, Sign In to add comment