User Tools

Site Tools


backup

Backup

Ci sono diverse soluzioni per il backup di una Linux box. Dopo aver usato per anni rsnapshot sono passato a Borg backup. Qui di seguito la configurazione

Installazione di Borg

Borg può funzionare sia come client andando a salvare i dati su un disco accessibile dal client, sia in modalità client-server facendo uso di un servizio sul server per salvare i dati, quest'ultimo è il più efficiente. Una buona regola è avere due backup su due dischi diversi per garantire maggiore sicurezza. Due backup non vuol dire fare la copia del primo backup su un disco diverso, perchè se il primo backup fosse corrotto, lo sarebbe anche il secondo, quindi si intende fare due backup distinti.

Borg Client

Per installare Borg su Slackware usare Slackbuilds

sqg -p "borgbackup" -o bb
sbopkg -i bb

Borg Server

Il mio server di Borg è sulla Diskstation Synology. Per installarlo seguire queste istruzioni oppure queste. Attivare l'accesso ai pacchetti della community:

  • Login to DSM as administrator with your web browser.
  • Open the Package Center app.
  • Click on the Settings button.
  • In the General tab, set the Trust Level to Any publisher.
  • Click on the Package Sources tab.
  • Click the Add button.
  • Add a Name like SynoCommunity and the Location https://packages.synocommunity.com/
  • Click the OK button.

Installare il pacchetto:

  • Login to DSM as administrator with your web browser.
  • Open the Package Center app.
  • Find the Borg package in the Community section.
  • Click the Install button.

Configurare il pacchetto:

  • Login into DSM with your administration user and open the control panel app.
  • Create a group called borg-backup
  • Create a user called borg-backup
  • Create a shared folder BorgBackup
  • Allow full control for borg-backup user and group to the shared folder.

aprire una sessione SSH con utente root

$ ssh root@diskstation

Creare la cartella per la chiave pubblica SSH nella cartella home di borg-backup e impostare i permessi:

$ mkdir -p /var/services/homes/borg-backup/.ssh
$ touch /var/services/homes/borg-backup/.ssh/authorized_keys
$ chown -R borg-backup:borg-backup /var/services/homes/borg-backup
$ chmod 0700 /var/services/homes/borg-backup
$ chmod 0700 /var/services/homes/borg-backup/.ssh
$ chmod 0600 /var/services/homes/borg-backup/.ssh/*

Client Setup Copiare la chiave pubblica su borg-backup

root@darkstar:~> ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
3e:4f:05:79:3a:9f:96:7c:3b:ad:e9:58:37:bc:37:e4 root@darkstar.gperon.org
root@darkstar:~> cat .ssh/id_rsa.pub | ssh borg-backup@diskstation 'cat >> .ssh/authorized_keys'
borg-backup@diskstation s password: 
root@darkstar:~> ssh borg-backup@diskstation

Bene ora si riesce a fare login senza inserire la password. Per restringere ulteriormente i permessi riportare in autorized_keys il comando da eseguire, in questo caso è borg create e niente altro

command="/usr/local/bin/borg serve --restrict-to-path /volume1/BorgBackup/" ssh-rsa AAAAC3...

Init

Prima di usare borg, occorre inizializzare le cartelle di borg per contenere i backup. Eseguire

borg init --encryption=repokey-blake2 ssh://borg-backup@diskstation/volume1/BorgBackup
borg init --encryption=repokey-blake2 /mnt/elements/public/backup/borg

Esecuzione

Adesso serve uno script da eseguire come root in cron. Questo va bene

#!/bin/sh
 
# Setting this, so the repo does not need to be given on the commandline:
export BORG_REPO=/mnt/elements/public/backup/borg
 
# See the section "Passphrase notes" for more infos.
export BORG_PASSPHRASE='s3cr3t'
 
# some helpers and error handling:
info() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; }
trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM
 
info "Starting backup"
 
# Backup the most important directories into an archive named after
# the machine this script is currently running on:
 
borg create                         \
    --verbose                       \
    --filter AME                    \
    --list                          \
    --stats                         \
    --show-rc                       \
    --compression lz4               \
    --exclude-caches                \
    --exclude '/home/*/.cache/*'    \
    --exclude '/var/cache/*'        \
    --exclude '/var/tmp/*'          \
    --exclude '/home/*/Downloads/*' \
    --exclude '/home/*/Dropbox/*'   \
                                    \
    ::'{hostname}-{now}'            \
    /etc                            \
    /home/gperon                    \
    /root                           \
 
    # /root                           \
    # /var                            \
 
backup_exit=$?
 
info "Pruning repository"
 
# Use the `prune` subcommand to maintain 7 daily, 4 weekly and 6 monthly
# archives of THIS machine. The '{hostname}-' prefix is very important to
# limit prune's operation to this machine's archives and not apply to
# other machines' archives also:
 
borg prune                          \
    --list                          \
    --prefix '{hostname}-'          \
    --show-rc                       \
    --keep-daily    7               \
    --keep-weekly   4               \
    --keep-monthly  6               \
 
prune_exit=$?
 
# use highest exit code as global exit code
global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
 
if [ ${global_exit} -eq 0 ]; then
    info "Backup and Prune finished successfully"
elif [ ${global_exit} -eq 1 ]; then
    info "Backup and/or Prune finished with warnings"
else
    info "Backup and/or Prune finished with errors"
fi
 
exit ${global_exit}

Ripristino da Backup

Per le attività di verifica e recupero da backup appoggiarsi ad un client come Vorta.

  1. Lanciare il programma da root e configurare le connessioni ai repository ad esempio elements e diskstation
  2. Fare Refresh degli archivi

    e montare un archivio su una cartella vuota
  3. a quel punto selezionare il file da recuperare o usare un programma come krusader per copiare il file dal punto di mount alla posizione di ripristino.
  4. Una volta terminato il recupero, smontare l'archivio e chiudere il programma.
backup.txt · Last modified: 2023/01/05 09:27 by gperon