Optimized auto backup

This commit is contained in:
2023-05-28 16:35:45 +02:00
parent bc0e5f33b8
commit 31dc0191f1
8 changed files with 89 additions and 75 deletions

View File

@@ -1 +0,0 @@
ACTION=="add", SUBSYSTEM=="block", ENV{ID_SERIAL_SHORT}=="{{ backup_to_usb_serial_short }}", RUN+="/usr/bin/systemd-mount --no-block $devnode {{ mount_point }}", SYMLINK+="backup_usb"

View File

@@ -1,10 +1,13 @@
[Unit]
Description=Backup to USB when it's plugged in
After=local-fs.target
Description=Backup to USB when mounted to {{ backup_to_usb_mount }}
Wants={{systemctl_mount_service_name}}
After={{systemctl_mount_service_name}}
OnFailure=systemd-email@%n.service
Requires=backups-cleanup.service
After=backups-cleanup.service
[Service]
ExecStart={{ backup_to_usb_script_path }} {{ mount_point }}/{{ backup_to_usb_subdirectory }} {{ backup_to_usb_source_path }}
ExecStart=/bin/python {{ backup_to_usb_script_path }} {{backup_to_usb_source}} {{backup_to_usb_destination}}
[Install]
WantedBy=multi-user.target
WantedBy=multi-user.target

View File

@@ -1,35 +0,0 @@
#!/bin/sh
backup_to_usb_destination_path="{{ mount_point }}" &&
echo "backup to usb destination path: $backup_to_usb_destination_path" &&
source_path="{{ backup_to_usb_source_path }}" &&
echo "source path: $source_path" || exit 1
if [ ! -d "$backup_to_usb_destination_path" ]; then
echo "Directory $backup_to_usb_destination_path does not exist" &&
exit 1
fi
machine_id="$(sha256sum /etc/machine-id | head -c 64 )" &&
echo "machine id: $machine_id" &&
versions_path="$backup_to_usb_destination_path$machine_id/backup-to-usb/" &&
echo "versions path: $versions_path" || exit 1
if [ ! -d "$versions_path" ]; then
echo "Creating $versions_path..." &&
mkdir -vp $versions_path || exit 1
fi
previous_version_path="$(ls -d $versions_path* | tail -1)" &&
echo "previous versions path: $previous_version_path" &&
current_version_path="$versions_path$(date '+%Y%m%d%H%M%S')" &&
echo "current versions path: $current_version_path" &&
echo "creating backup destination folder..." &&
mkdir -vp "$current_version_path" &&
echo "Starting synchronization..."
rsync -abP --delete --delete-excluded --link-dest="$previous_version_path" "$source_path" "$current_version_path" &&
echo "Synchronization finished." || exit 1