Orbit/app/jobs/backup_server.rb

26 lines
956 B
Ruby

class BackupServer
@queue = :high
def self.perform()
dbhost = Mongoid.config.database.connection.primary.join ':'
dbname = Mongoid.config.database.name
archive_db_list_path = OrbitSystemPreference::ArchiveDbListPath
dbdirectory = "#{Rails.root}/tmp/#{dbname}-"+Time.now.strftime("%Y-%m-%d-%H-%M")
%x[mongodump -h #{dbhost} -d #{dbname} -o #{dbdirectory} ]
Dir.foreach('tmp') do |item|
date_str = item.to_s.gsub("#{dbname}-",'')
next if not date_str.match(/\d{4}-\d{2}-\d{2}-\d{2}-\d{2}/)
if Date.parse(date_str).to_time < Site.first.backup_keep_for_days.days.ago
OrbitLogger.info "Deleting #{date_str}"
%x[rm -rf tmp/#{item}]
end
end
%x[rm -f #{archive_db_list_path}]
%x[cd tmp ; ls -l |grep #{dbname} | awk '{print $8}'|xargs du -h --block-size=1M --max-depth=0 |sort -h >> #{archive_db_list_path}]
OrbitLogger.info "DB backup done Path:#{dbdirectory}"
end
end