Skip to content
Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 83 lines (66 sloc) 1.89 KB
#!/usr/bin/env bash
# hrsync by Daniele Paroli
#
# simple script to backup a directory with rsync detecting moved and renamed files
#
# use it at your own risk, please see README and LICENSE files
# ------------------------------------------------------------
# Backup source and target directory
Source="${1%/}"
Target="${2%/}"
TargetHost=$3
# ------------------------------------------------------------
# Name of shadow directory
Shadow=".rsync_shadow"
# Start doing things
if [ "${Source:0:1}" != / ]
then
echo "Source path needs to be an absolute path"
exit 0
fi
if [ "${Target:0:1}" != / ]
then
echo "Target path needs to be an absolute path"
exit 0
fi
if [ -z "$2" ]
then
echo "Usage:"
echo " hrsync source target"
echo " hrsync source target remoteHost"
echo "Example:"
echo " hrsync /home/user/Documents /mnt/external/Documents"
echo " hrsync /home/user/Documents /root/Documents root@example.com"
echo "Please see the README file"
exit 0
fi
if [ ! -d "$Source/$Shadow" ]
then
# first time run: create source shadow
rsync -a --delete --link-dest="$Source" --exclude="/$Shadow" "$Source"/ "$Source/$Shadow"
fi
# do real syncronization
if [ -z "$3" ]
then
# local version
rsync -axXhHv --stats --no-inc-recursive --numeric-ids --delete --delete-after "$Source"/ "$Target"
else
# remote version
rsync -axXhHv --stats --no-inc-recursive --numeric-ids --delete --delete-after "$Source"/ $TargetHost:"'$Target'"
fi
status=$?
if [ $status -eq 0 ]
then
#update source shadow directory
rsync -a --delete --link-dest="$Source" --exclude="/$Shadow" "$Source"/ "$Source/$Shadow"
#update target shadow directory
if [ -z "$3" ]
then
# local version
rsync -a --delete --link-dest="$Target" --exclude="/$Shadow" "$Target/" "$Target/$Shadow"
else
# remote version
ssh $TargetHost "rsync -a --delete --link-dest='$Target' --exclude='/$Shadow' '$Target/' '$Target/$Shadow'"
fi
fi
exit $status
You can’t perform that action at this time.