mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-08-31 23:58:57 +02:00
Implemented deletion of not fully pulled backups
This commit is contained in:
34
roles/server_native-backups-consumer/README.md
Normal file
34
roles/server_native-backups-consumer/README.md
Normal file
@@ -0,0 +1,34 @@
|
||||
# role server_native-backups-consumer
|
||||
|
||||
## goal
|
||||
This script allows to pull backups from a remote server.
|
||||
|
||||
## scheme
|
||||
It is part of the following scheme:
|
||||
 <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 one of the following commands:
|
||||
|
||||
#### systemctl
|
||||
```bash
|
||||
watch -n2 "systemctl status pull-remote-backups.service"
|
||||
```
|
||||
|
||||
#### journalctl
|
||||
```bash
|
||||
journalctl -fu 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
|
||||
- https://serverfault.com/questions/304125/rsync-seems-incompatible-with-bashrc-causes-is-your-shell-clean
|
@@ -0,0 +1,63 @@
|
||||
#!/bin/bash
|
||||
# @param $1 hostname from which backup should be pulled
|
||||
|
||||
echo "pulling backups from: $1" &&
|
||||
|
||||
# error counter
|
||||
errors=0 &&
|
||||
|
||||
echo "loading meta data..." &&
|
||||
|
||||
remote_host="backup@$1" &&
|
||||
echo "host address: $remote_host" &&
|
||||
|
||||
remote_machine_id="$( (ssh "$remote_host" sha256sum /etc/machine-id) | head -c 64 )" &&
|
||||
echo "remote machine id: $remote_machine_id" &&
|
||||
|
||||
general_backup_machine_dir="/Backups/$remote_machine_id/" &&
|
||||
echo "backup dir: $general_backup_machine_dir" &&
|
||||
|
||||
remote_backup_types="$(ssh "$remote_host" "find $general_backup_machine_dir -maxdepth 1 -type d -execdir basename {} ;")" &&
|
||||
echo "backup types: $remote_backup_types" || exit 1
|
||||
|
||||
for backup_type in $remote_backup_types; do
|
||||
if [ "$backup_type" != "$remote_machine_id" ]; then
|
||||
echo "backup type: $backup_type" &&
|
||||
|
||||
general_backup_type_dir="$general_backup_machine_dir""$backup_type/" &&
|
||||
general_versions_dir="$general_backup_type_dir" &&
|
||||
local_previous_version_dir="$(ls -d $general_versions_dir* | tail -1)" &&
|
||||
echo "last local backup: $local_previous_version_dir" &&
|
||||
|
||||
remote_backup_versions="$(ssh "$remote_host" ls -d "$general_backup_type_dir"\*)" &&
|
||||
echo "remote backup versions: $remote_backup_versions" &&
|
||||
|
||||
|
||||
remote_last_backup_dir=$(echo "$remote_backup_versions" | tail -1) &&
|
||||
echo "last remote backup: $remote_last_backup_dir" &&
|
||||
|
||||
remote_source_path="$remote_host:$remote_last_backup_dir/" &&
|
||||
echo "source path: $remote_source_path" &&
|
||||
|
||||
local_backup_destination_path=$remote_last_backup_dir &&
|
||||
echo "backup destination: $local_backup_destination_path" &&
|
||||
|
||||
echo "creating local backup destination folder..." &&
|
||||
mkdir -vp "$local_backup_destination_path" &&
|
||||
|
||||
status_pulling_file="$local_backup_destination_path/.pulling" &&
|
||||
echo "creating: $status_pulling_file" &&
|
||||
echo "pulling backup since $(date)" > $status_pulling_file &&
|
||||
|
||||
echo "starting backup..." &&
|
||||
rsync_command='rsync -abP --delete --delete-excluded --rsync-path="sudo rsync" --link-dest="'$local_previous_version_dir'" "'$remote_source_path'" "'$local_backup_destination_path'"' &&
|
||||
echo "executing: $rsync_command" &&
|
||||
eval "$rsync_command" &&
|
||||
|
||||
echo "removing: $status_pulling_file" &&
|
||||
rm -vf $status_pulling_file
|
||||
|
||||
|| ((errors+=1));
|
||||
fi
|
||||
done
|
||||
exit $errors;
|
12
roles/server_native-backups-consumer/handlers/main.yml
Normal file
12
roles/server_native-backups-consumer/handlers/main.yml
Normal file
@@ -0,0 +1,12 @@
|
||||
- name: "reload pull-remote-backups service"
|
||||
systemd:
|
||||
name: pull-remote-backups.service
|
||||
state: reloaded
|
||||
enabled: yes
|
||||
daemon_reload: yes
|
||||
- name: "restart pull-remote-backups timer"
|
||||
systemd:
|
||||
name: pull-remote-backups.timer
|
||||
state: restarted
|
||||
enabled: yes
|
||||
daemon_reload: yes
|
4
roles/server_native-backups-consumer/meta/main.yml
Normal file
4
roles/server_native-backups-consumer/meta/main.yml
Normal file
@@ -0,0 +1,4 @@
|
||||
dependencies:
|
||||
- server_native-git
|
||||
- server_native-systemd-email
|
||||
- server_native-backups-cleanup
|
26
roles/server_native-backups-consumer/tasks/main.yml
Normal file
26
roles/server_native-backups-consumer/tasks/main.yml
Normal file
@@ -0,0 +1,26 @@
|
||||
- name: "create {{docker_pull_primary_backups_folder}}"
|
||||
file:
|
||||
path: "{{docker_pull_primary_backups_folder}}"
|
||||
state: directory
|
||||
mode: 0755
|
||||
|
||||
- name: create pull-remote-backup.sh
|
||||
copy:
|
||||
src: pull-remote-backup.sh
|
||||
dest: "{{docker_pull_primary_backups_folder}}pull-remote-backup.sh"
|
||||
mode: 0755
|
||||
|
||||
- name: create pull-remote-backups.service
|
||||
template: src=pull-remote-backups.service.j2 dest=/etc/systemd/system/pull-remote-backups.service
|
||||
notify: reload pull-remote-backups service
|
||||
|
||||
- name: create pull-remote-backups.timer
|
||||
template: src=pull-remote-backups.timer.j2 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.j2
|
||||
dest: "{{docker_pull_primary_backups_folder}}pull-remote-backups.sh"
|
||||
mode: 0755
|
||||
|
@@ -0,0 +1,7 @@
|
||||
[Unit]
|
||||
Description=pull remote backups
|
||||
OnFailure=systemd-email@%n.service
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStart=/usr/bin/bash {{docker_pull_primary_backups_folder}}pull-remote-backups.sh
|
@@ -0,0 +1,8 @@
|
||||
#!/bin/bash
|
||||
# Pulls the remote backups from multiple hosts
|
||||
hosts="{{pull_remote_backups}}";
|
||||
errors=0
|
||||
for host in $hosts; do
|
||||
bash {{docker_pull_primary_backups_folder}}pull-remote-backup.sh $host || ((errors+=1));
|
||||
done;
|
||||
exit $errors;
|
@@ -0,0 +1,10 @@
|
||||
[Unit]
|
||||
Description=starts pull remote backup timer
|
||||
|
||||
[Timer]
|
||||
OnCalendar={{on_calendar_pull_primary_backups}}
|
||||
RandomizedDelaySec={{randomized_delay_sec}}
|
||||
Persistent=false
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
1
roles/server_native-backups-consumer/vars/main.yml
Normal file
1
roles/server_native-backups-consumer/vars/main.yml
Normal file
@@ -0,0 +1 @@
|
||||
docker_pull_primary_backups_folder: "/home/administrator/scripts/pull-primary-backups/"
|
Reference in New Issue
Block a user