forked from Stonyx/RSyncSnapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRSyncSnapshot_remote
executable file
·285 lines (255 loc) · 7.08 KB
/
RSyncSnapshot_remote
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/bin/bash
#
# Copyright (C) 2009-2018 Stonyx
# http://www.stonyx.com
#
# This script is free software. You can redistribute it and/or modify it under the terms of the GNU
# General Public License Version 3 (or at your option any later version) as published by The Free
# Software Foundation.
#
# This script is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# If you did not received a copy of the GNU General Public License along with this script see
# http://www.gnu.org/copyleft/gpl.html or write to The Free Software Foundation, 675 Mass Ave,
# Cambridge, MA 02139, USA.
# ------------------------------
# RSnapshot Type Script
# ------------------------------
# Usage: RSyncSnapshot Source_Directory Target_Directory Number_of_Snapshots_to_Keep Log_File
# Lock_File Optional_Additional_RSync_Arguments ...
USAGE="
Usage: bash RSyncSnapshot HOST USER Source_Directory Target_Directory
Number_of_Snapshots_to_Keep Log_File Lock_File
Optional_Additional_RSync_Arguments ...
Options:
HOST hostname of the remote machine, must be authorized without password, i.e., via ssh key pair
USER login name on the remote machine
Source_Directory the local dir to be copied
Target_Directory the target dir on remote machine, must preexist and full path, not include \"user@host:\"
Number_of_Snapshots_to_Keep a integer number
Log_File Must be a local file
Lock_File any file indicating a RSyncSnapshot session is running if exists
"
# ----- Commands -----
# NAS4Free command locations
ECHO=/bin/echo
SLEEP=/bin/sleep
RM=/bin/rm
MV=/bin/mv
MKDIR=/bin/mkdir
RSYNC=/usr/bin/rsync
TOUCH=/usr/bin/touch
# ----- RSync Options -----
# Explanation of default options
# a = archive mode; equals rlptgoD
# r = recurse into directories
# l = copy symlinks as symlinks
# p = preserve permissions
# t = preserve modification times
# g = preserve group
# o = preserve owner
# D = preserve device and special files
# c = skip based on checksum, not mod-time & size
# v = increase verbosity
# x = don't cross filesystem boundaries
# H = preserve hard links
# S = handle sparse files efficiently
RSYNC_OPTIONS="-acvxHS"
# ----- Script -----
# Make things pretty
$ECHO
# Get HOST and USER
HOST=$1; shift
USER=$1; shift
# Process the source argument
if [[ ! $1 ]]
then
#$ECHO "${USAGE}"
echo "${USAGE}"
$ECHO
$ECHO "Source directory not specified."
$ECHO
exit 1
fi
SOURCE=$1
if [[ ! -d $SOURCE ]]
then
$ECHO "Specified source directory (\"$SOURCE\") doesn't exist."
$ECHO
exit 1
fi
# Process the target argument
if [[ ! $2 ]]
then
echo "${USAGE}"
$ECHO
$ECHO "Target directory not specified."
$ECHO
exit 1
fi
TARGET=${2%/}
#if [[ ! -d $TARGET ]]
if ! ssh ${HOST} "test -e ${TARGET}"
then
$ECHO "Specified target directory (\"$TARGET\") doesn't exist."
$ECHO
exit 1
fi
# Process the snapshot count argument
if [[ ! $3 ]]
then
echo "${USAGE}"
$ECHO
$ECHO "Number of snapshots to keep not specified."
$ECHO
exit 1
fi
SNAPSHOT_COUNT=$3
if [[ ! $SNAPSHOT_COUNT =~ ^[0-9]+$ ]]
then
$ECHO "Specified number of snapshots to keep has to be a numeric value."
$ECHO
exit 1
fi
if [[ $SNAPSHOT_COUNT -lt 1 ]]
then
$ECHO "Specified number of snapshots to keep can not be less than one."
$ECHO
exit 1
fi
# Process the log file argument
if [[ ! $4 ]]
then
echo "${USAGE}"
$ECHO
$ECHO "Log file not specified."
$ECHO
exit 1
fi
LOG_FILE=$4
# Process the lock file argument
if [[ ! $5 ]]
then
echo "${USAGE}"
$ECHO
$ECHO "Lock file not specified."
$ECHO
exit 1
fi
LOCK_FILE=$5
# Make sure no other instance of this script is running
if [[ -e $LOCK_FILE ]]
then
$ECHO "Another instance of the RSyncSnapshot script is already running."
$ECHO "Press Ctrl+C to cancel this script or if no other copy of RSnapshot"
$ECHO "is actually running delete the \"$LOCK_FILE\" file."
$ECHO
$ECHO -n "Waiting for the other instance of RSyncSnapshot to finish ..."
$ECHO "RSyncSnapshot: Waiting for another instance of RSyncSnapshot to finish." >> "$LOG_FILE"
# Check every 15 seconds if the other instance is done
$SLEEP 15
while [[ -e $LOCK_FILE ]]
do
$ECHO -n "."
$SLEEP 15
done
# Make things pretty
$ECHO
$ECHO
fi
# Create the lock file
: > "$LOCK_FILE"
# Find the first snapshots that doesn't exist
SNAPSHOT=0
while [[ $SNAPSHOT -le $SNAPSHOT_COUNT ]]
do
# Check if the snapshot directory doesn't exist
#if [[ ! -d "$TARGET/Snapshot.$SNAPSHOT" ]]
if ! ssh ${HOST} "test -e $TARGET/Snapshot.$SNAPSHOT"
then
break
fi
# Increment the counter by 1
SNAPSHOT=$((SNAPSHOT+1))
done
if [[ $SNAPSHOT -eq $((SNAPSHOT_COUNT+1)) ]]
then
SNAPSHOT=-1
fi
# Delete the oldest snapshot if all snapshots exist
if [[ $SNAPSHOT -eq -1 ]]
then
$ECHO "Deleting oldest snapshot (number $SNAPSHOT_COUNT) ..."
$ECHO "RSyncSnapshot: Deleting oldest snapshot (number $SNAPSHOT_COUNT)." >> "$LOG_FILE"
#$RM -rf "$TARGET/Snapshot.$SNAPSHOT_COUNT"
ssh ${HOST} "rm -rf $TARGET/Snapshot.$SNAPSHOT_COUNT"
fi
# Make each snapshot one snapshot older (up to the first snapshot that doens't exist)
if [[ $SNAPSHOT -eq -1 ]]
then
SNAPSHOT=$SNAPSHOT_COUNT
fi
while [[ $SNAPSHOT -gt 0 ]]
do
# Reduce the counter by 1
SNAPSHOT=$((SNAPSHOT-1))
# Move the snapshot
$ECHO "Moving snapshot number $SNAPSHOT to $((SNAPSHOT+1)) ..."
$ECHO "RSyncSnapshot: Moving snapshot number $SNAPSHOT to $((SNAPSHOT+1))." >> "$LOG_FILE"
#$MV -f "$TARGET/Snapshot.$SNAPSHOT" "$TARGET/Snapshot.$((SNAPSHOT+1))"
ssh ${HOST} "mv -f $TARGET/Snapshot.$SNAPSHOT" "$TARGET/Snapshot.$((SNAPSHOT+1))"
done
# Find the first snapshot that exists
SNAPSHOT=1
while [[ $SNAPSHOT -le $SNAPSHOT_COUNT ]]
do
# Check if the snapshot directory exists
#if [[ -d "$TARGET/Snapshot.$SNAPSHOT" ]]
if ssh ${HOST} "test -e $TARGET/Snapshot.$SNAPSHOT"
then
break
fi
# Increment the counter by 1
SNAPSHOT=$((SNAPSHOT+1))
done
if [[ $SNAPSHOT -eq $((SNAPSHOT_COUNT+1)) ]]
then
SNAPSHOT=-1
fi
# Create the snapshot directory
#$MKDIR "$TARGET/Snapshot.0"
ssh ${HOST} "mkdir $TARGET/Snapshot.0"
# Create the RSync command
RSYNC_COMMAND="$RSYNC $RSYNC_OPTIONS "
if [[ $SNAPSHOT -ne -1 ]]
then
RSYNC_COMMAND+="--link-dest=\"../Snapshot.$SNAPSHOT\" "
fi
RSYNC_COMMAND+="--log-file=\"$LOG_FILE\" "
for (( i=5; i<$#; i++ ))
do
RSYNC_COMMAND+="\"$@[$i]\" "
done
RSYNC_COMMAND+="\"$SOURCE\" "
RSYNC_COMMAND+="\"${USER}@${HOST}:${TARGET}/Snapshot.0\""
# Do the actual backup using RSync
$ECHO "Backing up \"$SOURCE\" to snapshot number 0 using the command below ..."
$ECHO "$RSYNC_COMMAND"
$ECHO
$ECHO "RSyncSnapshot: Backing up \"$SOURCE\" to snapshot number 0 using the command below:" >> \
"$LOG_FILE"
$ECHO "RSyncSnapshot: $RSYNC_COMMAND" >> "$LOG_FILE"
$ECHO >> "$LOG_FILE"
eval $RSYNC_COMMAND
# Update the time of the snapshot directory
#$TOUCH "$TARGET/Snapshot.0"
ssh ${HOST} "$TOUCH $TARGET/Snapshot.0"
# Remove the lock file
$RM -f "$LOCK_FILE"
# Make things pretty
$ECHO
$ECHO >> "$LOG_FILE"
# All done
exit 0