Skip to content
This repository
Browse code

Basic support for scheduled backups (issue 247).

  • Loading branch information...
commit 9782b69e19f24117856c12e129190c8fdc0095f6 1 parent 7871433
Alexei Yuzhakov authored February 02, 2013
2  app/controllers/admin/virtual_servers_controller.rb
@@ -45,6 +45,7 @@ def create
45 45
     end
46 46
     virtual_server.attributes = params
47 47
     virtual_server.start_on_boot = params.key?(:start_on_boot)
  48
+    virtual_server.daily_backup = params.key?(:daily_backup)
48 49
 
49 50
     if virtual_server.save_physically
50 51
       render :json => { :success => true }
@@ -64,6 +65,7 @@ def load_data
64 65
       :ip_address => virtual_server.ip_address,
65 66
       :host_name => virtual_server.host_name,
66 67
       :start_on_boot => virtual_server.start_on_boot,
  68
+      :daily_backup => virtual_server.daily_backup,
67 69
       :nameserver => virtual_server.nameserver,
68 70
       :search_domain => virtual_server.search_domain,
69 71
       :diskspace => 0 == virtual_server.diskspace ? '' : virtual_server.diskspace,
12  app/models/backup.rb
@@ -13,13 +13,19 @@ def delete_physically
13 13
     destroy
14 14
   end
15 15
 
16  
-  def self.backup(virtual_server)
  16
+  def self.backup(virtual_server, async = true)
17 17
     veid = virtual_server.identity
18 18
     name = "ve-dump.#{veid}.#{Time.now.to_i}.tar"
19 19
     backup_name = "#{virtual_server.hardware_server.backups_dir}/#{name}"
20  
-    job = virtual_server.hardware_server.rpc_client.job('tar', "-cf #{backup_name} #{virtual_server.private_dir}")
21 20
     server_backup = Backup.new(:name => name, :virtual_server_id => virtual_server.id)
22  
-    { :job => job, :backup => server_backup }
  21
+
  22
+    if async
  23
+      job = virtual_server.hardware_server.rpc_client.job('tar', "-cf #{backup_name} #{virtual_server.private_dir}")
  24
+      { :job => job, :backup => server_backup }
  25
+    else
  26
+      virtual_server.hardware_server.rpc_client.exec('tar', "-cf #{backup_name} #{virtual_server.private_dir}")
  27
+      server_backup
  28
+    end
23 29
   end
24 30
 
25 31
   def restore
23  app/models/virtual_server.rb
@@ -3,7 +3,7 @@ class VirtualServer < ActiveRecord::Base
3 3
     :orig_os_template, :password, :start_on_boot, :start_after_creation, :state,
4 4
     :nameserver, :search_domain, :diskspace, :memory, :password_confirmation,
5 5
     :user_id, :orig_server_template, :description, :cpu_units, :cpus, :cpu_limit,
6  
-    :expiration_date, :vswap
  6
+    :expiration_date, :vswap, :daily_backup
7 7
   attr_accessor :password, :password_confirmation, :start_after_creation
8 8
   belongs_to :hardware_server
9 9
   belongs_to :user
@@ -19,6 +19,8 @@ class VirtualServer < ActiveRecord::Base
19 19
   validates_format_of :host_name, :with => /^[a-z0-9\-\.]*$/i
20 20
   validates_format_of :description, :with => /^[a-z0-9\-\.\s]*$/i if AppConfig.vzctl.save_descriptions
21 21
 
  22
+  named_scope :daily_backed_up, :conditions => { :daily_backup => true }
  23
+
22 24
   def self.ip_addresses
23 25
     result = []
24 26
     VirtualServer.all.each do |virtual_server|
@@ -139,7 +141,7 @@ def save_physically
139 141
       vzctl_set(ip_address.split.map { |ip| "--ipadd #{ip} " }.join + "--save") if !ip_address.blank? and ip_address_changed?
140 142
 
141 143
       if memory_changed?
142  
-        if vswap > 0 and hardware_server.vswap
  144
+        if vswap and vswap > 0 and hardware_server.vswap
143 145
           vzctl_set("--ram #{shellescape(memory.to_s)}M --save")
144 146
         else
145 147
           privvmpages = 0 == memory.to_i ? 'unlimited' : memory.to_i * 1024 / 4
@@ -148,7 +150,7 @@ def save_physically
148 150
         end
149 151
       end
150 152
 
151  
-      if vswap_changed? and vswap > 0 and hardware_server.vswap
  153
+      if vswap_changed? and vswap and vswap > 0 and hardware_server.vswap
152 154
         vzctl_set("--swap #{shellescape(vswap.to_s)}M --save")
153 155
       end
154 156
 
@@ -351,6 +353,21 @@ def create_template(template_name)
351 353
     hardware_server.sync_os_templates
352 354
   end
353 355
 
  356
+  def create_daily_backup
  357
+    last_backup = backups.find_by_description('auto')
  358
+    last_backup.delete_physically if last_backup
  359
+
  360
+    orig_ve_state = state
  361
+    suspend if 'running' == orig_ve_state
  362
+
  363
+    backup_info = Backup.backup(self, false)
  364
+    backup_info.description = 'auto'
  365
+    backup_info.sync_size
  366
+    backup_info.save
  367
+
  368
+    resume if 'running' == orig_ve_state
  369
+  end
  370
+
354 371
   private
355 372
 
356 373
     def vzctl_set(param)
