1
0
mirror of https://github.com/krislamo/puppet-rsnapshot synced 2024-11-10 00:00:35 +00:00

random time feature with ranges (random between 1am and 5 am: 1..5)

This commit is contained in:
Norbert Varzariu 2015-12-20 18:23:37 +01:00
parent b2d6edad94
commit bc86010c51
6 changed files with 103 additions and 2 deletions

View File

@ -0,0 +1,18 @@
module Puppet::Parser::Functions
newfunction(:assert_empty_hash, :type => :rvalue, :doc => <<-EOS
This function checks an input struct for undefined hashes in key => hash and assigns {}. This is only a helper function to make a hash.each work if one of the values is undefined
EOS
)do |args|
fail "Must receive one argument." if args.empty?
args.each do |arg|
h = Hash.new
arg.each_pair do |host, hash|
unless hash.is_a? Hash
hash = {}
end
h[host] = hash
end
return h
end
end
end

View File

@ -0,0 +1,37 @@
module Puppet::Parser::Functions
newfunction(:rand_from_array, :type => :rvalue, :doc => <<-EOS
This function takes either an int or an array as input and returns the int or a
random element from the array
EOS
)do |args|
fail "Must receive two argument." if args.empty?
#++ this works if the input is an array
# return args.sample
# +++++++++++++++++++++++++++++++++++++
# args.flatten!
params = []
params << args[0]
params.flatten!
arr = []
salt = args[1].sum % 60
rnd = Random.new(salt)
# rnd = Random.new()
params.each do |key|
if key.is_a?(String) and key =~ /\d\.\.\d/
k = key.split('..')
r = Range.new(k[0],k[1]).to_a
arr << r
arr.flatten!
elsif key.is_a?(String)
arr << key
arr.flatten!
elsif key.is_a?(Integer)
arr << key.to_s
arr.flatten!
end
end
last_i = arr.length - 1
return arr[rnd.rand(0..last_i)]
end
end

View File

@ -16,7 +16,6 @@ class rsnapshot::config (
ensure => 'directory',
}
#$foo = ['1', '2', '3', '20..21']
# $foo='*/5'
#$qax = rand_from_array($foo)
#notice("QAX is $qax ")
@ -34,6 +33,7 @@ class rsnapshot::config (
$snapshot_root = pick($hash['snapshot_root'], $rsnapshot::params::config_snapshot_root)
$backup_user = pick($hash['backup_user'], $rsnapshot::params::config_backup_user)
$default_backup_dirs = pick($rsnapshot::default_backup, $rsnapshot::params::config_default_backup)
$backup_levels = pick($hash['backup_levels'], $rsnapshot::params::config_backup_levels, 'weekly')
$backup = $hash['backup']
$backup_defaults = pick($hash['backup_defaults'], $rsnapshot::params::config_backup_defaults)
$cmd_cp = pick($hash['cmd_cp'], $rsnapshot::params::config_cmd_cp)
@ -114,6 +114,37 @@ class rsnapshot::config (
file { $config:
content => template('rsnapshot/rsnapshot.erb')
}
########################### CRON ####################################################
#30 1 * * * /usr/bin/rsnapshot -c /etc/rsnapshot/cmweb1.rsnapshot.conf daily
#15 1 * * 0 /usr/bin/rsnapshot -c /etc/rsnapshot/cmweb1.rsnapshot.conf weekly
# 00 1 1 * * /usr/bin/rsnapshot -c /etc/rsnapshot/cmweb1.rsnapshot.conf monthly
$cronfile = "/tmp/rsnapshot.d/cron/${host}"
$cron = pick_default($hash['cron'], $rsnapshot::params::cron, 'absent')
concat { "${cronfile}":
# replace => false,
}
$backup_levels.each |String $level| {
# allow to globally override ranges, create random numbers for backup_levels daily, weekly, monthly
$c_min = pick($cron['minute'], $rsnapshot::params::cron['minute'], '*')
$c_hour = pick($cron['hour'], $rsnapshot::params::cron['hour'], '*')
$c_monthday = pick($cron['monthday'], $rsnapshot::params::cron['monthday'], '*')
$c_month = pick($cron['month'], $rsnapshot::params::cron['month'], '*')
$c_weekday = pick($cron['weekday'], $rsnapshot::params::cron['weekday'], '*')
$minute = rand_from_array($c_min, "${host}.${level}")
$hour = rand_from_array($c_hour, "${host}.${level}")
$monthday = rand_from_array($c_monthday, "${host}.${level}")
$month = rand_from_array($c_month, "${host}.${level}")
$weekday = rand_from_array($c_weekday, "${host}.${level}")
concat::fragment { "${host}.${level}":
target => "${cronfile}",
content => template('rsnapshot/cron.erb'),
}
}
}
}

7
manifests/cron.pp Normal file
View File

@ -0,0 +1,7 @@
# this class sets up cron
# * todo
# let the user specify a range of hours and randomize cron creation in this range use rand for #args and return arg[rand]
class rsnapshot::cron {
}

View File

@ -7,6 +7,7 @@ class rsnapshot::params {
$config_backup_user = 'root'
$package_name = 'rsnapshot'
$package_ensure = 'present'
$config_backup_levels = [ 'daily', 'weekly', ]
$config_backup_defaults = true
$config_version = '1.2'
$config_cmd_cp = '/bin/cp'
@ -60,5 +61,11 @@ class rsnapshot::params {
}
$config_backup_scripts = {}
$cron = {
'minute' => ['0..20'],
'hour' => ['21..23','0..4','5'],
'monthday' => '*',
'month' => '*',
'weekday' => '*',
}
}

1
templates/cron.erb Normal file
View File

@ -0,0 +1 @@
<%= @minute %> <%= @hour %> <%= @monthday %> <%= @weekday %> root /usr/bin/rsnapshot -c <%= @config %> <%= @level %>