renamed role

This commit is contained in:
2021-01-11 15:03:20 +01:00
parent 278d61759a
commit a213db0de3
9 changed files with 2 additions and 2 deletions

View File

@@ -0,0 +1,27 @@
# role native-pull-primary-backups
## goal
This script allows to pull backups from a remote server.
## scheme
It is part of the following scheme:
![backup scheme](https://www.veen.world/wp-content/uploads/2020/12/server-backup-768x567.jpg) <br />
Further information you will find [in this blog post](https://www.veen.world/2020/12/26/how-i-backup-dedicated-root-servers/).
## debug
### live
To track what the service is doing execute the following command:
```bash
watch -n2 "systemctl status pull-remote-backups.service"
```
### history
```bash
sudo journalctl -u pull-remote-backups
```
## see
- https://superuser.com/questions/363444/how-do-i-get-the-output-and-exit-value-of-a-subshell-when-using-bash-e
- https://gist.github.com/otkrsk/b0ffd4018e8a79b9010c461af298471e

View File

@@ -0,0 +1,36 @@
#!/bin/bash
# @param $1 hostname from which backup should be pulled
# error counter
errors=0
source_host="backup@$1"
source_machine_id="$( (ssh "$source_host" sha256sum /etc/machine-id) | head -c 64)" || exit 1
source_path="/Backups/$source_machine_id/"
directories="$(ssh "$source_host" find "$source_path" -maxdepth 1 -type d)" || exit 1
for folder in $directories; do
if [ "$folder" != "$source_path" ]; then
# this folder is neccessary to make restricted rsync possible:
diff_current="$folder/diffs/current/"
# this is the folder where the diffs will be copied to:
diff_store="$folder/diffs/$(date '+%Y%m%d%H%M%S')/"
# this is the folder which contains the actual backup:
latest_path="$folder/latest/"
# source path of the backup files:
remote_source_path="$source_host:$latest_path"
# file in which the logs will be saved:
log_path="$folder/log.txt"
# create working folders:
mkdir -vp "$latest_path"
mkdir -vp "$diff_store"
rm -vr "$diff_current"
mkdir -vp "$diff_current"
# do backup:
rsync -abP --delete --delete-excluded --rsync-path="sudo rsync" --log-file="$log_path" --backup-dir="$diff_current" "$remote_source_path" "$latest_path" || ((errors+=1));
mv -v "$diff_current"* "$diff_store"
fi
done
exit $errors;

View File

@@ -0,0 +1,12 @@
- name: "restart pull-remote-backups service"
systemd:
name: pull-remote-backups.service
state: restarted
enabled: yes
daemon_reload: yes
- name: "restart pull-remote-backups timer"
systemd:
name: pull-remote-backups.timer
state: restarted
enabled: yes
daemon_reload: yes

View File

@@ -0,0 +1,3 @@
dependencies:
- native-git
- native-systemd-email

View File

@@ -0,0 +1,15 @@
- name: create pull-remote-backup.sh
copy:
src: pull-remote-backup.sh
dest: "/usr/local/bin/pull-remote-backup.sh"
- name: create pull-remote-backups.service
template: src=pull-remote-backups.service dest=/etc/systemd/system/pull-remote-backups.service
notify: restart pull-remote-backups service
- name: create pull-remote-backups.timer
template: src=pull-remote-backups.timer dest=/etc/systemd/system/pull-remote-backups.timer
notify: restart pull-remote-backups timer
- name: create pull-remote-backups.sh
template: src=pull-remote-backups.sh dest=/usr/local/bin/pull-remote-backups.sh

View File

@@ -0,0 +1,7 @@
[Unit]
Description=pull remote backups
OnFailure=systemd-email@%n.service
[Service]
Type=oneshot
ExecStart=/usr/bin/bash /usr/local/bin/pull-remote-backups.sh

View File

@@ -0,0 +1,8 @@
#!/bin/bash
# Pulls the remote backups from multiple hosts
hosts="{{pull_remote_backups_hosts}}";
errors=0
for host in $hosts; do
bash /usr/local/bin/pull-remote-backup.sh $host || ((errors+=1));
done;
exit $errors;

View File

@@ -0,0 +1,9 @@
[Unit]
Description=starts pull remote backup timer
[Timer]
OnCalendar=22:00
RandomizedDelaySec=1h
[Install]
WantedBy=timers.target