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 local/exports -o mountpoint=/var/backups/lxd'] ) if not host.fact.command('mount | grep exports || true'): server.shell( name='Mount local/exports', commands=['zfs mount local/exports'], ) 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 local/databases -o mountpoint=/var/backups/databases'] ) if not host.fact.command('mount | grep databases || true'): server.shell( name='Mount local/databases', commands=['zfs mount local/databases'], ) 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', )