From 1aa0ca5ff293d007be0206ca6e23cb5b7e50b811 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Soko=C5=82owski?= Date: Fri, 24 Jan 2020 16:34:25 +0100 Subject: [PATCH] switch to using systemd service and timer instead of cron MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jakub Sokołowski --- README.md | 24 ++++++++++++++++++++++++ defaults/main.yml | 24 ++++++++++++++---------- tasks/backups.yml | 27 --------------------------- tasks/main.yml | 3 ++- tasks/script.yml | 16 ++++++++++++++++ tasks/timer.yml | 27 +++++++++++++++++++++++++++ templates/backup.service.j2 | 14 ++++++++++++++ templates/backup.timer.j2 | 6 ++++++ 8 files changed, 103 insertions(+), 38 deletions(-) delete mode 100644 tasks/backups.yml create mode 100644 tasks/script.yml create mode 100644 tasks/timer.yml create mode 100644 templates/backup.service.j2 create mode 100644 templates/backup.timer.j2 diff --git a/README.md b/README.md index b98e167..99ac72c 100644 --- a/README.md +++ b/README.md @@ -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 +``` diff --git a/defaults/main.yml b/defaults/main.yml index 3013cc2..48ed2bf 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -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: ~ diff --git a/tasks/backups.yml b/tasks/backups.yml deleted file mode 100644 index 05f8ed0..0000000 --- a/tasks/backups.yml +++ /dev/null @@ -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 diff --git a/tasks/main.yml b/tasks/main.yml index 551be4e..5e3f7ea 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,3 +1,4 @@ --- - include_tasks: s3cmd.yml -- include_tasks: backups.yml +- include_tasks: script.yml +- include_tasks: timer.yml diff --git a/tasks/script.yml b/tasks/script.yml new file mode 100644 index 0000000..133fa63 --- /dev/null +++ b/tasks/script.yml @@ -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 diff --git a/tasks/timer.yml b/tasks/timer.yml new file mode 100644 index 0000000..079aa4e --- /dev/null +++ b/tasks/timer.yml @@ -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 diff --git a/templates/backup.service.j2 b/templates/backup.service.j2 new file mode 100644 index 0000000..5aae6c2 --- /dev/null +++ b/templates/backup.service.j2 @@ -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 }} diff --git a/templates/backup.timer.j2 b/templates/backup.timer.j2 new file mode 100644 index 0000000..c6cf6ac --- /dev/null +++ b/templates/backup.timer.j2 @@ -0,0 +1,6 @@ +[Unit] +After=multi-user.target + +[Timer] +OnCalendar={{ backup_timer_frequency }} +Persistent=yes