8  app/views/admin/virtual_servers/_edit_form.html.erb
@@ -257,7 +257,7 @@ var showVirtualServerEditForm = function(mode, grid, statsGrid) {
257 257
           }]
258 258
         }, {
259 259
           xtype: 'fieldset',
260  
-          title: '<%= tjs('admin.virtual_servers.form.create_server.fieldset.start_options') %>',
  260
+          title: '<%= tjs('admin.virtual_servers.form.create_server.fieldset.misc') %>',
261 261
           autoHeight: true,
262 262
           defaultType: 'checkbox',
263 263
           labelWidth: 180,
@@ -273,6 +273,12 @@ var showVirtualServerEditForm = function(mode, grid, statsGrid) {
273 273
             checked: true,
274 274
             name: 'start_after_creation',
275 275
             id: 'checkboxStartAfterCreation'
  276
+          }, {
  277
+            fieldLabel: '<%= tjs('admin.virtual_servers.form.create_server.field.daily_backup') %>',
  278
+            xtype: 'checkbox',
  279
+            checked: false,
  280
+            name: 'daily_backup',
  281
+            id: 'checkboxDailyBackup'
276 282
           }]
277 283
         }]
278 284
       }]
3  config/locales/en.yml
@@ -363,11 +363,12 @@ en:
363 363
             cpus: "Number of CPUs"
364 364
             cpu_limit: "CPU Limit, %"
365 365
             expiration_date: "Expiration date"
  366
+            daily_backup: "Daily backup"
366 367
           fieldset:
367 368
             general: "General"
368 369
             network: "Network"
369 370
             system_access: "System Access"
370  
-            start_options: "Start Options"
  371
+            misc: "Miscellaneous"
371 372
             dns_settings: "DNS Settings"
372 373
             limits: "Limits"
373 374
           tab:
9  db/migrate/20130202132959_add_daily_backup_to_virtual_server.rb
... ...
@@ -0,0 +1,9 @@
  1
+class AddDailyBackupToVirtualServer < ActiveRecord::Migration
  2
+  def self.up
  3
+    add_column :virtual_servers, :daily_backup, :boolean, :default => false
  4
+  end
  5
+
  6
+  def self.down
  7
+    remove_column :virtual_servers, :daily_backup
  8
+  end
  9
+end
3  db/schema.rb
@@ -9,7 +9,7 @@
9 9
 #
10 10
 # It's strongly recommended to check this file into your version control system.
11 11
 
12  
-ActiveRecord::Schema.define(:version => 20130202075214) do
  12
+ActiveRecord::Schema.define(:version => 20130202132959) do
13 13
 
14 14
   create_table "background_jobs", :force => true do |t|
15 15
     t.string  "description"
@@ -146,6 +146,7 @@
146 146
     t.integer "cpus"
147 147
     t.date    "expiration_date"
148 148
     t.integer "vswap",                              :default => 0
  149
+    t.boolean "daily_backup",                       :default => false
149 150
   end
150 151
 
151 152
 end
38  lib/tasks/cron.rake
... ...
@@ -0,0 +1,38 @@
  1
+namespace :cron do
  2
+
  3
+  desc "Perfrom all scheduled tasks"
  4
+  task :all => :environment do
  5
+    puts "Loading #{Rails.env} environment..."
  6
+    do_scheduled_backups
  7
+    do_clear_old_logs
  8
+  end
  9
+
  10
+  desc "Create scheduled backups"
  11
+  task :backups => :environment do
  12
+    do_scheduled_backups
  13
+  end
  14
+
  15
+  desc "Clear old logs"
  16
+  task :logs => :environment do
  17
+    do_clear_old_logs
  18
+  end
  19
+
  20
+  private
  21
+
  22
+    def do_scheduled_backups
  23
+      puts "Create scheduled backups..."
  24
+      VirtualServer.daily_backed_up.each do |virtual_server|
  25
+        begin
  26
+          virtual_server.create_daily_backup
  27
+        rescue Exception => e
  28
+          puts "Unable to create backup for server with internal id ##{virtual_server.id}: #{e.message}"
  29
+        end
  30
+      end
  31
+    end
  32
+
  33
+    def do_clear_old_logs
  34
+      puts "Clear old logs..."
  35
+      Rake::Task['event_log:clear_old'].invoke
  36
+    end
  37
+
  38
+end
9  script/cron_runner
... ...
@@ -0,0 +1,9 @@
  1
+#!/usr/bin/env ruby
  2
+
  3
+require File.dirname(__FILE__) + '/../config/boot'
  4
+require 'rake'
  5
+require 'rake/testtask'
  6
+require 'rake/rdoctask'
  7
+require 'tasks/rails'
  8
+
  9
+Rake::Task['cron:all'].invoke
15  script/owp.cron
... ...
@@ -0,0 +1,15 @@
  1
+#!/bin/sh
  2
+###
  3
+# OpenVZ Web Panel cron script
  4
+###
  5
+
  6
+# configuration defaults
  7
+if [ -f `dirname $0`/../script/owp ]; then
  8
+  INSTALL_DIR=`dirname $0`/../
  9
+else
  10
+  INSTALL_DIR="/opt/ovz-web-panel/"
  11
+fi
  12
+
  13
+[ -f /etc/owp.conf ] && . /etc/owp.conf
  14
+
  15
+RAILS_ENV=production $INSTALL_DIR/script/cron_runner

0 notes on commit 9782b69

Please sign in to comment.
Something went wrong with that request. Please try again.