-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsync.sh
executable file
·112 lines (98 loc) · 3.32 KB
/
rsync.sh
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/bash
# check if conf file is provided
if [ "$#" -lt 1 ]; then
echo "Error: Please provide at least a config file as an argument"
fi
CONF=$1
LOGFILE=$(awk '/^logfile/{print $3}' $CONF)-"$(date +%Y%m%d)".log
SOURCE=$(awk '/^source/{print $3}' $CONF)
TARGET=$(awk '/^target/{print $3}' $CONF)
KEYCHAIN=$(awk '/^keychain/{print $3}' $CONF)
RSYNCCONF=$(awk '/^rsyncconf/{print $3}' $CONF)
EXCLUDES=$(awk '/^excludes/{print $3}' $CONF)
RSYNC=`which rsync`
function log {
echo $1 >> $LOGFILE 2>&1
}
function run {
log "Sync time: "$(date +%Y%m%d-%H:%M:%S)
log "Running filesync"
log "Getting ssh key ..."
. $KEYCHAIN
# Log command
log 'Running '$RSYNC' -aPEh -e ssh '$RSYNCCONF' '$EXCLUDES' '$SOURCE' '$TARGET' >> '$LOGFILE' 2>&1'
# Run
$RSYNC -aPEh -e ssh $RSYNCCONF $EXCLUDES "$SOURCE" $TARGET >> $LOGFILE 2>&1
log "####################"
log ""
}
# check if more than the allowed arguments are provided
if [ "$#" -gt 3 ]; then
log "Error: Allowed arguments are: <conf> [<external1-path> <external1-uuid] [<external2>]"
fi
if [ "$#" -eq 1 ]; then
run
else
while [ ! $# -eq 0 ]
do
case "$1" in
--external1-mount)
EXTERNAL1_MOUNT=true
EXTERNAL1_PATH=$(awk '/^external1path/{print $3}' $CONF)
EXTERNAL1_UUID=$(awk '/^external1uuid/{print $3}' $CONF)
;;
--external2-mount)
EXTERNAL2_MOUNT=true
EXTERNAL2_PATH=$(awk '/^external2path/{print $3}' $CONF)
EXTERNAL2_UUID=$(awk '/^external2uuid/{print $3}' $CONF)
;;
esac
shift
done
# runs the sync if external hard drive/drives is/are mounted
# else mounts before running the sync, unmounts after and triggers harddrive spindown
if [ "$EXTERNAL1_MOUNT" = true ] && [ "$EXTERNAL2_MOUNT" = true ] ; then
if (mount | grep $EXTERNAL1_PATH > /dev/null) && (mount | grep $EXTERNAL2_PATH > /dev/null); then
run
elif (mount | grep $EXTERNAL1_PATH > /dev/null); then
mount $EXTERNAL2_PATH
run
umount $EXTERNAL2_PATH
hdparm -y /dev/disk/by-uuid/$EXTERNAL2_UUID
elif (mount | grep $EXTERNAL2_PATH > /dev/null); then
mount $EXTERNAL1_PATH
run
umount $EXTERNAL1_PATH
hdparm -y /dev/disk/by-uuid/$EXTERNAL1_UUID
else
mount $EXTERNAL1_PATH
mount $EXTERNAL2_PATH
run
umount $EXTERNAL1_PATH
umount $EXTERNAL2_PATH
hdparm -y /dev/disk/by-uuid/$EXTERNAL1_UUID
hdparm -y /dev/disk/by-uuid/$EXTERNAL2_UUID
fi
elif [ "$EXTERNAL1_MOUNT" = true ]; then
if (mount | grep $EXTERNAL1_PATH > /dev/null); then
run
else
mount $EXTERNAL1_PATH
run
umount $EXTERNAL1_PATH
hdparm -y /dev/disk/by-uuid/$EXTERNAL1_UUID
fi
elif [ "$EXTERNAL2_MOUNT" = true ]; then
if (mount | grep $EXTERNAL2_PATH > /dev/null); then
run
else
mount $EXTERNAL2_PATH
run
umount $EXTERNAL2_PATH
hdparm -y /dev/disk/by-uuid/$EXTERNAL2_UUID
fi
else
log "Something went wrong regarding external drive mounts."
fi
fi
exit 0