-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsync.sh
executable file
·135 lines (100 loc) · 2.33 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/bash
# This script will differntially copy your JENKINS_HOME to a remote server
# using Rsync
#
# It performs a differential backup using hardlinks, saving each backup with a date/timestamp directory
#
#
#
SERVER=
USER=
DATA='/data'
TARGET='~/work'
timestamp=`date +"%Y-%m-%d-%H_%M_%S"`
inprogress='/data/backuplog/inprogress'
excludefile='excludes.txt'
while getopts s:t:u: opt ; do
case $opt in
s)
SERVER=$OPTARG
;;
t)
TARGET=$OPTARG
;;
u)
USER=$OPTARG
;;
esac
done
shift $((OPTIND - 1))
CMD=`echo $1 | tr '[:upper:]' '[:lower:]'`
function usage {
echo "Usage $0 -s <target host> -t <target dir> -u <userid> " \
where cmd is push or pull \
-s is the server \
-t is target directory \
-u is the userid for the server
exit
}
function dieonerror {
exitcode=$?
if [ $exitcode -ne $1 ] ; then
echo ERROR: $2 : Exit code $exitcode
exit 1
fi
}
function doRsync {
echo Performing full back up to $2
rsync -Hav --delete --exclude-from $excludefile $1 $2
dieonerror 0 "Error running rsync"
}
function doDiffRsync {
echo Performing diff back up to $2, previous $3
rsync -Hav --delete --exclude-from $excludefile $1 $2 --link-dest $3
dieonerror 0 "Error running rsync"
}
case $CMD in
push)
# Grab last backup
mkdir -p /data/backuplog
lastbackup=`ls -tr /data/backuplog | tail -1 | rev | cut -d' ' -f1 | rev`
case $lastbackup in
'inprogress')
echo A backup is in progress and needs to be continued
type=`cat $inprogress | cut -d':' -f1`
timestamp=`cat $inprogress | cut -d':' -f2`
lastbackup=`cat $inprogress | cut -d':' -f3`
case $type in
FULL)
doRsync ${DATA} ${USER}@${SERVER}:${TARGET}/${timestamp}
;;
PARTIAL)
doDiffRsync ${DATA} ${USER}@${SERVER}:${TARGET}/${timestamp} ${TARGET}/${lastbackup}
;;
*)
echo unexpected previous backup type
exit 2
;;
esac
;;
'')
echo No previous completed backup
echo FULL:$timestamp >> $inprogress
doRsync ${DATA} ${USER}@${SERVER}:${TARGET}/${timestamp}
dieonerror 0 "Error running rsync"
;;
*)
echo Last Backup was $lastbackup
echo PARTIAL:$timestamp:$lastbackup >> $inprogress
doDiffRsync ${DATA} ${USER}@${SERVER}:${TARGET}/${timestamp} ${TARGET}/${lastbackup}
;;
esac
touch /data/backuplog/$timestamp
rm $inprogress
;;
pull)
;;
*)
usage
;;
esac