edit crontab to store backup for postgres and gitea

This commit is contained in:
2025-08-28 19:35:52 +02:00
parent c5a8d5ef52
commit c6807851c5
4 changed files with 183 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
---
- name: backup
ansible.builtin.import_playbook: ./backup/backup.yml

View File

@@ -0,0 +1,12 @@
---
# - name: postgres
# 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"

View File

@@ -0,0 +1,93 @@
---
- name: Backup Gitea
hosts: gitea
gather_facts: yes
become: yes
vars:
gitea_container_name: "{{ gitea.dockercompose.services.gitea.container_name }}"
gitea_user: "git"
backup_dir: "{{ backup_root_dir }}/{{ backup_dirname }}"
scripts_dir: "/home/pi/arcodange/docker_composes/gitea/scripts"
keep_days: 15
tasks:
- name: S'assurer que le répertoire de backup existe
file:
path: "{{ backup_dir }}"
state: directory
mode: '0755'
- name: define backup command
set_fact:
backup_cmd: >-
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 -
- name: test backup_cmd
ansible.builtin.shell: |
{{ backup_cmd }} > /dev/null
- name: Ajouter une tâche cron pour backup Gitea tous les jours à 4h
cron:
name: "Backup Gitea archive"
minute: "0"
hour: "4"
user: root
job: >-
{{ backup_cmd }} > {{ backup_dir }}/backup_$(date +\\%Y\\%m\\%d).gitea.gz
&& find {{ backup_dir }} -type f -name 'backup_*.gitea.gz' -mtime +{{ keep_days }} -delete
- name: Créer le script de restauration
copy:
dest: "{{ scripts_dir }}/restore.sh"
mode: '0755'
content: |
#!/bin/bash
set -e
CONTAINER_NAME="{{ gitea_container_name }}"
BACKUP_DIR="{{ backup_dir }}"
if [ -z "$1" ]; then
FILE=$(ls -1t "$BACKUP_DIR"/backup_*.gitea.gz | head -n 1)
echo "Aucune date fournie, restauration du dernier dump : $FILE"
else
FILE="$BACKUP_DIR/backup_$1.gitea.gz"
if [ ! -f "$FILE" ]; then
echo "Fichier $FILE introuvable"
exit 1
fi
fi
echo "Copie du dump dans le container..."
docker cp "$FILE" "$CONTAINER_NAME":/tmp/
BASENAME=$(basename "$FILE" .gitea.gz)
docker exec --user git "$CONTAINER_NAME" mkdir -p /tmp/$BASENAME/
echo "Décompression du dump..."
docker exec --user git "$CONTAINER_NAME" tar -xzf "/tmp/"$BASENAME".gitea.gz" --directory /tmp/$BASENAME/
echo "Restauration des données..."
docker exec --user git "$CONTAINER_NAME" bash -c "
cd /tmp/$BASENAME/data &&
find . -type d -exec mkdir -p /data/gitea/{} \; &&
find . -type f -exec mv {} /data/gitea/{} \;
"
docker exec --user root "$CONTAINER_NAME" bash -c "
cd /tmp/$BASENAME/repos &&
find . -type d -exec mkdir -p /data/git/repositories/{} \; &&
find . -type f -exec mv {} /data/git/repositories/{} \;
"
echo "Réglage des permissions..."
docker exec "$CONTAINER_NAME" chown -R git:git /data
echo "Régénération des hooks..."
docker exec --user git "$CONTAINER_NAME" /usr/local/bin/gitea -c '/data/gitea/conf/app.ini' admin regenerate hooks
echo "Netoyage"
docker exec "$CONTAINER_NAME" find /tmp/ -maxdepth 1 -name "$BASENAME*" -exec rm -rf {} + -depth
echo "Restauration Gitea terminée."

View File

@@ -0,0 +1,75 @@
---
- name: Backup Postgres
hosts: postgres
gather_facts: yes
become: yes
vars:
postgres_container_name: "{{ postgres.dockercompose.services.postgres.container_name }}"
postgres_user: "{{ postgres.dockercompose.services.postgres.environment.POSTGRES_USER }}"
backup_dir: "{{ backup_root_dir }}/{{ backup_dirname }}"
scripts_dir: "/home/pi/arcodange/docker_composes/postgres/scripts"
keep_days: 15
tasks:
- name: S'assurer que le répertoire de backup existe
file:
path: "{{ backup_dir }}"
state: directory
mode: '0755'
- name: define backup command
set_fact:
backup_cmd: "docker exec {{ postgres_container_name }} pg_dumpall -U {{ postgres_user }}"
- name: test backup_cmd
ansible.builtin.shell: |
{{ backup_cmd }} > /dev/null
- name: Ajouter une tâche cron pour dump PostgreSQL tous les jours à 4h avec compression
cron:
name: "Backup PostgreSQL compressé"
minute: "0"
hour: "4"
user: root
job: >-
{{ backup_cmd }} | gzip > {{ backup_dir }}/backup_$(date +\\%Y\\%m\\%d).sql.gz
&& find {{ backup_dir }} -type f -name 'backup_*.sql.gz' -mtime +{{ keep_days }} -delete
- name: Créer le script de restauration
copy:
dest: "{{ scripts_dir }}/restore.sh"
mode: '0755'
content: |
#!/bin/bash
set -e
CONTAINER_NAME="{{ postgres_container_name }}"
POSTGRES_USER="{{ postgres_user }}"
BACKUP_DIR="{{ backup_dir }}"
if [ -z "$1" ]; then
FILE=$(ls -1t "$BACKUP_DIR"/backup_*.sql.gz | head -n 1)
echo "Aucune date fournie, restauration du dernier dump : $FILE"
else
FILE="$BACKUP_DIR/backup_$1.sql.gz"
if [ ! -f "$FILE" ]; then
echo "Fichier $FILE introuvable"
exit 1
fi
fi
echo "Copie du fichier dans le container..."
docker cp "$FILE" "$CONTAINER_NAME":/tmp/restore.sql.gz
echo "Décompression dans le container..."
docker exec "$CONTAINER_NAME" sh -c "gunzip -f /tmp/restore.sql.gz"
echo "Restauration dans PostgreSQL..."
docker exec -i "$CONTAINER_NAME" psql -U "$POSTGRES_USER" -f /tmp/restore.sql
echo "Restauration terminée."
echo "Si cela ne fonctionne pas, supprimez le contenu du dossier data avant de rejouer le script"
# docker exec -u git gitea
# gitea dump --skip-log --skip-db --type tar.gz -c /data/gitea/conf/app.ini -C /data/gitea/ -f /tmp/backup_20250828.gitea.gz