Skip to content

Commit

Permalink
Add a basic script to make a database backup
Browse files Browse the repository at this point in the history
Also a playbook to download the backup files.

It's crude, but in this case something is better than nothing.
  • Loading branch information
simonbaird committed Feb 14, 2021
1 parent 9d215af commit ad01216
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ rails/config/master.key
certs
etc/build-info.txt
etc/certbot-dns-user-credentials.yml
backups
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,12 @@ deploy-certs:
deploy-app:
$(PLAY) -v ansible/deploy.yml --tags=app

deploy-scripts:
$(PLAY) -v ansible/deploy.yml --tags=scripts

deploy-backup:
mkdir -p backups
$(PLAY) -v ansible/backup.yml

deploy-ssh:
@ssh fedora@tiddlyhost.com
31 changes: 31 additions & 0 deletions ansible/backup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

- hosts: all
become: yes
become_user: root

vars:
local_backup_dir: ../backups

tasks:
- name: Make a fresh backup of the database
command: "{{ bin_dir }}/db-backup"

- name: Download all the files
# Beware it will overwrite existing files
# TODO: Timestamps maybe?
fetch:
src: "{{ item }}"
dest: "{{ local_backup_dir }}/"
flat: yes
fail_on_missing: no
with_items:
- "{{ db_dump_file }}"
- "{{ db_dump_file }}.1"
- "{{ db_dump_file }}.2"
- "{{ db_dump_file }}.3"
- "{{ db_dump_file }}.4"
- "{{ db_dump_file }}.5"
- "{{ db_dump_file }}.6"
- "{{ db_dump_file }}.7"
- "{{ db_dump_file }}.8"
- "{{ db_dump_file }}.9"
23 changes: 20 additions & 3 deletions ansible/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
become_user: root

vars:
- home_dir: /home/{{ ansible_user }}
root_home_dir: /root

certbot_dns_credentials: ../etc/certbot-dns-user-credentials.yml
docker_compose_file: ../docker-compose-prod.yml

cert_host: tiddlyhost.com
letsencrypt_dir: /etc/letsencrypt/live/{{ cert_host }}
certs_dir: "{{ home_dir }}/certs"

db_name: app_production
db_user: postgres

tasks:

- name: Add repo for docker-ce
Expand Down Expand Up @@ -137,3 +137,20 @@
command:
cmd: docker-compose rm
tags: app

- name: Create bin dir
become: no
file:
path: "{{ bin_dir }}"
state: directory
tags: scripts

- name: Copy up some scripts
become: no
template:
src: templates/{{ item }}.j2
dest: "{{ bin_dir }}/{{ item }}"
mode: "0755"
with_items:
- db-backup
tags: scripts
7 changes: 7 additions & 0 deletions ansible/inventory.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,10 @@ all:
hosts:
tiddlyhost.com:
ansible_user: fedora

vars:
root_home_dir: /root
home_dir: /home/{{ ansible_user }}
bin_dir: "{{ home_dir }}/bin"
backups_dir: "{{ home_dir }}/backups"
db_dump_file: "{{ backups_dir }}/dbdump.gz"
13 changes: 13 additions & 0 deletions ansible/templates/db-backup.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

mkdir -p {{ backups_dir }}

# Keep a few old backups
for i in {8..0}; do
[[ -f {{ db_dump_file }}.$i ]] && mv {{ db_dump_file }}.$i {{ db_dump_file }}.$((i+1))
done
mv {{ db_dump_file }} {{ db_dump_file }}.0

# Make a new backup
sudo docker-compose exec db bash -c \
'pg_dump --dbname={{ db_name }} --username={{ db_user }}' | gzip > {{ db_dump_file }}

0 comments on commit ad01216

Please sign in to comment.