Skip to content

Commit

Permalink
Merge pull request #3 from nl2go/add-cronjob
Browse files Browse the repository at this point in the history
Add cronjob for daily, weekly and monthly backups
  • Loading branch information
build-failure authored Aug 20, 2020
2 parents 5ef87ee + 7682cae commit b4d88f5
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 15 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ None.
- hosts: all
roles:
- { role: nl2go.clickhouse_backup }
vars:
clickhouse_backup_version: 0.5.2
clickhouse_backup_general_remote_storage: gcs
clickhouse_backup_gcs_credentials_json: !vault |
$ANSIBLE_VAULT;1.2;AES256;production
1234xxx...
clickhouse_backup_gcs_bucket: clickhouse-backup-bucket
clickhouse_backup_gcs_path: "clickhouse"
clickhouse_backup_clickhouse_host: "{{ ansible_default_ipv4.address }}"
clickhouse_backup_clickhouse_password: 123SECURE

### Install cronjob for executing backups

The role contains tasks to automatically set up 3 cron jobs that execute the backup on a regular basis. The installation of these cron jobs can be controlled by setting the variable `clickhouse_backup_install_crontab` which defaults to `true`. The default configuration would set up daily, weekly and monthly backups. The number of backups to keep on the remote storage for each kind of backups can be controlled by the variables:

clickhouse_backup_general_backups_to_keep_remote_daily: 7
clickhouse_backup_general_backups_to_keep_remote_weekly: 4
clickhouse_backup_general_backups_to_keep_remote_monthly: 12

The numbers given here are the default values.

## Development

Expand Down
17 changes: 13 additions & 4 deletions defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
---
# clickhouse backup tool version to install
clickhouse_backup_version: 0.5.2
# clickhouse-backup config file location
clickhouse_backup_config_file: /etc/clickhouse-backup/config.yml
# clickhouse-backup config directory
clickhouse_backup_config_dir: /etc/clickhouse-backup

####################################
# General clickhouse-backup settings
####################################
clickhouse_backup_general_remote_storage: s3
clickhouse_backup_general_disable_progress_bar: false
clickhouse_backup_general_backups_to_keep_local: 0
clickhouse_backup_general_backups_to_keep_remote: 0
clickhouse_backup_general_backups_to_keep_local: 1
clickhouse_backup_general_backups_to_keep_remote_daily: 7
clickhouse_backup_general_backups_to_keep_remote_weekly: 4
clickhouse_backup_general_backups_to_keep_remote_monthly: 12

###############################################
# Clickhouse related clickhouse-backup settings
Expand Down Expand Up @@ -64,3 +66,10 @@ clickhouse_backup_cos_path: ""
clickhouse_backup_cos_compression_format: gzip
clickhouse_backup_cos_compression_level: 1
clickhouse_backup_cos_debug: false

clickhouse_backup_install_crontab: true
clickhouse_backup_cron_month: "*"
clickhouse_backup_cron_day: "*"
clickhouse_backup_cron_weekday: "*"
clickhouse_backup_cron_minute: 0
clickhouse_backup_cron_hour: 2
4 changes: 2 additions & 2 deletions molecule/default/verify/backup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
backup_name: "molecule_backup_{{ lookup('pipe','date -u +%Y-%m-%d') }}"

- name: Create new backup
shell: "clickhouse-backup create {{ backup_name }}"
shell: "clickhouse-backup -c /etc/clickhouse-backup/config-daily.yml create {{ backup_name }}"
register: backup_out

- name: Verify backup creation output
Expand All @@ -25,7 +25,7 @@
failed_when: not backup_shadow_result.stat.exists

- name: Check local backup list
shell: "clickhouse-backup list local"
shell: "clickhouse-backup -c /etc/clickhouse-backup/config-daily.yml list local"
register: local_backup_list

- name: Verify local backup list output
Expand Down
6 changes: 5 additions & 1 deletion molecule/default/verify/base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@

- name: Check that clickhouse-backup config is created
stat:
path: /etc/clickhouse-backup/config.yml
path: "/etc/clickhouse-backup/config-{{ item }}.yml"
register: stat_config_result
failed_when: not stat_config_result.stat.exists
loop:
- daily
- weekly
- monthly
20 changes: 20 additions & 0 deletions molecule/default/verify/cronjob.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
- name: Check that clickhouse-backup cronjob file is created
stat:
path: "/etc/cron.d/clickhouse-backup-{{ item }}"
register: stat_cronjob_result
failed_when: not stat_cronjob_result.stat.exists
loop:
- daily
- weekly
- monthly

- name: Run cronjob script
command: /usr/local/bin/cronjob-clickhouse-backup-daily.sh
register: cronjob_script_result
run_once: yes

- name: Verify cronjob script return code
assert:
that:
- cronjob_script_result.rc == 0
5 changes: 5 additions & 0 deletions molecule/default/verify/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@
tasks:
- include_tasks: restore.yml
- include_tasks: dataset.yml

- name: Verify clickhouse-backup cronjob
hosts: clickhouse
tasks:
- include_tasks: cronjob.yml
4 changes: 2 additions & 2 deletions molecule/default/verify/restore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
backup_name: "molecule_backup_{{ lookup('pipe','date -u +%Y-%m-%d') }}"

- name: Download backup
shell: "clickhouse-backup download {{ backup_name }}"
shell: "clickhouse-backup -c /etc/clickhouse-backup/config-daily.yml download {{ backup_name }}"

- name: Restore backup
shell: "clickhouse-backup restore {{ backup_name }}"
shell: "clickhouse-backup -c /etc/clickhouse-backup/config-daily.yml restore {{ backup_name }}"
register: backup_restore

- name: Verify backup restore return code
Expand Down
2 changes: 1 addition & 1 deletion molecule/default/verify/upload.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
backup_name: "molecule_backup_{{ lookup('pipe','date -u +%Y-%m-%d') }}"

- name: Upload backup
shell: "clickhouse-backup upload {{ backup_name }}"
shell: "clickhouse-backup -c /etc/clickhouse-backup/config-daily.yml upload {{ backup_name }}"
register: backup_upload

- name: Verify backup upload return code
Expand Down
6 changes: 5 additions & 1 deletion tasks/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
- name: Create clickhouse-backup configuration file
template:
src: config.yml.j2
dest: "{{ clickhouse_backup_config_file }}"
dest: "{{ clickhouse_backup_config_dir }}/config-{{ item }}.yml"
owner: root
group: root
mode: '0664'
loop:
- daily
- weekly
- monthly
34 changes: 34 additions & 0 deletions tasks/crontab.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
- name: Create clickhouse-backup cronjob bash script
template:
src: cronjob-clickhouse-backup.sh.j2
dest: "/usr/local/bin/cronjob-clickhouse-backup-{{ item }}.sh"
owner: root
group: root
mode: '0775'
loop:
- daily
- weekly
- monthly

- name: Set default state for clickhouse-backup cron file
set_fact:
clickhouse_backup_crontab_state: absent

- name: Overwrite state for clickhouse-backup cron file
set_fact:
clickhouse_backup_crontab_state: present
when: clickhouse_backup_install_crontab

- name: Creates/remove cron file under /etc/cron.d
cron:
name: Add cronjob for clickhouse backup
special_time: "{{ item }}"
user: root
job: "/usr/local/bin/cronjob-clickhouse-backup-{{ item }}.sh"
cron_file: "clickhouse-backup-{{ item }}"
state: "{{ clickhouse_backup_crontab_state }}"
loop:
- daily
- weekly
- monthly
2 changes: 2 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
tags: [clickhouse-backup, clickhouse-backup_install, install]
- import_tasks: config.yml
tags: [clickhouse-backup, clickhouse-backup_config, config]
- import_tasks: crontab.yml
tags: [clickhouse-backup, clickhouse-backup_crontab, crontab]
8 changes: 4 additions & 4 deletions templates/config.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ general:
remote_storage: {{ clickhouse_backup_general_remote_storage }}
disable_progress_bar: {{ clickhouse_backup_general_disable_progress_bar }}
backups_to_keep_local: {{ clickhouse_backup_general_backups_to_keep_local }}
backups_to_keep_remote: {{ clickhouse_backup_general_backups_to_keep_remote }}
backups_to_keep_remote: {{ lookup('vars', 'clickhouse_backup_general_backups_to_keep_remote_' + item) }}
clickhouse:
username: {{ clickhouse_backup_clickhouse_username }}
password: {{ clickhouse_backup_clickhouse_password }}
Expand All @@ -20,7 +20,7 @@ s3:
region: {{ clickhouse_backup_s3_region }}
acl: {{ clickhouse_backup_s3_acl }}
force_path_style: {{ clickhouse_backup_s3_force_path_style }}
path: {{ clickhouse_backup_s3_path }}
path: {{ clickhouse_backup_s3_path }}/{{ item }}
disable_ssl: {{ clickhouse_backup_s3_disable_ssl }}
part_size: {{ clickhouse_backup_s3_part_size }}
compression_level: {{ clickhouse_backup_s3_compression_level }}
Expand All @@ -31,15 +31,15 @@ gcs:
credentials_file: {{ clickhouse_backup_gcs_credentials_file }}
credentials_json: '{{ clickhouse_backup_gcs_credentials_json }}'
bucket: {{ clickhouse_backup_gcs_bucket }}
path: {{ clickhouse_backup_gcs_path }}
path: {{ clickhouse_backup_gcs_path }}/{{ item }}
compression_level: {{ clickhouse_backup_gcs_compression_level }}
compression_format: {{ clickhouse_backup_gcs_compression_format }}
cos:
url: {{ clickhouse_backup_cos_url }}
timeout: {{ clickhouse_backup_cos_timeout }}
secret_id: {{ clickhouse_backup_cos_secret_id }}
secret_key: {{ clickhouse_backup_cos_secret_key }}
path: {{ clickhouse_backup_cos_path }}
path: {{ clickhouse_backup_cos_path }}/{{ item }}
compression_format: {{ clickhouse_backup_cos_compression_format }}
compression_level: {{ clickhouse_backup_cos_compression_level }}
debug: {{ clickhouse_backup_cos_debug }}
12 changes: 12 additions & 0 deletions templates/cronjob-clickhouse-backup.sh.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

backup_name=`date -u +%Y-%m-%d`

echo "Create {{ item }} local backup..."
clickhouse-backup -c {{ clickhouse_backup_config_dir }}/config-{{ item }}.yml create $backup_name-{{ item }}

echo "Upload {{ item }} full backup..."
clickhouse-backup -c {{ clickhouse_backup_config_dir }}/config-{{ item }}.yml upload $backup_name-{{ item }}

echo "Delete {{ item }} local backup..."
clickhouse-backup -c {{ clickhouse_backup_config_dir }}/config-{{ item }}.yml delete local $backup_name-{{ item }}

0 comments on commit b4d88f5

Please sign in to comment.