Add database backup
This commit is contained in:
parent
6d2c30acff
commit
202083aeb0
|
@ -1,2 +1,7 @@
|
|||
---
|
||||
- name: reload postgres_backup service
|
||||
systemd:
|
||||
name: postgres_backup.service
|
||||
state: reloaded
|
||||
daemon_reload: yes
|
||||
# handlers file for ansible-drone
|
|
@ -16,8 +16,6 @@
|
|||
tags:
|
||||
- configuration
|
||||
|
||||
- include_tasks: custom_volumes.yml
|
||||
|
||||
- name: run drone agent
|
||||
docker_container:
|
||||
name: "drone-agent"
|
||||
|
|
|
@ -1,10 +1,24 @@
|
|||
---
|
||||
- name: Creates postgres directory
|
||||
- name: Creates postgres backup directory
|
||||
file:
|
||||
path: "{{ drone_postgres_data_dir }}"
|
||||
path: "{{ drone_postgres_backup_dir }}/backup"
|
||||
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
|
||||
register: drone_postgres_data_dir
|
||||
when: drone_database_driver == "postgres"
|
||||
tags:
|
||||
- postgres
|
||||
|
@ -19,18 +33,46 @@
|
|||
ports:
|
||||
- "5432:5432"
|
||||
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:
|
||||
POSTGRES_PASSWORD: "{{ drone_postgres_password }}"
|
||||
POSTGRES_USER: "{{ drone_postgres_user }}"
|
||||
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"
|
||||
tags:
|
||||
- postgres
|
||||
- database
|
||||
- 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
|
||||
file:
|
||||
path: "{{ drone_mysql_data_dir }}"
|
||||
|
|
|
@ -79,7 +79,7 @@
|
|||
traefik.basic.port: "80"
|
||||
traefik.basic.protocol: "http"
|
||||
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"
|
||||
tags:
|
||||
- drone-server
|
||||
|
|
|
@ -6,3 +6,4 @@
|
|||
when: drone_server_enable == "true"
|
||||
- include: drone-agent.yml
|
||||
when: drone_agent_enable == "true"
|
||||
- include_tasks: custom_volumes.yml
|
||||
|
|
16
templates/pg_backup.sh.j2
Executable file
16
templates/pg_backup.sh.j2
Executable 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:
|
9
templates/postgres_backup.service.j2
Normal file
9
templates/postgres_backup.service.j2
Normal 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:
|
11
templates/postgres_backup.timer.j2
Normal file
11
templates/postgres_backup.timer.j2
Normal 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:
|
|
@ -1,2 +0,0 @@
|
|||
---
|
||||
# vars file for ansible-drone
|
Loading…
Reference in New Issue
Block a user