#!/usr/bin/env ruby # Copyright 2007 JumpBox Inc. All rights reserved. # $Id:$ # run_backup.rb - backs up jumpbox given /jumpbox/state.xml # /appliance/admin/backup/oldbackups # a - keep all # 0 - keep none # 1 - keep one # 5 - keep last five # 10 - keep last ten # 30 - keep last 30 # 90 - keep last 90 # 180 - keep last 180 # 360 - keep last 360 $LOAD_PATH.unshift File.join(File.dirname(__FILE__), "../application_portal/app/models/") $LOAD_PATH.unshift File.join(File.dirname(__FILE__), "../application_portal/privileged_scripts/ubuntu/lib/") $LOAD_PATH.unshift File.join(File.dirname(__FILE__), "../lib") $LOAD_PATH.unshift File.join(File.dirname(__FILE__), "../application_portal/lib/") require 'xmlconf' require 'backup' require 'fileutils' require 'status' include FileUtils require '/jumpbox/lib/apputils' require 'zlib' def get_clean_list(bupath, oldbackups, jbname, jbversion) # TODO: I am only checking for the jumpbox name and not the version # This should be sufficient #bufileprfix="jb-#{jbname}-#{jbversion}" bufileprefix="jb-#{jbname}" bufiles=Dir.glob("#{bupath}/#{bufileprefix}*") case oldbackups when "a" # Keep All Backups [] when "0" # Delete all Old Backups bufiles else # Keep n d = bufiles.length - oldbackups.to_i d = 0 if d < 0 (bufiles.sort { |x, y| x[-18..-5] <=> y[-18..-5] }).first(d) end end begin # load and parse META file app = XmlConf.new(true) jbname = app.get('name') jbversion = app.get('version') oldbackups = app.get('admin/backup/oldbackups') curtime = Time.now bupath = "/var/data/backup" admindbpath = "/var/data/databases" statexml = '/jumpbox/state.xml' rstatexml = '/jumpbox/restored-state.xml' bufile = "#{bupath}/jb-#{jbname}-#{jbversion}-#{curtime.strftime("%Y%m%d%H%M%S")}.tgz" # Start Portmap and NFS-Common if NFS Backup.nfs_init("start") if app.get('admin/backup/mounttype') == 'nfs' # Check Mountpoint if ! Backup.mounted? puts "Backup is not mounted, mounting." `mount #{bupath}` raise "Could not mount backup location" unless $? == 0 end tmppath = `mktemp -d -p #{bupath}`.chomp! raise "Error creating temp dir" unless $? == 0 tmptar = "#{tmppath}/bu.tar" puts tmptar, bufile, tmppath JBUtils.service('mongrel', 'forcestop') cp statexml, rstatexml `tar cf #{tmptar} #{rstatexml} #{admindbpath} 2>&1` raise "tar of #{statexml} failed" unless $? == 0 AppUtils.app_backup({"tmptar" => tmptar}) rmlist = get_clean_list(bupath, oldbackups, jbname, jbversion) `gzip #{tmptar}` FileUtils.mv "#{tmptar}.gz", bufile raise "gzip of #{tmptar} failed" unless File.exists?(bufile) rm(rmlist) JBUtils.service('mongrel', 'start') Status.write_status('success','') rescue Exception => e Status.write_status('error', "general=#{e.message}") sleep 1 if not /^Mongrel is running/ =~ `/etc/init.d/mongrel status 2>/dev/null` JBUtils.service('mongrel', 'start') rescue puts "couldn't start mongrel!!" sleep 1 end if not /^Mongrel is running/ =~ `/etc/init.d/mongrel status 2>/dev/null` f = File.new('/tmp/mongrelfailed', 'w') f.puts "Error: #{e.message}" f.puts "Backtrace:" f.puts e.backtrace.join("\n") f.close end raise ensure rm_rf([rstatexml, tmppath]) # Unmount the backup, also stops portmap and statd in case of NFS Backup.unmount end