Permalink
Browse files

Basic support for scheduled backups (issue 247).

  • Loading branch information...
1 parent 7871433 commit 9782b69e19f24117856c12e129190c8fdc0095f6 @sibprogrammer committed Feb 2, 2013
@@ -45,6 +45,7 @@ def create
end
virtual_server.attributes = params
virtual_server.start_on_boot = params.key?(:start_on_boot)
+ virtual_server.daily_backup = params.key?(:daily_backup)
if virtual_server.save_physically
render :json => { :success => true }
@@ -64,6 +65,7 @@ def load_data
:ip_address => virtual_server.ip_address,
:host_name => virtual_server.host_name,
:start_on_boot => virtual_server.start_on_boot,
+ :daily_backup => virtual_server.daily_backup,
:nameserver => virtual_server.nameserver,
:search_domain => virtual_server.search_domain,
:diskspace => 0 == virtual_server.diskspace ? '' : virtual_server.diskspace,
View
@@ -13,13 +13,19 @@ def delete_physically
destroy
end
- def self.backup(virtual_server)
+ def self.backup(virtual_server, async = true)
veid = virtual_server.identity
name = "ve-dump.#{veid}.#{Time.now.to_i}.tar"
backup_name = "#{virtual_server.hardware_server.backups_dir}/#{name}"
- job = virtual_server.hardware_server.rpc_client.job('tar', "-cf #{backup_name} #{virtual_server.private_dir}")
server_backup = Backup.new(:name => name, :virtual_server_id => virtual_server.id)
- { :job => job, :backup => server_backup }
+
+ if async
+ job = virtual_server.hardware_server.rpc_client.job('tar', "-cf #{backup_name} #{virtual_server.private_dir}")
+ { :job => job, :backup => server_backup }
+ else
+ virtual_server.hardware_server.rpc_client.exec('tar', "-cf #{backup_name} #{virtual_server.private_dir}")
+ server_backup
+ end
end
def restore
@@ -3,7 +3,7 @@ class VirtualServer < ActiveRecord::Base
:orig_os_template, :password, :start_on_boot, :start_after_creation, :state,
:nameserver, :search_domain, :diskspace, :memory, :password_confirmation,
:user_id, :orig_server_template, :description, :cpu_units, :cpus, :cpu_limit,
- :expiration_date, :vswap
+ :expiration_date, :vswap, :daily_backup
attr_accessor :password, :password_confirmation, :start_after_creation
belongs_to :hardware_server
belongs_to :user
@@ -19,6 +19,8 @@ class VirtualServer < ActiveRecord::Base
validates_format_of :host_name, :with => /^[a-z0-9\-\.]*$/i
validates_format_of :description, :with => /^[a-z0-9\-\.\s]*$/i if AppConfig.vzctl.save_descriptions
+ named_scope :daily_backed_up, :conditions => { :daily_backup => true }
+
def self.ip_addresses
result = []
VirtualServer.all.each do |virtual_server|
@@ -139,7 +141,7 @@ def save_physically
vzctl_set(ip_address.split.map { |ip| "--ipadd #{ip} " }.join + "--save") if !ip_address.blank? and ip_address_changed?
if memory_changed?
- if vswap > 0 and hardware_server.vswap
+ if vswap and vswap > 0 and hardware_server.vswap
vzctl_set("--ram #{shellescape(memory.to_s)}M --save")
else
privvmpages = 0 == memory.to_i ? 'unlimited' : memory.to_i * 1024 / 4
@@ -148,7 +150,7 @@ def save_physically
end
end
- if vswap_changed? and vswap > 0 and hardware_server.vswap
+ if vswap_changed? and vswap and vswap > 0 and hardware_server.vswap
vzctl_set("--swap #{shellescape(vswap.to_s)}M --save")
end
@@ -351,6 +353,21 @@ def create_template(template_name)
hardware_server.sync_os_templates
end
+ def create_daily_backup
+ last_backup = backups.find_by_description('auto')
+ last_backup.delete_physically if last_backup
+
+ orig_ve_state = state
+ suspend if 'running' == orig_ve_state
+
+ backup_info = Backup.backup(self, false)
+ backup_info.description = 'auto'
+ backup_info.sync_size
+ backup_info.save
+
+ resume if 'running' == orig_ve_state
+ end
+
private
def vzctl_set(param)
@@ -257,7 +257,7 @@ var showVirtualServerEditForm = function(mode, grid, statsGrid) {
}]
}, {
xtype: 'fieldset',
- title: '<%= tjs('admin.virtual_servers.form.create_server.fieldset.start_options') %>',
+ title: '<%= tjs('admin.virtual_servers.form.create_server.fieldset.misc') %>',
autoHeight: true,
defaultType: 'checkbox',
labelWidth: 180,
@@ -273,6 +273,12 @@ var showVirtualServerEditForm = function(mode, grid, statsGrid) {
checked: true,
name: 'start_after_creation',
id: 'checkboxStartAfterCreation'
+ }, {
+ fieldLabel: '<%= tjs('admin.virtual_servers.form.create_server.field.daily_backup') %>',
+ xtype: 'checkbox',
+ checked: false,
+ name: 'daily_backup',
+ id: 'checkboxDailyBackup'
}]
}]
}]
View
@@ -363,11 +363,12 @@ en:
cpus: "Number of CPUs"
cpu_limit: "CPU Limit, %"
expiration_date: "Expiration date"
+ daily_backup: "Daily backup"
fieldset:
general: "General"
network: "Network"
system_access: "System Access"
- start_options: "Start Options"
+ misc: "Miscellaneous"
dns_settings: "DNS Settings"
limits: "Limits"
tab:
@@ -0,0 +1,9 @@
+class AddDailyBackupToVirtualServer < ActiveRecord::Migration
+ def self.up
+ add_column :virtual_servers, :daily_backup, :boolean, :default => false
+ end
+
+ def self.down
+ remove_column :virtual_servers, :daily_backup
+ end
+end
View
@@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20130202075214) do
+ActiveRecord::Schema.define(:version => 20130202132959) do
create_table "background_jobs", :force => true do |t|
t.string "description"
@@ -146,6 +146,7 @@
t.integer "cpus"
t.date "expiration_date"
t.integer "vswap", :default => 0
+ t.boolean "daily_backup", :default => false
end
end
View
@@ -0,0 +1,38 @@
+namespace :cron do
+
+ desc "Perfrom all scheduled tasks"
+ task :all => :environment do
+ puts "Loading #{Rails.env} environment..."
+ do_scheduled_backups
+ do_clear_old_logs
+ end
+
+ desc "Create scheduled backups"
+ task :backups => :environment do
+ do_scheduled_backups
+ end
+
+ desc "Clear old logs"
+ task :logs => :environment do
+ do_clear_old_logs
+ end
+
+ private
+
+ def do_scheduled_backups
+ puts "Create scheduled backups..."
+ VirtualServer.daily_backed_up.each do |virtual_server|
+ begin
+ virtual_server.create_daily_backup
+ rescue Exception => e
+ puts "Unable to create backup for server with internal id ##{virtual_server.id}: #{e.message}"
+ end
+ end
+ end
+
+ def do_clear_old_logs
+ puts "Clear old logs..."
+ Rake::Task['event_log:clear_old'].invoke
+ end
+
+end
View
@@ -0,0 +1,9 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../config/boot'
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+require 'tasks/rails'
+
+Rake::Task['cron:all'].invoke
View
@@ -0,0 +1,15 @@
+#!/bin/sh
+###
+# OpenVZ Web Panel cron script
+###
+
+# configuration defaults
+if [ -f `dirname $0`/../script/owp ]; then
+ INSTALL_DIR=`dirname $0`/../
+else
+ INSTALL_DIR="/opt/ovz-web-panel/"
+fi
+
+[ -f /etc/owp.conf ] && . /etc/owp.conf
+
+RAILS_ENV=production $INSTALL_DIR/script/cron_runner

0 comments on commit 9782b69

Please sign in to comment.