Skip to content

Instantly share code, notes, and snippets.

@gunslingerfry
Last active November 4, 2016 04:52
Show Gist options
  • Save gunslingerfry/4241a6d215bf6d0d744cb154d71d881b to your computer and use it in GitHub Desktop.
Save gunslingerfry/4241a6d215bf6d0d744cb154d71d881b to your computer and use it in GitHub Desktop.
backup with rsync
#!/bin/bash
trap cleanup INT TERM
cleanup() {
if [ -a "$AUTO" ]; then
rm "$AUTO"
fi
if [ -a "$MANUAL" ]; then
rm "$MANUAL"
fi
exit
}
# verify the config directory exists, that time.txt exists
# copy old time to time2
if [ ! -d "$HOME"/.config/backup ]; then
mkdir -p "$HOME"/.config/backup
fi
if [ -a "$HOME"/.config/backup/time ]; then
\cp "$HOME"/.config/backup/time "$HOME"/.config/backup/time2
fi
#overwrite old time.txt file with new time
TIME="$(date +%F-%I%p)"
echo "$TIME" > "$HOME"/.config/backup/time
# make or shift the old log file, this should not happen very often if ever
LOG_NAME="rsync-"$TIME".log"
if [ -a /tmp/"$LOG_NAME" ]; then
COUNTER=0
# find the latest file
while [ -a /tmp/"$LOG_NAME"."$COUNTER" ]; do
COUNTER=$(($COUNTER+1))
done
# shift everything down
# while [ $COUNTER -gt -1 ]; do
# if [ $COUNTER -eq 0 ]; then
# \cp "$HOME"/.config/backup/"$LOG_NAME" "$HOME"/.config/backup/"$LOG_NAME"."$COUNTER"
# else
# \cp "$HOME"/.config/backup/"$LOG_NAME"."$(($COUNTER-1))" "$HOME"/.config/backup/"$LOG_NAME"."$COUNTER"
# fi
# COUNTER=$(($COUNTER-1))
# done
LOG_NAME="$LOG_NAME"."$COUNTER"
fi
> /tmp/"$LOG_NAME"
# rsync command
EXCLUDE=""
if [ -a "$HOME"/.config/backup/exclude ]; then
EXCLUDE="--exclude-from="$HOME"/.config/backup/exclude"
fi
rsync -azhR --chmod=Du=rwx,Dgo=rx,Fu=rw,Fgo=r --delete --stats --log-file=/tmp/"$LOG_NAME" "$EXCLUDE" --link-dest=/volume1/backups/rsync-backup/"$(cat "$HOME"/.config/backup/time2)" -e 'ssh -i /home/matthew/.ssh/id_rsa.pub' --rsync-path=/usr/bin/rsync /home/./matthew/ fryfamily@192.168.2.203:/volume1/backups/rsync-backup/"$TIME"
#don’t forget to scp the log file and put it with the backup
scp /tmp/"$LOG_NAME" nas:/volume1/backups/rsync-backup/"$TIME"/"$LOG_NAME"
# get the list of manually and automatically installed packages and store that as well
AUTO="/tmp/"$TIME"-auto.apt"
MANUAL="/tmp/"$TIME"-manual.apt"
apt-mark showauto > "$AUTO"
apt-mark showmanual > "$MANUAL"
scp "$AUTO" "$MANUAL" nas:/volume1/backups/rsync-backup/"$TIME"
cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment