Add an untested barebones backup script wrapper
This commit is contained in:
parent
26a83ae2ab
commit
86ab23bd07
5
roles/backups/defaults/main.yml
Normal file
5
roles/backups/defaults/main.yml
Normal file
@ -0,0 +1,5 @@
|
||||
# vim:ft=ansible:
|
||||
backups_dir: "/opt/backups"
|
||||
backups_outdir: "/backups"
|
||||
backups_modulesdir: "/opt/backups/modules"
|
||||
backups_script: "/opt/backups/backup.sh"
|
26
roles/backups/tasks/main.yml
Normal file
26
roles/backups/tasks/main.yml
Normal file
@ -0,0 +1,26 @@
|
||||
#!/usr/bin/ansible-playbook
|
||||
# vim:ft=ansible:
|
||||
---
|
||||
- name: Set up general backups
|
||||
block:
|
||||
- name: Create backups directories
|
||||
file:
|
||||
state: directory
|
||||
mode: "0700"
|
||||
path: "{{ item }}"
|
||||
loop:
|
||||
- "{{ backups_dir }}"
|
||||
- "{{ backups_outdir }}"
|
||||
- "{{ backups_modulesdir }}"
|
||||
- name: Template out backup script
|
||||
template:
|
||||
src: "backup.sh"
|
||||
dest: "{{ backups_script }}"
|
||||
mode: "0700"
|
||||
- name: Set backup cronjob
|
||||
cron:
|
||||
minute: "0"
|
||||
hour: "2"
|
||||
name: ansible-backup
|
||||
job: "{{ backups_script }}"
|
||||
become: yes
|
43
roles/backups/templates/backup.sh
Normal file
43
roles/backups/templates/backup.sh
Normal file
@ -0,0 +1,43 @@
|
||||
#! /bin/bash
|
||||
#
|
||||
# backup.sh
|
||||
# General-purpose backup script that accepts subtasks
|
||||
# Copyright (C) 2020 Vintage Salt <rehashedsalt@cock.li>
|
||||
#
|
||||
# Distributed under terms of the MIT license.
|
||||
#
|
||||
|
||||
set -e
|
||||
|
||||
export BACKUPSDIR="{{ backups_outdir }}"
|
||||
export MODULESDIR="{{ backups_modulesdir }}"
|
||||
export DATE="$(date -Iseconds)"
|
||||
|
||||
# Helper functions
|
||||
log() {
|
||||
[ -z "$1" ] && return 1
|
||||
printf "$(date -Iseconds): $1\n"
|
||||
}
|
||||
|
||||
# Do the do
|
||||
if ! [ -d "$BACKUPSDIR" ]; then
|
||||
log "Unable to find backups directory: $BACKUPSDIR"
|
||||
exit 1
|
||||
fi
|
||||
if ! [ -d "$MODULESDIR" ]; then
|
||||
log "Unable to find modules directory: $MODULESDIR"
|
||||
exit 2
|
||||
fi
|
||||
log "Beginning backups"
|
||||
for file in "$MODULESDIR"/*; do
|
||||
# Just keep going if we don't have any tasks to do
|
||||
[ -f "$file" ] || continue
|
||||
# Execute the module and alert if it fails
|
||||
log "Executing module: $file"
|
||||
(
|
||||
source "$file"
|
||||
) || {
|
||||
log "Error executing module: $file"
|
||||
}
|
||||
done
|
||||
|
Loading…
Reference in New Issue
Block a user