Skip to content

Instantly share code, notes, and snippets.

@binarymason
Last active July 13, 2021 13:59
Show Gist options
  • Save binarymason/ebf3054d4c4c1b855f4c7b5d37912efa to your computer and use it in GitHub Desktop.
Save binarymason/ebf3054d4c4c1b855f4c7b5d37912efa to your computer and use it in GitHub Desktop.
backup home directory
#!/bin/sh
rsync="/usr/bin/rsync"
################################
# VARIABLES
################################
# General Variables
backup_user="m"
source="/home/$backup_user"
destination_folder="/media/$backup_user/Storage/home-backups"
destination=$destination_folder
################################
# BACKUP ALL FILES IN /HOME/
# This should be configured in /etc/anacrontab:
#
# 1 15 cron.daily /bin/sh /home/m/backup.sh
################################
if [ ! -d "$destination/Progress" ];
then
mkdir -p "$destination/Progress"
fi
# try rsync for x times
I=0
MAX_RESTARTS=2
LAST_EXIT_CODE=1
while [ $I -le $MAX_RESTARTS ]
do
I=$(( $I + 1 ))
echo $I. start of rsync $1
rsync -ax -v --stats --progress --delete --link-dest="$destination/Latest" \
--exclude *.JPG \
--exclude *.PNG \
--exclude *.cache \
--exclude *.gif \
--exclude *.jpg \
--exclude *.log \
--exclude *.mov \
--exclude *.mp4 \
--exclude *.png \
--exclude *.pth \
--exclude *.tmp \
--exclude *.vscode\
--exclude *.zip\
--exclude Downloads \
--exclude Dropbox \
--exclude node_modules \
--exclude *.csv \
--exclude *.git \
--exclude *.pkl \
--exclude *.pth \
--exclude *.swp \
--exclude *.venv \
--exclude .config/Code \
--exclude .config/google-chrome \
--exclude .dropbox \
--exclude .fasd* \
--exclude .local/share \
--exclude .vim/undo \
--exclude .zoom \
--exclude /var/lib/postgresql \
--exclude Nextcloud \
--exclude Slack \
--exclude __pycache__ \
--exclude backup \
--exclude snap \
"$source" "$destination/Progress"
LAST_EXIT_CODE=$?
if [ $LAST_EXIT_CODE -eq 0 ]; then
break
fi
done
# check if successful
if [ $LAST_EXIT_CODE -ne 0 ]; then
echo rsync failed for $I times. giving up. $1
else
echo rsync successful after $I times. $1
# Move Progress to Current Date Folder
date=`date "+%Y-%m-%d-%H%M%S"`
mv "$destination/Progress" "$destination/$date"
chown -R "$destination_user:$destination_user" "$destination/$date"
# Create New Latest Link to Current Date Folder
ln -sfn "$date" "$destination/Latest"
# Delete Folders Leaving Last 7
find $destination/* -maxdepth 1 -prune -type d | sort -rn | awk 'NR>7' |
while read file;
do
echo "+ pruning $file"
rm -Rf "$file";
done;
echo DONE $1;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment