backup k3s volumes

This commit is contained in:
2026-01-22 19:05:31 +01:00
parent 451dfa5133
commit 35a4cea214
4 changed files with 102 additions and 13 deletions

View File

@@ -1,15 +1,21 @@
--- ---
- name: setup cron report # - name: setup cron report
ansible.builtin.import_playbook: cron_report.yml # ansible.builtin.import_playbook: cron_report.yml
- name: postgres # - name: postgres
ansible.builtin.import_playbook: postgres.yml # ansible.builtin.import_playbook: postgres.yml
# vars:
# backup_root_dir: "/mnt/backups"
# backup_dirname: "postgres"
# - name: gitea
# ansible.builtin.import_playbook: gitea.yml
# vars:
# backup_root_dir: "/mnt/backups"
# backup_dirname: "gitea"
- name: k3s_pvc
ansible.builtin.import_playbook: k3s_pvc.yml
vars: vars:
backup_root_dir: "/mnt/backups" backup_root_dir: "/mnt/backups"
backup_dirname: "postgres" backup_dirname: "k3s_pvc"
- name: gitea
ansible.builtin.import_playbook: gitea.yml
vars:
backup_root_dir: "/mnt/backups"
backup_dirname: "gitea"

View File

@@ -24,7 +24,7 @@
name: name:
- postfix - postfix
- msmtp - msmtp
- msmtp-mta # - msmtp-mta # conflicts with recent pi setup - may be required by pi2 with old setup
- mailutils - mailutils
state: present state: present
update_cache: yes update_cache: yes

View File

@@ -22,7 +22,7 @@
set_fact: set_fact:
backup_cmd: >- backup_cmd: >-
docker exec -u {{ gitea_user }} {{ gitea_container_name }} docker exec -u {{ gitea_user }} {{ gitea_container_name }}
gitea dump --skip-log --skip-db --type tar.gz -c /data/gitea/conf/app.ini -C /data/gitea/ -f - gitea dump --skip-log --skip-db --skip-package-data --type tar.gz -c /data/gitea/conf/app.ini -C /data/gitea/ -f -
- name: test backup_cmd - name: test backup_cmd
ansible.builtin.shell: | ansible.builtin.shell: |

View File

@@ -0,0 +1,83 @@
---
- name: Backup K3S Persistent Volumes
hosts: pi1
gather_facts: yes
become: yes
vars:
backup_dir: "{{ backup_root_dir }}/{{ backup_dirname }}"
scripts_dir: "/opt/k3s_volumes"
keep_days: 15
tasks:
- name: S'assurer que le répertoire de backup existe
file:
path: "{{ backup_dir }}"
state: directory
mode: '0755'
- name: S'assurer que le répertoire de scripts existe
file:
path: "{{ scripts_dir }}"
state: directory
mode: '0755'
- name: define backup command
set_fact:
backup_cmd: |-
echo "
$(kubectl get -A pv -o yaml)
---
$(kubectl get -A pvc -o yaml)
"
- name: test backup_cmd
ansible.builtin.shell: |
{{ backup_cmd }} > /dev/null
- name: Créer le script de backup
copy:
dest: "{{ scripts_dir }}/backup.sh"
mode: '0755'
content: |
#!/bin/bash
set -e
mkdir -p {{ backup_dir }}
{{ backup_cmd }} > {{ backup_dir }}/backup_$(date +\%Y\%m\%d).volumes
find {{ backup_dir }} -type f -name 'backup_*.volumes' -mtime +{{ keep_days }} -delete
SCRIPTS_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
{{ backup_cmd }} > $SCRIPTS_DIR/backup.volumes
- name: Ajouter une tâche cron pour backup k3s volumes tous les jours à 4h
cron:
name: "Backup K3S Volumes"
minute: "0"
hour: "4"
user: root
job: "{{ scripts_dir }}/backup.sh"
- name: Créer le script de restauration
copy:
dest: "{{ scripts_dir }}/restore.sh"
mode: '0755'
content: |
#!/bin/bash
set -e
BACKUP_DIR="{{ backup_dir }}"
if [ -z "$1" ]; then
FILE=$(ls -1t "$BACKUP_DIR"/backup_*.volumes | head -n 1)
echo "Aucune date fournie, restauration du dernier dump : $FILE"
else
FILE="$BACKUP_DIR/backup_$1.volumes"
if [ ! -f "$FILE" ]; then
echo "Fichier $FILE introuvable"
exit 1
fi
fi
kubectl apply -f "$FILE"
echo "Restauration des volumes k3s terminée."