summaryrefslogtreecommitdiff
path: root/puppet/modules/backupninja/manifests/server.pp
blob: 49e42a0f33963551a63130411c7bd24fa4054e66 (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# this define realizes all needed resources for a hosted backup
define backupninja_server_realize($host) {
  User               <<| tag == "backupninja-$host" |>>
  File               <<| tag == "backupninja-$host" |>>
  Ssh_authorized_key <<| tag == "backupninja-$host" |>>
}

class backupninja::server (
  $backupdir = '/backup',
  $backupdir_ensure = 'directory',
  $manage_nagios = false,
  $nagios_server = undef,
  $nagios_warn_level = 129600,
  $nagios_crit_level = 216000,
) {

  group { "backupninjas":
    ensure => "present",
    gid => 700
  }
  
  file { $backupdir:
    ensure => $backupdir_ensure,
    mode => 0710, owner => root, group => "backupninjas",
    require => $backupdir_ensure ? {
      'directory' => undef,
      default     => File["$backupdir_ensure"],
    }
  }

  if $manage_nagios {

    case $nagios_server { undef: { err('Cannot manage nagios without nagios_server parameter!') } }

    include nagios::nsca::client
    
    file { "/usr/local/bin/checkbackups":
      ensure => "present",
      source => "puppet:///modules/backupninja/checkbackups.pl",
      mode => 0755, owner => root, group => root,
    }

    cron { checkbackups:
      command => "/usr/local/bin/checkbackups -d ${backupdir} -s ${nagios_server} -w ${nagios_warn_level} -c ${nagios_crit_level} | grep -v 'sent to host successfully'",
      user => "root",
      hour => "8-23",
      minute => 59,
      require => [ File["/usr/local/bin/checkbackups"], Package['nsca'] ]
    }
  }

  # collect all resources from hosted backups
  Backupninja_server_realize <<| tag == $::fqdn |>>

  # this define allows nodes to declare a remote backup sandbox, that have to
  # get created on the server
  define sandbox (
    $user = $name,
    $host = $::fqdn,
    $installuser = true,
    $dir,
    $manage_ssh_dir = true,
    $ssh_dir = "${dir}/.ssh",
    $authorized_keys_file = 'authorized_keys',
    $key = false,
    $keytype = 'dss',
    $backupkeys = "${fileserver}/keys/backupkeys",
    $uid = false,
    $gid = "backupninjas",
    $backuptag = "backupninja-${::fqdn}",
  ) {

    if !defined(Backupninja_server_realize["${::fqdn}@${host}"]) {
      @@backupninja_server_realize { "${::fqdn}@${host}":
        host => $::fqdn,
        tag  => $host,
      }
    }

    if !defined(File["$dir"]) {
      @@file { "$dir":
        ensure => directory,
        mode => 0750, owner => $user, group => 0,
        tag => "$backuptag",
      }
    }

    if $installuser {

       if $manage_ssh_dir {
        if !defined(File["$ssh_dir"]) {
          @@file { "${ssh_dir}":
            ensure => directory,
            mode => 0700, owner => $user, group => 0,
            require => [User[$user], File["$dir"]],
            tag => "$backuptag",
          }
         }
       } 

      if $key {
        # $key contais ssh public key
        if !defined(Ssh_autorized_key["$user"]) {
          @@ssh_authorized_key{ "$user":
            type    => $keytype,
            key     => $key,
            user    => $user,
            target  => "${ssh_dir}/${authorized_keys_file}",
            tag     => "$backuptag",
            require => User[$user],
          }
        }
      }
      else {
        # get ssh public key exists from server
        if !defined(File["${ssh_dir}/${authorized_keys_file}"]) {
          @@file { "${ssh_dir}/${authorized_keys_file}":
            ensure => present,
            mode => 0644, owner => 0, group => 0,
            source => "${backupkeys}/${user}_id_${keytype}.pub",
            require => File["${ssh_dir}"],
            tag => "$backuptag",
          }
        }
      }
      
      if !defined(User["$user"]) {
        @@user { "$user":
          ensure   => "present",
          uid      => $uid ? {
              false   => undef,
              default => $uid
          },
          gid      => "$gid",
          comment  => "$user backup sandbox",
          home     => "$dir",
          managehome => true,
          shell    => "/bin/bash",
          password => '*',
          require  => Group['backupninjas'],
          tag      => "$backuptag"
        }
      }
    }
  }
}