from pyinfra import host from pyinfra.operations import server, files, systemd, apt SUDO = True apt.packages( name='Install packages', packages=['borgbackup', 'borgmatic'], update=True, ) files.directory( name='Ensure /var/backups/lxd exists', path='/var/backups/lxd', user='root', group='root', mode=700 ) if not host.fact.command('zfs list | grep exports || true'): server.shell( name='Create ZFS volume exports', commands=['zfs create backup/exports -o mountpoint=/var/backups/lxd'] ) if not host.fact.command('mount | grep exports || true'): server.shell( name='Mount backup/exports', commands=['zfs mount backup/exports || true'], ) files.directory( name='Ensure /var/backups/databases exists', path='/var/backups/databases', user='root', group='root', mode=700 ) if not host.fact.command('zfs list | grep databases || true'): server.shell( name='Create ZFS volume databases', commands=['zfs create backup/databases -o mountpoint=/var/backups/databases'] ) if not host.fact.command('mount | grep databases || true'): server.shell( name='Mount backup/databases', commands=['zfs mount backup/databases || true'], ) files.template( name='Push borgmatic config', src='templates/borgmatic.yaml.j2', dest='/etc/borgmatic/config.yaml', mode='600', user='root', group='root', ) files.put( name='Push lxd-export script', src='files/lxd-export.sh', dest='/usr/local/bin/lxd-export.sh', user='root', group='root', mode='700', ) files.put( name='Push lxd-databases script', src='files/lxd-databases.sh', dest='/usr/local/bin/lxd-databases.sh', user='root', group='root', mode='700', ) files.put( name='Push borgmatic timer', src='files/borgmatic.timer', dest='/etc/systemd/system/borgmatic.timer', user='root', group='root', mode='644', ) files.put( name='Push borgmatic service', src='files/borgmatic.service', dest='/etc/systemd/system/borgmatic.service', user='root', group='root', mode='644', ) systemd.daemon_reload( name='Reload systemd', user_mode=False, ) systemd.service( name='Enable borgmatic timer', service='borgmatic.timer', running=True, enabled=True, )