Post

💾 Sauvegarde/Restauration dossier (rsync+timer)

💾 Sauvegarde/Restauration dossier (rsync+timer)

Sauvegarde

une sauvegarde “miroir exact” de /sharenfs sur 3 jours max avec rsync avec seulement des echo contrôlés par VERBOSE=1 + rsync --info=progress2.

Script de sauvegarde (1x/jour)

Création /usr/local/sbin/backup-sharenfs-rsync.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/bin/bash
set -euo pipefail

. "/sharenfs/scripts/env/ntfy.env"

VERBOSE="${VERBOSE:-0}"

log() { [ "$VERBOSE" = "1" ] && echo "[backup-sharenfs-rsync] $*"; }

SRC="/sharenfs/"
DEST_BASE="/mnt/FreeUSB2To/sauvegardes/sharenfs"
TODAY="$(date +%F)"
DEST="$DEST_BASE/$TODAY"

log "VERBOSE=$VERBOSE"
log "SRC=$SRC"
log "DEST_BASE=$DEST_BASE"
log "TODAY=$TODAY"
log "DEST=$DEST"

mkdir -p "$DEST"
log "mkdir -p $DEST"

RSYNC_OPTS=(-aHAX --delete --numeric-ids --info=progress2)
if [ "$VERBOSE" = "1" ]; then
  RSYNC_OPTS+=(--verbose)
else
  RSYNC_OPTS+=(--quiet)
fi

log "rsync start"
rsync "${RSYNC_OPTS[@]}" "$SRC" "$DEST"/
log "rsync exit code: $?"

log "done"
# Message
curl \
-H "X-Email: $NTFY_EMAIL" \
-H "Title: ℹ️ backup local sharenfs" \
-H "Authorization: Bearer $NTFY_TOKEN" \
-H prio:low \
-d "CWWK Debian 13
 ✔️  Fin rsync $SRC vers $DEST_BASE `date +%d/%m/%Y-%Hh%M`" \
"$NTFY_URL"

Puis :

1
chmod +x /usr/local/sbin/backup-sharenfs-rsync.sh

Purge (garder 3 jours)

Création /usr/local/sbin/purge-backup-sharenfs.sh :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/bin/bash
set -euo pipefail

VERBOSE="${VERBOSE:-0}"

log() { [ "$VERBOSE" = "1" ] && echo "[purge-backup-sharenfs] $*"; }

DEST_BASE="/mnt/FreeUSB2To/sauvegardes/sharenfs"
KEEP_DAYS=3
cutoff="$(date -d "$KEEP_DAYS days ago" +%F)"

log "VERBOSE=$VERBOSE"
log "DEST_BASE=$DEST_BASE"
log "KEEP_DAYS=$KEEP_DAYS"
log "cutoff=$cutoff"

shopt -s nullglob
for d in "$DEST_BASE"/*; do
  [ -d "$d" ] || continue
  name="$(basename "$d")"
  if [[ "$name" < "$cutoff" ]]; then
    log "removing $d"
    rm -rf "$d"
  else
    log "keeping $d"
  fi
done

log "done"

Puis :

1
chmod +x /usr/local/sbin/purge-backup-sharenfs.sh

Exécution manuelle (verbose)

1
VERBOSE=1 /usr/local/sbin/backup-sharenfs-rsync.sh

(Le purge se lance ensuite via systemd avec ExecStartPost.)

Planification

systemd timer (1x/jour) + purge (garde 3 jours) pour /sharenfs.

1) Service

Crée /etc/systemd/system/backup-sharenfs-rsync.service :

1
2
3
4
5
6
7
[Unit]
Description=Daily rsync backup of /sharenfs (keeps 3 days)

[Service]
Type=oneshot
ExecStart=/usr/local/sbin/backup-sharenfs-rsync.sh
ExecStartPost=/usr/local/sbin/purge-backup-sharenfs.sh

2) Timer

Crée /etc/systemd/system/backup-sharenfs-rsync.timer :

1
2
3
4
5
6
7
8
9
10
[Unit]
Description=Run daily backup-sharenfs-rsync

[Timer]
OnCalendar=daily
RandomizedDelaySec=15m
Persistent=true

[Install]
WantedBy=timers.target

3) Activer

1
2
3
systemctl daemon-reload
systemctl enable --now backup-sharenfs-rsync.timer
systemctl list-timers | grep sharenfs

Assure-toi que tes scripts existent et sont exécutables :

  • /usr/local/sbin/backup-sharenfs-rsync.sh
  • /usr/local/sbin/purge-backup-sharenfs.sh

Restauration

Choisir le jour à restaurer (ex. YYYY-MM-DD) et vérifier le dossier :

1
ls -lh /mnt/FreeUSB2To/sauvegardes/sharenfs/YYYY-MM-DD/

(Option recommandé) Stopper tout ce qui écrit dans /sharenfs pendant la restauration (ex. si un service recrée des fichiers).

Faire une restauration miroir (remplace le contenu de /sharenfs/ par la sauvegarde)

1
2
3
4
# restauration miroir
SRC="/mnt/FreeUSB2To/sauvegardes/sharenfs/YYYY-MM-DD/"
DEST="/sharenfs/"
rsync -aHAX --delete --numeric-ids --info=progress2 "$SRC" "$DEST"/

Faire une restauration en merge

1
2
3
SRC="/mnt/FreeUSB2To/sauvegardes/sharenfs/YYYY-MM-DD/"
DEST="/sharenfs/"
rsync -aHAX --numeric-ids --info=progress2 --ignore-existing "$SRC" "$DEST"/

Vérification rapide

1
2
df -h /sharenfs
du -sh /sharenfs/

Si besoin, redémarrer les services qui ont été stoppés

Cet article est sous licence CC BY 4.0 par l'auteur.