From 5b49f1ab5f10df2b2eea07c8e647e9a34243e5cc Mon Sep 17 00:00:00 2001 From: Nathan Steel Date: Sun, 31 Aug 2025 14:46:06 +0100 Subject: [PATCH] Add USB backup for fedora install --- backup/fedora/backup.sh | 59 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 backup/fedora/backup.sh diff --git a/backup/fedora/backup.sh b/backup/fedora/backup.sh new file mode 100644 index 0000000..81e3a67 --- /dev/null +++ b/backup/fedora/backup.sh @@ -0,0 +1,59 @@ +#!/bin/sh +# 0 * * * * /home/nathan/Documents/backup.sh + +# Check if the USB is plugged in (Gnome auto-mounts to /run/media) +USB="/run/media/$USER/sd_backup"; +if [[ ! -d $USB ]]; then + exit 1 # sd_backup is not plugged in; +fi + +USB_DIR="/fedora/borg"; + +DIRS="git nas 360ss"; +BASE_DIR="/home/nathan/Documents/"; + +# Basically the code from directory_backup in scripts.git +borg_backup (){ + if [ ! -d "$BACKUP_DIR" ]; then + mkdir -p $BACKUP_DIR + borg init --encryption=none $BACKUP_DIR + fi + + export BORG_REPO=$BACKUP_DIR + borg create ::{hostname}-{now} $DIR +} +borg_prune () { + # Keep last 12 hours, 7 days, 4 weeks worth of backups + # LESS for local to save storage + borg prune \ + --glob-archives '{hostname}-*' \ + --keep-hourly 12 \ + --keep-daily 7 \ + --keep-weekly 4 + # Then clear up the disk space of the files removed via prune + borg compact +} +# Also do an rsync, so there's an accessible clone of data on USB +# makes it easier to drop things between PCs, but it is a CLONE +rsync_backup (){ + if [ ! -d "$CLONE_DIR" ]; then + mkdir -p $CLONE_DIR; + fi + # avh with --dry-run when testing + # Contents of directory for rsync (extra /) + rsync -avh $DIR/ $CLONE_DIR \ + --delete-before +} + +for bDir in $DIRS +do + BACKUP_DIR=$USB$USB_DIR/$bDir; + CLONE_DIR=$USB/fedora/rsync/$bDir; + DIR=$BASE_DIR$bDir; + + borg_backup + borg_prune + + rsync_backup +done +