Skip to content

Instantly share code, notes, and snippets.

@amityweb
Last active December 2, 2021 09:41
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save amityweb/4251879 to your computer and use it in GitHub Desktop.
Save amityweb/4251879 to your computer and use it in GitHub Desktop.
Incremental RSync Backup with 14 Day Retention for Databases and Home Folder
#!/bin/sh
rsync="/usr/bin/rsync"
################################
# VARIABLES
################################
# General Variables
remote_server="yourserver.com"
remote_port="22"
source="/home/"
destination_folder="/path/to/local/backup"
destination=$destination_folder/$remote_server
filter_list="filter_list.txt"
################################
# Execute dbdump.sh script on server
################################
echo start of database dumps $1
ssh -p $remote_port root@$remote_server 'bash -s' < "$destination_folder"/dbdump.sh
echo end of database dumps $1
################################
# BACKUP ALL FILES IN /HOME/
################################
if [ ! -d "$destination/Progress" ];
then
mkdir "$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
# with bandwidth limit
#rsync -ax --bwlimit=500 --include-from="$destination_folder/$filter_list" -e "ssh -p $remote_port" --delete --link-dest="$destination/Latest" "root@$remote_server:$source" "$destination/Progress"
# with stats and verbos
#rsync -ax -v --stats --progress --include-from="$destination_folder/$filter_list" -e "ssh -p $remote_port" --delete --link-dest="$destination/Latest" "root@$remote_server:$source" "$destination/Progress"
# no stats, no limit
rsync -ax --include-from="$destination_folder/$filter_list" -e "ssh -p $remote_port" --delete --link-dest="$destination/Latest" "root@$remote_server:$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"
# Create New Latest Link to Current Date Folder
ln -sfn "$date" "$destination/Latest"
# Delete Folders Leaving Last 7
ls | find "$destination/." ! -name '.' -type d -maxdepth 1 | sort -rn | awk 'NR>7' |
while read file;
do 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