Configuración de Restic con systemd en Linux

Restic es un software de copia de seguridad muy conocido. Es lo suficientemente simple como para ser portado a cualquier sistema operativo, y probablemente por eso no viene con una configuración de muestra completa en un sistema Linux promedio. Arreglemoslo con esta publicación.







Establezcamos el problema de la siguiente manera:







  1. La copia de seguridad automática se ejecuta a diario.
  2. La copia de seguridad solo almacena archivos y datos importantes.
  3. La copia de seguridad también incluye el contenido de las bases de datos de PostgreSQL, que se pueden restaurar psql -f



    .


TL; puesto de DR

/ systemd, restic CAP_DAC_READ_SEARCH



, PostgreSQL pg_dumpall



.







Esto supone que la copia de seguridad se realiza en una máquina Ubuntu Server 20.04 y se realiza en un servidor de descanso en ejecución 192.168.1.200



. Sin embargo, la configuración se puede adaptar trivialmente a cualquier proveedor de la nube. También asume que el repositorio ya ha sido inicializado por el comando restic -r rest:http://192.168.1.200/your-repo/ init



.







Copia de seguridad de archivos / directorios



No es deseable ejecutar software con derechos de superusuario innecesariamente, así que creemos un usuario separado para nuestras tareas restic



sin un grupo y un shell de comandos:







# useradd -m -N -s /usr/sbin/nologin restic
      
      





Necesitamos el siguiente servicio systemd con un parámetro y un temporizador:







/etc/systemd/system/restic@.service



:







[Unit]
#      @,   
#   systemctl start restic@your-repo.service
# %I  "your-repo"
Description=Restic backup on %I
After=syslog.target
After=network-online.target

[Service]
Type=oneshot
User=restic
#        /etc/restic/your-repo.files
ExecStart=/usr/local/bin/restic backup --files-from /etc/restic/%I.files
#      /etc/restic/your-repo.env
EnvironmentFile=/etc/restic/%I.env
#  restic  capability DAC_READ_SEARCH,  
#      Linux,    
# ,      
#  
AmbientCapabilities=CAP_DAC_READ_SEARCH

[Install]
WantedBy=multi-user.target
      
      





/etc/systemd/system/restic@.timer



:







[Unit]
# ,      @
# (restic@your-repo.timer),  restic@your-repo.service
Description=Run Restic at 12:00 AM

[Timer]
#  restic   12  
OnCalendar=*-*-* 12:00:00

[Install]
WantedBy=timers.target
      
      





/etc/restic/your-repo.env



. systemd root, /etc/restic/



(.. 700 root



):







RESTIC_PASSWORD=your_repo_password
RESTIC_REPOSITORY=rest:http://192.168.1.200/your-repo/
      
      





/ /etc/restic/your-repo.files



:







/var/lib/docker
/etc/postgresql
/etc/restic
...
      
      





PostgreSQL



Restic , , pg_dumpall



. systemd ExecStart



execve(3)



, /usr/local/bin/pgdump.sh



:







#!/usr/bin/env bash

set -euo pipefail

/usr/bin/sudo -u postgres pg_dumpall --clean \
    | gzip --rsyncable \
    | /usr/local/bin/restic backup --host $1 --stdin \
        --stdin-filename postgres-$1.sql.gz
      
      





/etc/systemd/system/restic-pg@.service



:







[Unit]
Description=Restic PostgreSQL backup on %I
After=syslog.target
After=network-online.target
After=postgresql.service
Requires=postgresql.service

[Service]
Type=oneshot
User=restic
ExecStart=/usr/local/bin/pgdump.sh %I
EnvironmentFile=/etc/restic/%I.env

[Install]
WantedBy=multi-user.target
      
      





/etc/systemd/system/restic-pg@.timer



:







[Unit]
Description=Run Restic on PostgreSQL at 12:00 AM

[Timer]
OnCalendar=*-*-* 0:00:00

[Install]
WantedBy=timers.target
      
      







Iniciemos los temporizadores y habilitemos su carga automática:







# systemctl enable --now restic@your-repo.timer restic-pg@your-repo.timer
      
      





Comprobemos si el sistema construido funciona:







# systemctl start restic@your-repo.service
# systemctl start restic-pg@your-repo.service
      
      





Este conjunto de unidades le permite realizar copias de seguridad en un número ilimitado de repositorios, solo necesita crear los apropiados /etc/restic/repo-name.{env,files}



.







Enlaces






All Articles