#!/bin/sh # Nightly second copy of the ytplayer uploads library (homelab host, cron). # # USB drive (primary): /mnt/data/ytplayer/uploads # stable disk (backup): /uploads-backup # # Copies new files both ways and never deletes: the server removes a deleted # upload from both places itself, and anything uploaded while the drive was # offline (written to the backup) is copied back onto the drive here. set -eu VOL=${VOL:-/var/lib/docker/volumes/ytplayer-main-1dihzn_ytplayer-data/_data} USB=/mnt/data/ytplayer MIN_FREE_KB=${MIN_FREE_KB:-20971520} # keep 20 GiB free on the stable disk log() { logger -t ytplayer-uploads-backup "$*"; echo "$*"; } [ -f "$USB/.hl-data" ] || { log "USB drive offline (no marker) — skipped"; exit 0; } mkdir -p "$USB/uploads" "$VOL/uploads-backup" need=$(du -sk "$USB/uploads" | cut -f1) have=$(du -sk "$VOL/uploads-backup" | cut -f1) free=$(df -Pk "$VOL" | awk 'NR==2{print $4}') if [ $((free - (need - have))) -lt "$MIN_FREE_KB" ]; then log "not enough room on the stable disk for the backup (free ${free} KB, need ~$((need - have)) KB more) — skipped" exit 1 fi rsync -a --update "$USB/uploads/" "$VOL/uploads-backup/" # newer wins (e.g. replaced cover art) rsync -a --update "$VOL/uploads-backup/" "$USB/uploads/" log "ok — $(ls "$USB/uploads" | wc -l) files, $(du -sh "$USB/uploads" | cut -f1)"