Backup module for WMDE
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
2.1 KiB

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 = [],
$encryption = "repokey",
$passphrase = "`/bin/cat /root/borg_passphrase.txt`",
$ssh_user = 'root',
$ssh_port = '22',
$ssh_check_hostkey = 'no',
$borg_options = $wmdeit_backup::borg::borg_options,
$weekday = undef,
$hour = '0',
$minute = '0',
$retain_daily = '30',
$retain_monthly = '6',
$retain_yearly = '0',
$retry_count = 3,
$statistics = true,
) {
$scripts = $wmdeit_backup::borg::scripts
$repos = $wmdeit_backup::borg::repos
$borg_cmd = $wmdeit_backup::borg::borg_cmd
$sshfs_options = $wmdeit_backup::borg::sshfs_options
$repo = "$repos/$title"
if $statistics {
$time_cmd = '/usr/bin/time'
$borg_statistics = '-s'
}
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`
$wmdeit_backup::borg::sshfs_cmd $sshfs_options -oStrictHostKeyChecking=$ssh_check_hostkey -oPort=$ssh_port $ssh_user@$server:/ $mnt
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
cd /
umount $mnt
PRUNECMD=\"$borg_cmd prune -d $retain_daily -m $retain_monthly -y $retain_yearly ${repo}\"
\$PRUNECMD
"
}->
cron {"borg-$title":
weekday => $weekday,
hour => $hour,
minute => $minute,
command => "$time_cmd /bin/sh $script_name"
}
}