diff --git a/playbooks/backup.yaml b/playbooks/backup.yaml new file mode 100644 index 0000000..9b92253 --- /dev/null +++ b/playbooks/backup.yaml @@ -0,0 +1,80 @@ +- name: Setup backup + hosts: berliet.medicenlutte.fr + tasks: + + - name: Setup /opt/backup folder + ansible.builtin.file: + path: /opt/backup + state: directory + owner: root + group: root + mode: u=rwx,g=rx,o=rx + become: true + + - name: Setup make-backup.sh script + ansible.builtin.copy: + dest: /opt/backup/make-backup.sh + src: backup/make-backup.sh + owner: root + group: root + mode: u=rwx,g=rx,o=rx + become: true + + - name: Setup advertize-pending-backup.sh script + ansible.builtin.copy: + dest: /opt/backup/advertize-pending-backup.sh + src: backup/advertize-pending-backup.sh + owner: root + group: root + mode: u=rwx,g=rx,o=rx + become: true + + - name: Setup make-backup service + ansible.builtin.copy: + dest: /etc/systemd/system/make-backup.service + src: backup/make-backup.service + owner: root + group: root + mode: u=rw,g=r,o=r + become: true + + - name: Setup make-backup service timer + ansible.builtin.copy: + dest: /etc/systemd/system/make-backup.timer + src: backup/make-backup.timer + owner: root + group: root + mode: u=rw,g=r,o=r + become: true + + - name: Setup advertize-pending-backup service + ansible.builtin.copy: + dest: /etc/systemd/system/advertize-pending-backup.service + src: backup/advertize-pending-backup.service + owner: root + group: root + mode: u=rw,g=r,o=r + become: true + + - name: Setup advertize-pending-backup service timer + ansible.builtin.copy: + dest: /etc/systemd/system/advertize-pending-backup.timer + src: backup/advertize-pending-backup.timer + owner: root + group: root + mode: u=rw,g=r,o=r + become: true + + - name: Enable make-backup service timer + ansible.builtin.systemd_service: + name: make-backup.timer + state: started + enabled: true + become: true + + - name: Enable advertize-pending-backup service timer + ansible.builtin.systemd_service: + name: advertize-pending-backup.timer + state: started + enabled: true + become: true \ No newline at end of file diff --git a/playbooks/backup/advertize-pending-backup.service b/playbooks/backup/advertize-pending-backup.service new file mode 100644 index 0000000..9dbeee8 --- /dev/null +++ b/playbooks/backup/advertize-pending-backup.service @@ -0,0 +1,6 @@ +[Unit] +Description=Advertize pending packup + +[Service] +Type=oneshot +ExecStart=/bin/bash /opt/backup/advertize-pending-backup.sh \ No newline at end of file diff --git a/playbooks/backup/advertize-pending-backup.sh b/playbooks/backup/advertize-pending-backup.sh new file mode 100644 index 0000000..bfa6691 --- /dev/null +++ b/playbooks/backup/advertize-pending-backup.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +BACKUP_DIR="/opt/backup/pending" +UNIFIEDPUSH_TOPIC="https://push.epickiwi.fr/hP4ByeeTDeLmCCPN-sauvegardes-mel" + +PENDING_BACKUP_COUNT=$( find "$BACKUP_DIR" -type f | grep -e '\.tar\(.gz\)*$' | wc -l ) + +if [[ "$PENDING_BACKUP_COUNT" -gt 0 ]]; then + curl -d "$(hostname --long): $PENDING_BACKUP_COUNT pending backups" "$UNIFIEDPUSH_TOPIC" + echo "Advertized $PENDING_BACKUP_COUNT panding backup" +else + echo "There is no pending backup" +fi \ No newline at end of file diff --git a/playbooks/backup/advertize-pending-backup.timer b/playbooks/backup/advertize-pending-backup.timer new file mode 100644 index 0000000..450e339 --- /dev/null +++ b/playbooks/backup/advertize-pending-backup.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Remind of pending timer every day + +[Timer] +OnCalendar=*-*-* 09:00:00 +Persistent=true + +[Install] +WantedBy=timers.target \ No newline at end of file diff --git a/playbooks/backup/make-backup.service b/playbooks/backup/make-backup.service new file mode 100644 index 0000000..c5131e0 --- /dev/null +++ b/playbooks/backup/make-backup.service @@ -0,0 +1,7 @@ +[Unit] +Description=Produce backup archive + +[Service] +Type=oneshot +ExecStart=/bin/bash /opt/backup/make-backup.sh +ExecStart=systemctl start advertize-pending-backup.service \ No newline at end of file diff --git a/playbooks/backup/make-backup.sh b/playbooks/backup/make-backup.sh new file mode 100755 index 0000000..56f3cbd --- /dev/null +++ b/playbooks/backup/make-backup.sh @@ -0,0 +1,49 @@ +#!/bin/bash + +## Directory where backup tar.gz files will be created into at the end of backup packaging process +PENDING_BACKUP_DIR="/opt/backup/pending" +## Directory where intermediate artifacts will be placed for backup +CURRENT_BACKUP_DIR="/opt/backup/current" +## File to include into backups +BACKUP_FILES=( + # "/var/lib/conduwuit" managed manually, please do not add in this list + "/srv/www/medicenlutte.fr" + "/opt/backup/current" +) +## Backup date in path-colplient format +DATE=$(date '+%FT%H-%M-%S') +## Final backup file name +BACKUP_FILENAME="backup-$(hostname --long)-$DATE.tar" + +# logging tool +LOG="tee -a $CURRENT_BACKUP_DIR/backup.log" + +# Removing previous backup if there is some +rm -f -r "$CURRENT_BACKUP_DIR/*" +mkdir -p "$CURRENT_BACKUP_DIR" +mkdir -p "$PENDING_BACKUP_DIR" + +# Lezgo + +echo "Starting backup $BACKUP_FILENAME" | $LOG + +# Backup of continuwuity database +echo "Backing up continuwuity database" +systemctl stop conduwuit 2>&1 | $LOG +tar -r -v -f "$PENDING_BACKUP_DIR/$BACKUP_FILENAME" /var/lib/conduwuit 2>&1 | $LOG +tar -r -v -f "$PENDING_BACKUP_DIR/$BACKUP_FILENAME" /var/lib/private/conduwuit 2>&1 | $LOG +systemctl start conduwuit 2>&1 | $LOG + +# Backup of files +for i in $(seq -w "${#BACKUP_FILES[@]}"); do + FILENAME="files-$i.tar" + SRC="${BACKUP_FILES[$(expr $i - 1)]}" + echo "Backing up $FILENAME: $SRC" | $LOG + tar -r -v -f "$PENDING_BACKUP_DIR/$BACKUP_FILENAME" $SRC 2>&1 | $LOG +done + +tar -r -v -f "$PENDING_BACKUP_DIR/$BACKUP_FILENAME" "$CURRENT_BACKUP_DIR/backup.log" + +gzip "$PENDING_BACKUP_DIR/$BACKUP_FILENAME" | $LOG + +rm -rf "$CURRENT_BACKUP_DIR/*" \ No newline at end of file diff --git a/playbooks/backup/make-backup.timer b/playbooks/backup/make-backup.timer new file mode 100644 index 0000000..6a8472e --- /dev/null +++ b/playbooks/backup/make-backup.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Produce backup archive every month + +[Timer] +OnCalendar=Wed *-*-1..7 12:00:00 +Persistent=true + +[Install] +WantedBy=timers.target \ No newline at end of file