This repository has been archived on 2025-02-14. You can view files and clone it, but cannot push or open issues or pull requests.
pyinfra-lxd/setup-backup.py
2021-02-01 19:42:43 +09:00

78 lines
1.7 KiB
Python

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',
)
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',
)