Add database backup

This commit is contained in:
sfigato 2021-02-21 00:05:41 +01:00
parent 6d2c30acff
commit 202083aeb0
Signed by: blallo
GPG Key ID: 0CBE577C9B72DC3F
9 changed files with 98 additions and 18 deletions

View File

@ -1,2 +1,7 @@
--- ---
- name: reload postgres_backup service
systemd:
name: postgres_backup.service
state: reloaded
daemon_reload: yes
# handlers file for ansible-drone # handlers file for ansible-drone

View File

@ -16,8 +16,6 @@
tags: tags:
- configuration - configuration
- include_tasks: custom_volumes.yml
- name: run drone agent - name: run drone agent
docker_container: docker_container:
name: "drone-agent" name: "drone-agent"

View File

@ -1,10 +1,24 @@
--- ---
- name: Creates postgres directory - name: Creates postgres backup directory
file: file:
path: "{{ drone_postgres_data_dir }}" path: "{{ drone_postgres_backup_dir }}/backup"
state: directory state: directory
owner: root
group: root
mode: 0755
register: drone_postgres_backup_dir_state
when: drone_database_driver == "postgres"
tags:
- postgres
- database
- name: Creates postgres recovery directory
file:
path: "{{ drone_postgres_backup_dir }}/restore"
state: directory
owner: root
group: root
mode: 0755 mode: 0755
register: drone_postgres_data_dir
when: drone_database_driver == "postgres" when: drone_database_driver == "postgres"
tags: tags:
- postgres - postgres
@ -19,18 +33,46 @@
ports: ports:
- "5432:5432" - "5432:5432"
volumes: volumes:
- "{{ drone_postgres_data_dir }}:/var/lib/postgresql/data" - "postgres_data:/var/lib/postgresql/data"
- "{{ drone_postgres_backup_dir }}/backup:/var/lib/pgbackup"
- "{{ drone_postgres_backup_dir }}/restore:/docker-entrypoint-initdb.d:ro"
env: env:
POSTGRES_PASSWORD: "{{ drone_postgres_password }}" POSTGRES_PASSWORD: "{{ drone_postgres_password }}"
POSTGRES_USER: "{{ drone_postgres_user }}" POSTGRES_USER: "{{ drone_postgres_user }}"
POSTGRES_DB: "{{ drone_postgres_db }}" POSTGRES_DB: "{{ drone_postgres_db }}"
when: drone_postgres_data_dir is defined and drone_postgres_data_dir.changed or when: drone_postgres_backup_dir_state is defined and drone_postgres_backup_dir_state.changed or
drone_database_driver == "postgres" drone_database_driver == "postgres"
tags: tags:
- postgres - postgres
- database - database
- skip_ansible_lint - skip_ansible_lint
- name: create backup service for drone postgres database
template:
src: "templates/{{ item.src }}"
dest: "{{ item.dest }}"
owner: root
group: root
mode: "{{ item.mode }}"
vars:
backup_frequency: "{{ drone_postgres_backup_frequency|default('daily') }}"
backup_path: "{{ drone_postgres_backup_dir }}"
with_items:
- {src: postgres_backup.service.j2, dest: /etc/systemd/system/postgres_backup.service, mode: "0644"}
- {src: postgres_backup.timer.j2, dest: /etc/systemd/system/postgres_backup.timer, mode: "0644"}
- {src: pg_backup.sh.j2, dest: /usr/local/bin/pg_backup.sh, mode: "0755"}
notify: reload postgres_backup service
when: drone_postgres_backup_dir_state is defined and drone_postgres_backup_dir_state.changed or
drone_database_driver == "postgres"
tags:
- postgres
- database
- name: ensure backup service for drone postgres database is enabled
systemd:
name: postgres_backup.timer
enabled: yes
- name: Creates mysql directory - name: Creates mysql directory
file: file:
path: "{{ drone_mysql_data_dir }}" path: "{{ drone_mysql_data_dir }}"

View File

@ -79,7 +79,7 @@
traefik.basic.port: "80" traefik.basic.port: "80"
traefik.basic.protocol: "http" traefik.basic.protocol: "http"
when: ((drone_server_config is defined and drone_server_config.changed) or when: ((drone_server_config is defined and drone_server_config.changed) or
(drone_postgres_data_dir is defined and drone_postgres_data_dir.changed)) and (drone_postgres_backup_dir_state is defined and drone_postgres_backup_dir_state.changed)) and
drone_database_driver != "sqlite" drone_database_driver != "sqlite"
tags: tags:
- drone-server - drone-server

View File

@ -1,8 +1,9 @@
--- ---
- include: setup.yml - include: setup.yml
- include: drone-database.yml - include: drone-database.yml
when: drone_server_enable == "true" and drone_database_driver != "sqlite" when: drone_server_enable == "true" and drone_database_driver != "sqlite"
- include: drone-server.yml - include: drone-server.yml
when: drone_server_enable == "true" when: drone_server_enable == "true"
- include: drone-agent.yml - include: drone-agent.yml
when: drone_agent_enable == "true" when: drone_agent_enable == "true"
- include_tasks: custom_volumes.yml

16
templates/pg_backup.sh.j2 Executable file
View File

@ -0,0 +1,16 @@
#!/bin/bash
set -xeo pipefail
NOW=$(date +%Y-%m-%d_%H:%M:%S)
BACKUP_PATH="{{ backup_path }}/backup"
RESTORE_PATH="{{ backup_path }}/restore"
cd ${BACKUP_PATH}
docker exec postgres pg_dump -U drone -d drone -f /var/lib/pgbackup/backup_${NOW}.sql
LATEST_BACKUP=$(ls -1t *.sql | head -n1)
if [ "z${LATEST_BACKUP}" != "z" ]; then
cp "${BACKUP_PATH}/${LATEST_BACKUP}" "${RESTORE_PATH}/latest_backup.sql"
fi
# vim: set ft=sh et sw=0 ts=2 sts=0:

View File

@ -0,0 +1,9 @@
[Unit]
Description=Backup the drone database in postgres
[Service]
ExecStart=/usr/local/bin/pg_backup.sh
# vim: set ft=dosini:

View File

@ -0,0 +1,11 @@
[Unit]
Description=Trigger the backup of the drone database
[Timer]
OnCalendar={{ backup_frequency }}
Persistent=True
[Install]
WantedBy=timers.target
# vim: set ft=dosini:

View File

@ -1,2 +0,0 @@
---
# vars file for ansible-drone