feat: Adds install script, updates locations of scripts referenced by the service.

This commit is contained in:
2025-04-21 10:50:26 -04:00
parent 723cd1ec1c
commit c59fb1df18
3 changed files with 40 additions and 7 deletions

View File

@@ -3,7 +3,7 @@ Description=Docker backup service
[Service] [Service]
Type=simple Type=simple
ExecStart=/bin/bash -c '/home/michael/docker-backup.sh' ExecStart=/bin/bash -c '/usr/local/bin/docker-backup'
[Install] [Install]
WantedBy=default.target WantedBy=default.target

View File

@@ -7,7 +7,7 @@ backupdir="/backups"
services=() services=()
readarray -d '' services < <(find "$stackdir" -maxdepth 1 -mindepth 1 -type d) readarray -d '' services < <(find "$stackdir" -maxdepth 1 -mindepth 1 -type d)
# Add volumes here to backup. # Directories to backup.
volumes=( volumes=(
"/var/lib/docker/volumes" "/var/lib/docker/volumes"
"/opt" "/opt"
@@ -42,13 +42,8 @@ function copyVolumes() {
mkdir "$dir" mkdir "$dir"
for volume in "${volumes[@]}"; do for volume in "${volumes[@]}"; do
# copy only directories. # copy only directories.
#
find "$volume" -maxdepth 1 -mindepth 1 -type d -print0 | find "$volume" -maxdepth 1 -mindepth 1 -type d -print0 |
xargs -0 -I {} cp -r --parents "$volume" "{}" xargs -0 -I {} cp -r --parents "$volume" "{}"
# for dir in $(find "$volume" -maxdepth 1 -mindepth 1 -type d); do
# cp -r --parents "$volume" "$dir"
# done
done done
} }
@@ -60,6 +55,13 @@ function backup() {
rm -rf "$1" "/$today.tar.gz" rm -rf "$1" "/$today.tar.gz"
} }
function cleanBackups() {
echo "Removing old backups..."
# Remove backups older than 7 days.
find "$backupdir" -maxdepth 1 -mindepth 1 -name "*.tar.gz" -mtime +7 -print0 |
xargs -0 -I {} rm -rf "{}"
}
function main() { function main() {
# stop services. # stop services.
stopServices stopServices
@@ -79,6 +81,9 @@ function main() {
# Restart services. # Restart services.
startServices startServices
# Remove backups older than 7 days.
cleanBackups
} }
main "$@" main "$@"

28
install.sh Normal file
View File

@@ -0,0 +1,28 @@
#!/bin/bash
baseurl="https://git.housh.dev/homelab/backup/raw/branch/main"
systemdir="/etc/systemd/system"
bindir="/usr/local/bin"
# Remove backup service file if it exists and install the service file.
[ -f "$systemdir/backup.service" ] &&
systemctl stop backup.service &&
rm -f "$systemdir/backup.service"
wget "$baseurl/backup.service" -O "$systemdir/backup.service"
# Only download the timer file if it doesn't exist.
[ ! -f "$systemdir/backup.timer" ] &&
wget "$baseurl/backup.timer" -O "$systemdir/backup.timer"
# Remove the backup script file if it exists and install the backup script.
[ -f "$bindir/docker-backup" ] && rm -f "$bindir/docker-backup"
wget "$baseurl/docker-backup.sh" -O "$bindir/docker-backup"
# Remove the restore script file if it exists and install the restore script.
[ -f "$bindir/docker-restore" ] && rm -f "$bindir/docker-restore"
wget "$baseurl/docker-restore.sh" -O "$bindir/docker-restore"
# Start services
systemctl daemon-reload
systemctl start backup.timer
systemctl start backup.service