Guest User

Untitled

a guest
Feb 28th, 2018
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. # 0) Setup
  2. $DATE_FORMAT = "%Y%m%d"
  3. $BACKUP_FILENAME = "mysecret-database-daily-#{Time.now.strftime($DATE_FORMAT)}.sql.gz"
  4. $BACKUP_LOCATION = "/mnt/backups/"
  5. $USERNAME = "ninja"
  6. $PASSWORD = "pirate"
  7. $DATABASE = "japanese_boat"
  8. $MAIL_FROM = "aaarrrgh@brightlight-ict.nl"
  9. $MAIL_TO = "maran@brightlight-ict.nl"
  10.  
  11. mailer = SimpleMailer.new($MAIL_FROM, $MAIL_TO)
  12.  
  13. # 1) Get backup from somewhere
  14. unless system("cp #{$BACKUP_LOCATION}#{$BACKUP_FILENAME} ./ ")
  15. mailer.sad_mail "Something went wrong while copying the backup, exiting..."
  16. exit
  17. end
  18.  
  19. # 2) Dump old tables
  20. unless system("mysql --silent --skip-column-names -u #{$USERNAME} --password=\"#{$PASSWORD}\" #{$DATABASE} -e \"show tables\" | gawk '{print \"drop table \" $1 \";\"}' | mysql -u #{$USERNAME} --password=\"#{$PASSWORD}\" #{$DATABASE}")
  21. mailer.sad_mail "Couldn't drop tables, exitings.."
  22. exit
  23. end
  24.  
  25. # 3) Import new database
  26. unless system("zcat #{$BACKUP_FILENAME} | mysql -u #{$USERNAME} --password=#{$PASSWORD} #{$DATABASE}")
  27. mailer.sad_mail "Couldn't import tables, exiting.."
  28. exit
  29. end
  30.  
  31. # 4) Everything went a-ok!
  32. mailer.happy_mail
Add Comment
Please, Sign In to add comment