admin:infrastructure:backup
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
admin:infrastructure:backup [2019/01/06 12:19] – [Log rotate] fpoulain | admin:infrastructure:backup [2024/12/29 09:19] (Version actuelle) – [Script de backup] correction d'une typo dans l'URL du remote (suppression ":") pilou | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
====== Paquet Debian ====== | ====== Paquet Debian ====== | ||
- | Le backup de chapril | + | Le backup de Chapril |
===== Aspects backup ===== | ===== Aspects backup ===== | ||
Ligne 7: | Ligne 7: | ||
==== Script de backup ==== | ==== Script de backup ==== | ||
- | <code bash src/backup.sh> | + | C'est fournis par [[https://torsion.org/borgmatic/ | borgmatic]]. |
- | #! /bin/bash | + | |
- | sleep $[$RANDOM % 30]m | + | On y adjoint une configuration dans ''/ |
- | logger=" | + | <code yaml /etc/borgmatic.d/root.yaml> |
- | borg_bin=" | + | location: |
- | backup_name=`date +%Y-%m-%d` | + | source_directories: |
- | backup_dest=" | + | - / |
+ | exclude_patterns: | ||
+ | - '/dev' | ||
+ | - '/ | ||
+ | - '/ | ||
+ | | ||
+ | - '/ | ||
+ | - '/ | ||
+ | - '/ | ||
+ | - '/ | ||
+ | - '/ | ||
+ | - '/ | ||
+ | repositories: | ||
+ | - ' | ||
- | export BORG_RSH=" | + | storage: |
+ | ssh_command: | ||
+ | archive_name_format: | ||
+ | # pour bullseye : borg_cache_directory: | ||
- | echo ======================================================================== >> $logger | + | consistency: |
- | echo " | + | |
- | echo ======================================================================== >> $logger | + | |
- | date >> | + | |
- | echo "" | + | |
- | echo "== Executing package selection: " >> $logger | + | retention: |
- | date >> | + | |
- | echo "" | + | |
- | dpkg --get-selections > / | + | |
- | echo "== Backup | + | hooks: |
- | date >> | + | before_backup: |
- | echo "" | + | - echo "Launching root backup at $(date -Iseconds)" |
+ | - for file in / | ||
+ | | ||
+ | - for file in / | ||
+ | - echo " | ||
+ | - borgmatic info --archive latest --json | ||
+ | on_error: | ||
+ | - echo " | ||
+ | # pour bullseye : | ||
+ | # after_check: | ||
+ | # - echo "Succeeded root checks at $(date -Iseconds)" | ||
+ | # after_prune: | ||
+ | # - echo " | ||
+ | </ | ||
- | for file in / | + | ==== Entrée Systemd ==== |
- | echo " | + | |
- | $file | + | |
- | done | + | |
- | echo "== Backup launch" | + | On déclenche avec un timer systemd qui retarde le démarrage avec un timing aléatoire pour éviter le ddos de [[admin: |
- | date >> | + | |
- | echo "" | + | |
- | $borg_bin create --stats $backup_dest:: | + | <code conf /etc/systemd/system/borgmatic.timer> |
- | --exclude | + | [Unit] |
- | --exclude | + | Description=Run borgmatic backup |
- | --exclude | + | |
- | --exclude / | + | |
- | --exclude / | + | |
- | --exclude / | + | |
- | --exclude '/ | + | |
- | | + | |
- | rc=$? | + | [Timer] |
- | if [[ $rc != 0 ]]; then exit $rc; fi | + | # Will trigger at 01:00 each day |
+ | # + 0-60 random minutes | ||
+ | # + 30 minutes delay from borgmatic.service | ||
+ | OnCalendar=*-*-* 01:00:00 | ||
+ | Persistent=true | ||
+ | RandomizedDelaySec=60 minutes | ||
- | echo "== Backup info" | + | [Install] |
- | date >> | + | WantedBy=timers.target |
- | echo "" | + | </code> |
- | $borg_bin info $backup_dest:: | + | <code conf / |
+ | [Unit] | ||
+ | Description=borgmatic backup | ||
+ | Wants=network-online.target | ||
+ | After=network-online.target | ||
+ | ConditionACPower=true | ||
- | echo "== Backup pruning" | + | [Service] |
- | date >> | + | Type=oneshot |
- | echo "" | + | |
- | $borg_bin prune -v --keep-daily=7 --keep-weekly=5 $backup_dest 2>&1 >> $logger | + | ## Lower CPU and I/O priority. |
+ | Nice=19 | ||
+ | CPUSchedulingPolicy=batch | ||
+ | IOSchedulingClass=best-effort | ||
+ | IOSchedulingPriority=7 | ||
+ | IOWeight=100 | ||
- | echo "== Backup post-hook" | + | ## Logs |
- | date >> | + | StandardOutput=syslog |
- | echo "" | + | StandardError=syslog |
- | + | SyslogIdentifier=borgmatic | |
- | for file in / | + | # Prevent rate limiting of borgmatic log events. |
- | [[ -f " | + | LogRateLimitIntervalSec=0 |
- | echo " | + | |
- | $file | + | |
- | done | + | |
- | + | ||
- | echo "" | + | |
- | date >> | + | |
- | echo Returned $rc >> | + | |
- | echo ======================================================================== >> $logger | + | |
- | + | ||
- | exit $rc | + | |
- | </ | + | |
- | + | ||
- | ==== Entrée Cron ==== | + | |
- | <code cron src/ | + | ## Launcher |
- | 00 2 * * * root bash / | + | # Delay start to prevent backups immediately upon system startup |
+ | ExecStartPre=sleep 30m | ||
+ | ExecStart=borgmatic -v1 | ||
+ | Restart=no | ||
</ | </ | ||
Ligne 126: | Ligne 145: | ||
su - postgres -c " | su - postgres -c " | ||
done | done | ||
+ | </ | ||
+ | |||
+ | <code bash scripts/ | ||
+ | #!/bin/bash | ||
+ | |||
+ | if test -x / | ||
+ | backup_dir=/ | ||
+ | db=icinga2 | ||
+ | |||
+ | # Prepare. | ||
+ | mkdir -p $backup_dir | ||
+ | chmod 700 $backup_dir | ||
+ | |||
+ | # Backup. | ||
+ | influxd backup -portable -database $db -host localhost: | ||
+ | |||
+ | # Prune. | ||
+ | find $backup_dir/ | ||
+ | fi | ||
</ | </ | ||
Ligne 223: | Ligne 261: | ||
exit 0 | exit 0 | ||
+ | </ | ||
+ | |||
+ | ==== Rsyslog ==== | ||
+ | |||
+ | <code conf / | ||
+ | if $programname == ' | ||
+ | & stop | ||
</ | </ | ||
==== Log rotate ==== | ==== Log rotate ==== | ||
- | <code conf debian/logrotate> | + | <code conf debian/borgmatic> |
- | /var/log/backup.log { | + | /var/log/borgmatic.log |
- | weekly | + | { |
- | | + | |
- | | + | |
- | | + | compress |
- | | + | missingok |
- | notifempty | + | notifempty |
- | create 644 root root | + | |
} | } | ||
</ | </ | ||
Ligne 242: | Ligne 286: | ||
C'est surtout du ssh. | C'est surtout du ssh. | ||
- | <code ssh / | + | <code ssh __felicette__/ |
command=" | command=" | ||
command=" | command=" | ||
Ligne 252: | Ligne 296: | ||
On a un script qui parse sur chaque machine le log de backup et qui est déployé par le paquet monitoring-plugins-chapril : | On a un script qui parse sur chaque machine le log de backup et qui est déployé par le paquet monitoring-plugins-chapril : | ||
- | <code python / | + | <code python / |
- | # | + | # |
- | # -*- encoding: | + | |
- | import datetime, os, re, locale | + | import datetime, itertools, os, re |
- | today= datetime.datetime.now () | + | now = datetime.datetime.now(datetime.timezone.utc) |
- | max_backup_delay = datetime.timedelta (1, 7200) | + | max_backup_delay = datetime.timedelta(1, |
- | def last_backup | + | def get_name(match): |
- | with open(log_file) as s: | + | return match.group(' |
- | | + | |
- | print "Last backup : " + logs_ok | + | def check_backup(filename): |
- | try: | + | with open(filename) as f: |
- | | + | |
- | except: | + | mixed_statuses = list(re.finditer(r'(? |
- | | + | for name, statuses in itertools.groupby(sorted(mixed_statuses, |
- | | + | last = sorted(statuses, |
+ | print(' | ||
+ | | ||
+ | | ||
+ | | ||
+ | failure.append(name) | ||
+ | failure = [] | ||
try: | try: | ||
- | | + | |
- | except: | + | except |
- | | + | |
- | if today - last_backup_date < max_backup_delay: | + | if failure: |
+ | exit (1) | ||
+ | else: | ||
exit (0) | exit (0) | ||
- | else: | ||
- | exit (1) | ||
</ | </ | ||
Et la conf icinga2 : | Et la conf icinga2 : | ||
- | <code conf / | + | <code conf __admin__/ |
object CheckCommand " | object CheckCommand " | ||
- | command = [ PluginDir + "/check_backup" ] | + | command = [ " |
} | } | ||
Ligne 297: | Ligne 346: | ||
} | } | ||
</ | </ | ||
- | |||
===== Aspects contrôle d' | ===== Aspects contrôle d' | ||
- | On contrôle directement chaque nuit sur la machine où les backups sont stockés ([[admin: | + | On contrôle directement chaque nuit sur la machine où les backups sont stockés ([[admin: |
==== Script de contrôle ==== | ==== Script de contrôle ==== | ||
- | <code bash / | + | <code bash __felicette__/ |
#! /bin/bash | #! /bin/bash | ||
Ligne 342: | Ligne 390: | ||
==== Entrée Cron ==== | ==== Entrée Cron ==== | ||
- | <code cron / | + | <code cron __felicette__/ |
00 4 * * * root bash / | 00 4 * * * root bash / | ||
</ | </ | ||
Ligne 348: | Ligne 396: | ||
==== Log rotate ==== | ==== Log rotate ==== | ||
- | <code conf / | + | <code conf __felicette__/ |
/ | / | ||
weekly | weekly | ||
Ligne 356: | Ligne 404: | ||
missingok | missingok | ||
notifempty | notifempty | ||
- | create 644 root root | + | create 644 backup backup |
} | } | ||
</ | </ | ||
- | ===== Configuration du monitoring | + | ==== Configuration du monitoring ==== |
On a un script qui parse sur la machine le log de check_backup : | On a un script qui parse sur la machine le log de check_backup : | ||
- | <code python / | + | <code python |
# | # | ||
# -*- encoding: | # -*- encoding: | ||
Ligne 394: | Ligne 442: | ||
Et la conf icinga2 : | Et la conf icinga2 : | ||
- | <code conf / | + | <code conf __admin__/ |
- | /* Backup checks */ | + | |
object CheckCommand " | object CheckCommand " | ||
- | command = [ LocalPluginDir + "/ | + | command = [ "/ |
} | } | ||
+ | </ | ||
+ | <code conf __admin__/ | ||
+ | /* Backup checks */ | ||
apply Service "Check Backup " { | apply Service "Check Backup " { | ||
import " | import " | ||
Ligne 406: | Ligne 455: | ||
command_endpoint = host.vars.client_endpoint | command_endpoint = host.vars.client_endpoint | ||
- | assign where host.name == "icinga2.cluster.chapril.org" | + | assign where host.name == "felicette.cluster.chapril.org" |
} | } | ||
</ | </ |
admin/infrastructure/backup.1546777156.txt.gz · Dernière modification : 2019/01/06 12:19 de fpoulain