Why not with rsync?
Even if you don't have more machines to keep in sync with the thumbdrive, off the top of my head I think I remember (or maybe it's a fabricated memory) that had some extra functionality and better performance.
DISCLAIMER: This is one of the first scripts i ever made and has the potential to shoot yourself in the foot. Error handling and security certainly need improvement.
Other than that it takes one (host) or more (directories) arguments and tries to sync the directories in 'dirs' from the remote host (which you need root access for). If an older snapshot (latest) is present, it only copies changes and hard links the rest. It needs the exclude list, otherwise it you end up with tons of stuff you normally don't want to back up. If you want a fresh snapshot, delete or rename 'latest'.
No idea if it runs in shells other than zsh. It only says bash in the code section to get the pretty colors.
Bash:
#!/bin/zsh
#
#[[ -f "$COLOR_FILE" ]] && source "$COLOR_FILE"
typeset -U hosts dirs
setopt pushdsilent
EXCLUDE_FROM_FILE="${ZSH}/misc/rsync_exclude-from"
if [[ ! -z "${1}" ]]; then
hosts="${1}"
shift
else
hosts=(
wb
pi2
pi3
)
fi
dirs=(
boot
etc
home
root
var
opt
)
if [[ ! -z "${1}" ]]; then
while [[ ! -z "${1}" ]]; do
dirs+=( "${1}" )
shift
done
fi
echo $hosts
echo $dirs
check_wd() {
if [[ ! -d "$WORKDIR" ]]; then
echo "${WORKDIR} does not exit"
echo "trying to create directory"
mkdir ${WORKDIR} || return 1
fi
pushd ${WORKDIR} && echo "Diving into ${WORKDIR}" || return 1
}
sync_dir() {
BACKUP_USER=$1
BACKUP_HOST=$2
BACKUP_DIR=$3
echo
echo "syncing ${BACKUP_USER}@${BACKUP_HOST}:/${BACKUP_DIR}"
rsync -rlD -u -ptog --info=progress2 --exclude-from=${EXCLUDE_FROM_FILE} --link-dest="${LINK}" "${BACKUP_USER}@${BACKUP_HOST}:/${BACKUP_DIR}" "${BACKUP_ROOT}/${BACKUP_HOST}/${DATE}"
}
for BACKUP_HOST in $hosts; do
BACKUP_ROOT=/mnt/spin0/backup/hosts
BACKUP_SNAP=latest
WORKDIR="${BACKUP_ROOT}/${BACKUP_HOST}"
LINK="${WORKDIR}/latest"
DATE=$(date "+%Y-%m-%d__%T")
echo "Starting backup on host: ${BACKUP_HOST}"
check_wd
if [[ ${BACKUP_HOST} == "bw" ]]; then
dirs+=(
mnt/fs.local/w10.system/Users
)
fi
for BACKUP_DIR in $dirs; do
sync_dir "root" "${BACKUP_HOST}" "${BACKUP_DIR}" || RC=1
done
# RC=$?
if [[ -d "${DATE}" ]] && [[ $RC != 0 ]]; then
rm -f latest
ln -s "${DATE}" latest
else
echo "${message[ERROR]}: ${DATE} does not exist."
echo "something went wrong. not linking."
fi
echo
done
EXCLUDE_FROM_FILE="${ZSH}/misc/rsync_exclude-from"
Code:
/var/log
*/*cache*
*/games
*/Games
*/swapfile
*/.local/share/Steam*
You should end up with a structure similar to this
Code:
├─ mnt
║ │ ├─ fs.local ║
║ │ │ ├─ ba0 ║
║ │ │ ├─ spin0 ║
║ │ │ │ ├─ backup ║
║ │ │ │ │ └─ hosts ║
║ │ │ │ │ ├─ hexenpups ║
║ │ │ │ │ ├─ pi0 ║
║ │ │ │ │ │ └─ 2021-04-11__08:23:34 ║
║ │ │ │ │ ├─ pi2 ║
║ │ │ │ │ │ ├─ 2021-08-14__06:48:28 ║
║ │ │ │ │ │ ├─ 2021-08-20__01:44:26 ║
║ │ │ │ │ │ ├─ 2022-07-14__23:48:00 ║
║ │ │ │ │ │ ├─ 2022-07-15__05:17:08 ║
║ │ │ │ │ │ ├─ 2022-07-28__18:47:10
Last edited: