add backup task

This commit is contained in:
vincent 2022-03-12 15:44:06 +01:00
parent bf470f0886
commit 7488c26f62
3 changed files with 59 additions and 0 deletions

View File

@ -25,3 +25,6 @@ consul_servers_list: "\
consul_join: [] consul_join: []
consul_join_wan: [] consul_join_wan: []
consul_bootstrap_expect: 3 consul_bootstrap_expect: 3
consul_snapshot: false
consul_backup_location: ""
consul_cron_hour: 1

39
files/cs-backup.sh Executable file
View File

@ -0,0 +1,39 @@
export PATH_SNAPSHOT=$1
export PATH_DIR="daily"
export PATH_BACKUP=$PATH_SNAPSHOT"/"$PATH_DIR
export TOKEN_AUTH_PROD=
export TOKEN_AUTH_STAGING=
export RETENTION=30
export ENV="0" # (0 = staging, 1 = production)
export SNAPSHOT_FILE=$(date +%Y-%m-%d)
select_env() {
if [ "$ENV" = "0" ]
then
export ENV_TOKEN="$TOKEN_AUTH_STAGING"
else
export ENV_TOKEN="$TOKEN_AUTH_PROD"
fi
}
create_snapshot_folder(){
mkdir -p $PATH_BACKUP
}
run_snapshot() {
select_env
/usr/bin/consul snapshot save -token=$ENV_TOKEN $PATH_BACKUP/$SNAPSHOT_FILE.snap
}
retention() {
find $PATH_BACKUP -name "*.snap" -mtime +${RETENTION} -print -exec rm {} \;
}
main() {
create_snapshot_folder
run_snapshot
retention
}
### START HERE ###
main $@

View File

@ -27,6 +27,23 @@
owner: consul owner: consul
mode: 0644 mode: 0644
notify: reload consul configuration notify: reload consul configuration
- name: configure backup
block:
- name: copy backup script
copy:
dest: "{{ consul_data_dir }}/cs-backup.sh"
mode: 0744
owner: consul
src: cs-backup.sh
- name: consul snaphot cron.d
cron:
name: consul backup
user: consul
state: present
job: "{{ consul_data_dir }}/cs-backup.sh {{ consul_backup_location }}"
hour: "{{consul_cron_hour}}"
when: consul_snapshot
- name: ensure service is started - name: ensure service is started
systemd: systemd:
name: "{{ consul_service_name }}" name: "{{ consul_service_name }}"