puppet-wmdeit_backup/manifests/borg.pp

111 lines
2.1 KiB
Puppet
Raw Permalink Normal View History

2021-06-25 08:36:35 +00:00
class wmdeit_backup::borg (
$repos = $borg_repos,
$scripts = $borg_scripts
)
inherits wmdeit_backup::params
{
class {"wmdeit_backup::setup":
}->
package {$borg_packages:
ensure => installed
}
file {$scripts:
ensure => directory
}
}
define wmdeit_backup::borg_backup
(
$server = $title,
$dirs = [],
2021-06-25 15:28:11 +00:00
$encryption = "repokey",
$passphrase = "`/bin/cat /root/borg_passphrase.txt`",
2021-06-25 08:36:35 +00:00
$ssh_user = 'root',
$ssh_port = '22',
$ssh_check_hostkey = 'no',
$borg_options = $wmdeit_backup::borg::borg_options,
2021-06-25 08:36:35 +00:00
$weekday = undef,
$hour = '0',
$minute = '0',
$retain_daily = '30',
$retain_monthly = '6',
$retain_yearly = '0',
$retry_count = 3,
$statistics = true,
2021-06-25 08:36:35 +00:00
) {
$scripts = $wmdeit_backup::borg::scripts
$repos = $wmdeit_backup::borg::repos
$borg_cmd = $wmdeit_backup::borg::borg_cmd
2021-06-25 15:28:11 +00:00
$sshfs_options = $wmdeit_backup::borg::sshfs_options
2021-06-25 08:36:35 +00:00
$repo = "$repos/$title"
if $statistics {
$time_cmd = '/usr/bin/time'
$borg_statistics = '-s'
}
2021-06-25 08:36:35 +00:00
exec {"borg_init_$title":
command => "/bin/sh -c 'export BORG_PASSPHRASE=$passphrase && $borg_cmd init -e $encryption $repo'",
unless => "/bin/sh -c 'export BORG_PASSPHRASE=$passphrase && $borg_cmd list -e $encryption $repo'",
# creates => $repo
}
$p = $dirs.map | $str | { ".$str" }
$backup_dirs = join($p," ")
$mnt = "$wmdeit_backup::borg::borg_mnt/$title"
$script_name = "$scripts/$title.sh"
file {$mnt:
ensure => directory
}->
file {$script_name:
ensure => file,
content => "#!/bin/sh
export BORG_PASSPHRASE=$passphrase
D=`date +%F`
2021-06-25 15:28:11 +00:00
$wmdeit_backup::borg::sshfs_cmd $sshfs_options -oStrictHostKeyChecking=$ssh_check_hostkey -oPort=$ssh_port $ssh_user@$server:/ $mnt
2021-06-25 08:36:35 +00:00
cd $mnt
CMD=\"$borg_cmd create $borg_statistics --checkpoint-interval 600 $borg_options ${repo}::$title-\${D} $backup_dirs\"
n=0
until [ \"\$n\" -ge $retry_count ]
do
\$CMD && break
n=\$((n+1))
sleep 1
done
2021-06-25 08:36:35 +00:00
cd /
umount $mnt
PRUNECMD=\"$borg_cmd prune -d $retain_daily -m $retain_monthly -y $retain_yearly ${repo}\"
\$PRUNECMD
2021-06-25 08:36:35 +00:00
"
}->
cron {"borg-$title":
weekday => $weekday,
hour => $hour,
minute => $minute,
command => "$time_cmd /bin/sh $script_name"
2021-06-25 08:36:35 +00:00
}
}