Ruby Backup Class

From Rory.wiki

Jump to: navigation, search

Usage

This is a quick class I knocked up to backup directories and the databases from my web server an mail them off to a gmail account. It works ok so long as each archive is under 1.5MB (gmail objects to larger mails from what I can see)


To use it just start a new instance of the class and pick some directories to back up.

eg,

back = Rbackup.new('/tmp/backups/','fromaddress@somedomain.com','toaddress@gmail.com')
 
back.backup('/var/www','var-www.tar.bz2')
back.databaseBackup('databases-file-name','databaseUser','databasePass')


Code

#!/usr/bin/ruby1.8
 
require 'logger'
require 'rubygems'
require 'mail'
 
 
class Rbackup
  def initialize(outputDir, mail_sender, mail_recipient)
    if !File.exists?(outputDir) || (File.exists?(outputDir) && !File.stat(outputDir).directory?)
      unless Dir.mkdir(outputDir)  
        puts "Can't Create Output directory check permissions"
        exit 
      end
    end
 
    unless Dir.chdir(outputDir)
      puts "Can't change directory to output directory "
      exit
    end
    #It's here 'cause it's got to be after the change in dir.
    @backupLog = Logger.new('backup-log.txt', 'daily')
    @backupLog.level = Logger::DEBUG
 
    @mail_sender = mail_sender
    @mail_recipient = mail_recipient
  end
 
  def backup(directory,filename)
    if File.exists?(filename + '.backup')
      File.delete(filename + '.backup')
      @backupLog.info("deleted backup file #{filename + '.backup'}")
    end
 
    if File.exists?(filename)
      File.rename(filename, filename + '.backup')
      @backupLog.info("renamed backup file #{filename}")
    end
    @backupLog.debug("got here ok before the kernel command")
    unless %x[/bin/tar -cjvf #{filename} #{directory}]
     @backupLog.warn("Error creating backup file for #{directory} " + $?) 
     exit
    end
    @backupLog.info("successfully backed up #{directory} to #{filename}")
    mailBackup(filename)
  end
 
  def databaseBackup(filename,username,password)
    if File.exists?(filename + '.backup')
      File.delete(filename + '.backup')
      @backupLog.info("deleted backup file #{filename + '.backup'}")
    end
 
    if File.exists?(filename + '.bz2')
      File.delete( filename + '.bz2')
      @backupLog.info("deleted old archive file")
    end    
 
    if File.exists?(filename)
      File.rename(filename, filename + '.backup')
      @backupLog.info("renamed backup file #{filename}")
    end
 
    unless Kernel.system("/usr/bin/mysqldump --all-databases -u #{username} --password=#{password} > #{filename}")
     @backupLog.warn("Error creating database backup file") 
     exit
    end
 
 
    unless Kernel.system("/bin/bzip2 #{filename}")
      @backupLog.warn("Error zipping databases")
      exit
    end
    @backupLog.info("successfully backed up databases to #{filename}")
    mailBackup(filename + '.bz2')
  end
 
 
  def mailBackup(filename)
    @backupLog.info("Started e-mailing the files")
    Mail.defaults do
      smtp 'gmail-smtp-in.l.google.com', 25
    end
 
    mail = Mail.new 
    mail.from = @mail_sender
    mail.to = @mail_recipient
    mail.subject = "backup of #{filename}"
    mail.body = File.read('backup-log.txt')
    mail.add_file filename
 
    mail.deliver!
    @backupLog.info("finished e-mailing the file #{filename}")
  end
 
 
end
Personal tools