edit crontab to store backup for postgres and gitea
This commit is contained in:
75
ansible/arcodange/factory/playbooks/backup/postgres.yml
Normal file
75
ansible/arcodange/factory/playbooks/backup/postgres.yml
Normal 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
|
||||
Reference in New Issue
Block a user