switch to using systemd service and timer instead of cron

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2020-01-24 16:34:25 +01:00
parent 226676e7d7
commit 1aa0ca5ff2
No known key found for this signature in database
GPG Key ID: 4EF064D0E6D63020
8 changed files with 103 additions and 38 deletions

View File

@ -34,3 +34,27 @@ If you want to use S3 you can set:
```yaml
backup_base_domain: s3.amazonaws.com
```
# Administration
The upload job runs as a systemd service triggered by a timer.
Assuming our backup target is called `database` you can do:
```
$ sudo systemctl status backup-database.service
● backup-database.service - "Service for uploading database backups to s3 buckets."
Loaded: loaded (/lib/systemd/system/backup-database.service; static; vendor preset: enabled)
Active: inactive (dead) since Fri 2020-01-24 15:08:57 UTC; 7min ago
Docs: https://github.com/status-im/infra-role-s3cmd-backup
Process: 15536 ExecStart=/var/lib/backups/backup_hackmd.sh (code=exited, status=0/SUCCESS)
Main PID: 15536 (code=exited, status=0/SUCCESS)
Jan 24 15:08:44 node-01.do-ams3.todo.misc systemd[1]: Starting "Service for uploading database backups to s3 buckets."...
Jan 24 15:08:44 node-01.do-ams3.todo.misc backup_database.sh[15536]: Uploading: database_db_dump_20200124040001.sql >> s3://hackmd-backups
...
```
You can check the timer status too:
```
$ sudo systemctl list-timers backup-database.timer
NEXT LEFT LAST PASSED UNIT ACTIVATES
Sat 2020-01-25 00:00:00 UTC 8h left n/a n/a backup-database.timer backup-database.service
```

View File

@ -1,24 +1,28 @@
---
# For uploading backups
# tool for uploading to s3 compatible backends
s3cmd_version: '2.0.2'
s3cmd_name: 's3cmd-{{ s3cmd_version }}'
s3cmd_archive: '{{ s3cmd_name }}.tar.gz'
s3cmd_checksum: 'md5:7e4043186f46a6832ba3fb4d9cb59bb8'
s3cmd_url: 'https://github.com/s3tools/s3cmd/releases/download/v{{ s3cmd_version }}/{{ s3cmd_archive }}'
# Backup timing
backup_hour: 2
backup_minute: 0
backup_day: '*'
backup_month: '*'
backup_name: 'default'
backup_directory: ~
backup_script_dir: '/var/lib/backups'
backup_script: '{{ backup_script_dir }}/backup_{{ backup_name }}.sh'
backup_service_path: '/lib/systemd/system'
backup_service_name: 'backup-{{ backup_name }}'
backup_service_user: root
# It takes a bit to upload files
backup_service_start_timeout: 120
# Optionally wait for specified service to run
backup_service_extra_after: ~
# Backup frequency in systemd OnCalendar format
backup_timer_frequency: 'daily'
# Number of most recent files to backup
backup_number: 1
# Digital Ocean Spaces configuration
backup_name: 'default'
backup_directory: ~
backup_script: '/var/lib/backups/backup_{{ backup_name }}.sh'
#backup_base_domain: s3.amazonaws.com
backup_base_domain: ams3.digitaloceanspaces.com
# example: s3://discourse-backups
backup_bucket_name: ~

View File

@ -1,27 +0,0 @@
---
- name: Create directory for backup script
file:
path: '{{ item }}'
state: directory
group: adm
mode: 0775
with_items:
- /var/tmp/backups
- /var/lib/backups
- name: Create backup script
template:
src: backup.sh
dest: '{{ backup_script }}'
mode: 0755
- name: Configure a cron job for backups
cron:
name: Upload Discourse Backups
# backup happens at 03:30 UTC
hour: '{{ backup_hour }}'
minute: '{{ backup_minute }}'
day: '{{ backup_day }}'
month: '{{ backup_month }}'
job: '{{ backup_script }}'
user: root

View File

@ -1,3 +1,4 @@
---
- include_tasks: s3cmd.yml
- include_tasks: backups.yml
- include_tasks: script.yml
- include_tasks: timer.yml

16
tasks/script.yml Normal file
View File

@ -0,0 +1,16 @@
---
- name: Create directory for backup script
file:
path: '{{ item }}'
state: directory
group: adm
mode: 0775
with_items:
- '{{ backup_script_dir }}'
- '{{ backup_directory }}'
- name: Create backup script
template:
src: backup.sh
dest: '{{ backup_script }}'
mode: 0755

27
tasks/timer.yml Normal file
View File

@ -0,0 +1,27 @@
---
- name: 'Create systemd service file: {{ backup_service_name }}'
template:
src: backup.service.j2
dest: '{{ backup_service_path }}/{{ backup_service_name }}.service'
mode: 0644
- name: 'Create systemd timer file: {{ backup_service_name }}'
template:
src: backup.timer.j2
dest: '{{ backup_service_path }}/{{ backup_service_name }}.timer'
mode: 0644
- name: Reload systemctl
command: systemctl daemon-reload
- name: '(Re)start fetching service: {{ backup_service_name }}'
service:
name: '{{ backup_service_name }}.service'
state: started
enabled: true
- name: 'Enable the service timer: {{ backup_service_name }}'
systemd:
name: '{{ backup_service_name }}.timer'
state: started
enabled: yes

View File

@ -0,0 +1,14 @@
[Unit]
Description="Service for uploading {{ backup_name }} backups to s3 buckets."
Documentation=https://github.com/status-im/infra-role-s3cmd-backup
Requires=network-online.target
After=network-online.target
{% if backup_service_extra_after is defined %}
After={{ backup_service_extra_after }}
{% endif %}
[Service]
User={{ backup_service_user }}
ExecStart={{ backup_script }}
Type=oneshot
TimeoutStartSec={{ backup_service_start_timeout }}

View File

@ -0,0 +1,6 @@
[Unit]
After=multi-user.target
[Timer]
OnCalendar={{ backup_timer_frequency }}
Persistent=yes