This repository has been archived by the owner on Apr 30, 2020. It is now read-only.
forked from dparoli/hrsync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhrsync
executable file
·83 lines (66 loc) · 1.89 KB
/
hrsync
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/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