default.rb
2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#
# Author:: Earth U (<sysadmin@chromedia.com>)
# Cookbook Name:: backup-file2s3
# Recipe:: default
#
# Copyright 2016, Chromedia Far East, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
include_recipe 'awscli'
include_recipe 'tar'
attribs = node['backup-file2s3']
pub_key_file = "#{attribs['script_dir']}/pub.key"
directory attribs['script_dir'] { recursive true }
directory attribs['log_dir'] { recursive true }
is_any_enc = attribs['backups'].any? do |back|
back[:paths].any? do |path|
path[:bak_encrypted]
end
end
if !attribs['encrypt']['pub_key'] && is_any_enc
Chef::Application.fatal!('No encryption public key contents supplied')
end
file pub_key_file do
content attribs['encrypt']['pub_key']
mode 0600
owner 'root'
group 'root'
sensitive true
only_if { attribs['encrypt']['pub_key'] }
end
attribs['backups'].each do |back|
snam = back[:script_name] || 'default'
sname = "#{snam.gsub(' ', '-')}_backup2s3"
enable = back.has_key?(:enable) ? back[:enable] : true
template "#{attribs['script_dir']}/#{sname}" do
mode 0644
source 'backup_file_to_s3.erb'
variables(
:aws_bin => attribs['aws_bin'],
:tar_bin => attribs['tar_bin'],
:tmp_dir => attribs['tmp_dir'],
:bucket => attribs['bucket'],
:region => attribs['region'],
:pub_key_file => pub_key_file,
:paths => back[:paths]
)
action( enable ? :create : :delete )
end
sched = attribs['cron']['sched'].split(' ')
cron_d sname do
command "bash #{attribs['script_dir']}/#{sname} >> "\
"#{attribs['log_dir']}/#{sname}.log 2>&1"
minute sched[0]
hour sched[1]
day sched[2]
month sched[3]
weekday sched[4]
mailto attribs['cron']['mailto']
path '/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin'
action( enable ? :create : :delete )
end
end
package 'logrotate'
loa = attribs['logrotate']
template "#{loa['conf_dir']}/backup_file_to_s3" do
source 'backup_file_to_s3_logrotate.erb'
only_if "test -d #{loa['conf_dir']} || mkdir -p #{loa['conf_dir']}"
variables(
:log_dir => attribs['log_dir'],
:options => loa['options']
)
end