• [^] # Re: petite solution redonée

    Posté par . En réponse au journal Y'a le feu. Évalué à 10. Dernière modification le 25 janvier 2022 à 12:32.

    Sommaire

    Et voici, c'est à adapter à chaque usage, et il y a probablement mieux comme config systemd ;-)

    Côté serveurs

    Je pars d'une Debian minimale.

    1. Perso je monte le disque dur externe dans /mnt/backup
    2. installer SSH + borgbackup (dans les paquets)
    3. désactiver les connexions SSH par mots de passe, dans /etc/ssh/sshd_config:
      PasswordAuthentication no
    4. créer un utilisateur, ajouter la clé SSH publique dans /home/USER/.ssh/authorized_keys, en la limitant au maximum: command="borg serve --restrict-to-repository /mnt/backup",restrict ssh-rsa ...

    4.Installer UFW et paramétrer le parefeu

    Un petit role Ansible permet d'avoir la même configuration sur les deux raspberry pi ;-)

    Côté Client

    borgmatic

    1. Installer borgmatic
    2. Crée un fichier de configuration ~/.config/borgmatic/config.yaml, en s'inspirant de la documentation.
    location:
     # List of source directories to backup (required). Globs and tildes are expanded.
     source_directories:
     - /home/USER
     repositories:
     - USER@RaspberryPi1:/mnt/backup/BACKUP_NAME/
     - USER@RaspberrPi2:/mnt/backup/BACKUP_NAME/
     # are expanded. See the output of "borg help patterns" for more details.
     exclude_patterns:
     # probleme avec les logiciels base sur electron:
     - "**/CodeCache/"
     - "**/GPUCache/"
     #
     - "**/[Cc]ache/"
     - "**/tmp/"
     - "*.pyc"
     - "*.swp"
     - /home/*/.cache/
     - /home/*/.gvfs
     - /home/*/.kde/socket-*
     - /home/*/.kde/tmp-*
     - /home/*/.kodi/userdata/Thumbnails/
     - /home/*/.local/share/Trash/
     - /home/*/.local/share/akonadi/
     - /home/*/.local/share/backintime/
     - /home/*/.local/share/marble/maps/
     - /home/*/.local/share/torbrowser/
     - /home/*/.opencpn/iconCache/
     - /home/*/.opencpn/raster_texture_cache/
     - /home/*/.steam/
     - /home/*/.thumbnails/
     #
     # Exclude directories that contain a CACHEDIR.TAG file. See
     # http://www.brynosaurus.com/cachedir/spec.html for details.
     exclude_caches: true
     # Exclude directories that contain a file with the given filename.
     exclude_if_present: .nobackup
    storage:
     encryption_passphrase: "PASSPHRASE"
     # Remote network upload rate limit in kiBytes/second. Defaults
     # to unlimited.
     # remote_rate_limit: 100
    retention:
     keep_within: 10H
     keep_hourly: 24
     keep_daily: 7
     keep_weekly: 4
     keep_monthly: 6
     keep_yearly: 1
     # When pruning, only consider archive names starting with this prefix.
     # Borg placeholders can be used. See the output of "borg help placeholders" for
     # details. Default is "{hostname}-".
     #prefix: sourcehostname
    consistency:
     checks:
     - disabled
    hooks:
     healthchecks: https://hc-ping.com/ID_HEALTH_CHECKS

    le hook healthchecks permet d'envoyer un ping sur https://healthchecks.io/ si des fois il y a un soucis (par ex. IP qui change, RPI éteint, etc.)

    Le timer systemd

    Créer un ficher ~/.config/systemd/user/borgmatic.timer, par exemple pour une sauvegarde toute les heures :

     [Unit]
     Description=Run borgmatic backup
     [Timer]
     OnCalendar=hourly
     Persistent=true
     [Install]
     WantedBy=timers.target
    

    Le service systemd

    Créer un fichier ~/.config/systemd/user/borgmatic.service :

    [Unit]
    Description=borgmatic backup
    Wants=network-online.target
    After=network-online.target
    # ConditionACPower=true
    [Service]
    Type=oneshot
    # Lower CPU and I/O priority.
    Nice=19
    CPUSchedulingPolicy=batch
    IOSchedulingClass=best-effort
    IOSchedulingPriority=7
    IOWeight=100
    Restart=no
    # Prevent rate limiting of borgmatic log events. If you are using an older version of systemd that
    # doesn't support this (pre-240 or so), you may have to remove this option.
    LogRateLimitIntervalSec=0
    # Delay start to prevent backups running during boot. Note that systemd-inhibit requires dbus and
    # dbus-user-session to be installed.
    ExecStartPre=sleep 1m
    ExecStart=systemd-inhibit --who="borgmatic" --why="Prevent interrupting sc# Lower CPU and I/O priority.
    Nice=19
    CPUSchedulingPolicy=batch
    IOSchedulingClass=best-effort
    IOSchedulingPriority=7
    IOWeight=100
    Restart=no
    # Prevent rate limiting of borgmatic log events. If you are using an older version of systemd that
    # doesn't support this (pre-240 or so), you may have to remove this option.
    LogRateLimitIntervalSec=0
    # Delay start to prevent backups running during boot. Note that systemd-inhibit requires dbus and
    # dbus-user-session to be installed.
    ExecStartPre=sleep 1m
    ExecStart=systemd-inhibit --who="borgmatic" --why="Prevent interrupting scheduled backup" /usr/bin/borgmaticheduled backup" /usr/bin/borgmatic
    

    Pourquoi systemd ? si j'allume mon PC à 16h10, cron attendra 17h pour faire la sauvegarde, tandis que systemd lancera la sauvegarde à 16h10, si aucune sauvegarde n'a été faite depuis plus d'une heure.

    Je me suis inspiré de l'exemple de borgmatic pour le service.

    lancement du timer:

    systemctl --user enable borgmatic.timer

    Pour voir si le timer se lance bien :

    systemctl --user list-timers

    Si tu as des questions n'hésite pas !