diff --git a/backup.service b/backup.service index 4184a74..bd0daca 100644 --- a/backup.service +++ b/backup.service @@ -3,7 +3,7 @@ Description=Docker backup service [Service] Type=simple -ExecStart=/bin/bash -c '/home/michael/docker-backup.sh' +ExecStart=/bin/bash -c '/usr/local/bin/docker-backup' [Install] WantedBy=default.target diff --git a/docker-backup.sh b/docker-backup.sh index c190453..669b7f2 100644 --- a/docker-backup.sh +++ b/docker-backup.sh @@ -7,7 +7,7 @@ backupdir="/backups" services=() readarray -d '' services < <(find "$stackdir" -maxdepth 1 -mindepth 1 -type d) -# Add volumes here to backup. +# Directories to backup. volumes=( "/var/lib/docker/volumes" "/opt" @@ -42,13 +42,8 @@ function copyVolumes() { mkdir "$dir" for volume in "${volumes[@]}"; do # copy only directories. - # find "$volume" -maxdepth 1 -mindepth 1 -type d -print0 | 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 } @@ -60,6 +55,13 @@ function backup() { 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() { # stop services. stopServices @@ -79,6 +81,9 @@ function main() { # Restart services. startServices + + # Remove backups older than 7 days. + cleanBackups } main "$@" diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..8aa8b27 --- /dev/null +++ b/install.sh @@ -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