{ pkgs, ... }: { systemd = { timers.backup-timer = { wantedBy = [ "timers.target" ]; partOf = [ "backup-timer.service" ]; timerConfig.OnCalendar = "*-*-* 4:00:00"; }; services.backup-timer = { serviceConfig.Type = "oneshot"; script = '' BKG_DIR=/var/backup EXCLUDE_APPS=("s3/") EXCLUDE_VOLUMES=("wireguard_data") EXCLUDE_CLEANUP=("s3/") DAYS=7 # how many days should daily backups be stored DF=daily # folder name for daily backups WF=weekly # folder name for weekly backups MF=monthly # folder name for monthly backups MBD=22 # day of month for monthly backup WBD=3 # weekday for weekly backups PV=podman-volumes # folder name for podman volumes cd $BKG_DIR PODMAN_VOLUMES=$(podman volume ls --format '{{.Name}}') # Podman volume backup if [ ! -d $PV ]; then mkdir $PV fi for vol in $PODMAN_VOLUMES; do if [[ " $\{EXCLUDE_VOLUMES[@]} " =~ " $\{vol} " ]]; then continue fi cd $BKG_DIR/$PV podman volume export $vol --output $vol.tar ; gzip $vol.tar done BACKUP_APPS=$(fd . -td -d 1 $BKG_DIR) # App backup for bkg in $BACKUP_APPS; do if [[ " $\{EXCLUDE_APPS[@]} " =~ " $\{bkg} " ]]; then continue fi cd $bkg #monthly if [ ! -d $MF ]; then mkdir $MF fi DOM=$(date +%d) if [ $DOM -eq $MBD ]; then fd . -tf -d 1 -x cp {/} $MF/$(date +"%Y%m")-{/} fi #weekly if [ ! -d $WF ]; then mkdir $WF fi DOW=$(date +%u) if [ $DOW -eq $WBD ]; then fd . -tf -d 1 -x cp {/} $WF/$(date +"%U%Y")-{/} fi #dailies if [ ! -d $DF ]; then mkdir $DF fi fd . -tf -d 1 -x mv {/} $DF/$(date +"%Y%m%d")-{/} done # Delete old backups for bkg in $BACKUP_APPS; do if [[ " $\{EXCLUDE_CLEANUP[@]} " =~ " $\{bkg} " ]]; then continue fi fd . $bkg"daily" --changed-before "$DAYS days" -x rm fd . $bkg"weekly" --changed-before "4 weeks" -x rm fd . $bkg"monthly" --changed-before "8 weeks" -x rm done ''; }; }; }