implement systemd timer for backing up the database

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2020-12-08 21:50:43 +01:00
parent 9f8009520d
commit 5c9096811e
No known key found for this signature in database
GPG Key ID: 4EF064D0E6D63020
4 changed files with 62 additions and 0 deletions

View File

@ -44,3 +44,29 @@ Configuration consists of two templates:
* `grafana.ini.j2` - Grafana main configuration file.
* `prometheus.yml.j2` - Initial configuration of the query backend(s).
# Backups
Done by a Sytemd timer daily:
```
> sudo systemctl list-timers 'grafana-backup'
NEXT LEFT LAST PASSED UNIT ACTIVATES
Wed 2020-12-09 00:00:00 UTC 3h 10min left n/a n/a grafana-backup.timer grafana-backup.service
> sudo systemctl status grafana-backup
● grafana-backup.service - Grafana batabase backup
Loaded: loaded (/lib/systemd/system/grafana-backup.service; static; vendor preset: enabled)
Active: inactive (dead) since Tue 2020-12-08 20:48:45 UTC; 6s ago
TriggeredBy: ● grafana-backup.timer
Docs: https://github.com/status-im/infra-role-systemd-timer
Process: 1938613 ExecStart=/usr/local/bin/grafana-backup (code=exited, status=0/SUCCESS)
Main PID: 1938613 (code=exited, status=0/SUCCESS)
```
```
> ls -lh /var/backups/grafana
total 2.3M
-rw-r--r-- 1 root root 563K Dec 5 20:20 grafana-20201205-202043.db.gz
-rw-r--r-- 1 root root 563K Dec 6 20:48 grafana-20201206-204838.db.gz
-rw-r--r-- 1 root root 563K Dec 7 20:48 grafana-20201207-204840.db.gz
-rw-r--r-- 1 root root 563K Dec 8 20:48 grafana-20201208-204845.db.gz
```

View File

@ -41,6 +41,13 @@ grafana_smtp_port: ~
grafana_smtp_user: ~
grafana_smtp_pass: ~
# Backups
grafana_backup_dir: '/var/backups/{{ grafana_service_name }}'
grafana_backup_user: 'root'
grafana_backup_frequency: 'daily'
grafana_backup_timeout_sec: 30
grafana_backup_max_kept: 14
# optional anonymous access
grafana_anonymous: false

28
tasks/backup.yml Normal file
View File

@ -0,0 +1,28 @@
---
- name: Create backups directory
file:
path: '{{ grafana_backup_dir }}'
owner: '{{ grafana_backup_user }}'
group: 'adm'
state: directory
mode: 0750
- name: Create backup timer service
include_role: name=systemd-timer
vars:
systemd_timer_description: 'Grafana batabase backup'
systemd_timer_name: '{{ grafana_service_name }}-backup'
systemd_timer_user: '{{ grafana_backup_user }}'
systemd_timer_frequency: '{{ grafana_backup_frequency }}'
systemd_timer_timeout_sec: '{{ grafana_backup_timeout_sec }}'
systemd_timer_work_dir: '{{ grafana_backup_dir }}'
systemd_timer_start_on_creation: true
systemd_timer_script_content: |
#!/usr/bin/env bash
MAX_KEPT={{ grafana_backup_max_kept }}
DB_PATH="{{ grafana_cont_vol }}/lib/grafana.db"
TSAMP=$(date -u +%Y%m%d-%H%M%S)
echo "Saving Grafana database..."
gzip -c "${DB_PATH}" > "grafana-${TSAMP}.db.gz"
echo "Cleaning up old archives..."
rm -vf $(ls -Art | head -n -${MAX_KEPT})

View File

@ -2,3 +2,4 @@
- import_tasks: config.yml
- import_tasks: container.yml
- import_tasks: consul.yml
- import_tasks: backup.yml