-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopymovedelete.sh
executable file
·353 lines (317 loc) · 11.5 KB
/
copymovedelete.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/bin/bash
set -euo pipefail
###############################################
#
# SF tool that performs data copy/move/delete.
# Uses sf job engine combined with rsync_wrapper to copy/move/delete data
#
###############################################
#********************************************************
#
# Starfish Storage Corporation ("COMPANY") CONFIDENTIAL
# Unpublished Copyright (c) 2011-2018 Starfish Storage Corporation, All Rights Reserved.
#
# NOTICE: All information contained herein is, and remains the property of COMPANY. The intellectual and
# technical concepts contained herein are proprietary to COMPANY and may be covered by U.S. and Foreign
# Patents, patents in process, and are protected by trade secret or copyright law. Dissemination of this
# information or reproduction of this material is strictly forbidden unless prior written permission is
# obtained from COMPANY. Access to the source code contained herein is hereby forbidden to anyone except
# current COMPANY employees, managers or contractors who have executed Confidentiality and Non-disclosure
# agreements explicitly covering such access.
#
# ANY REPRODUCTION, COPYING, MODIFICATION, DISTRIBUTION, PUBLIC PERFORMANCE, OR PUBLIC DISPLAY OF OR
# THROUGH USE OF THIS SOURCE CODE WITHOUT THE EXPRESS WRITTEN CONSENT OF COMPANY IS STRICTLY PROHIBITED,
# AND IN VIOLATION OF APPLICABLE LAWS AND INTERNATIONAL TREATIES. THE RECEIPT OR POSSESSION OF THIS SOURCE
# CODE AND/OR RELATED INFORMATION DOES NOT CONVEY OR IMPLY ANY RIGHTS TO REPRODUCE, DISCLOSE OR DISTRIBUTE
# ITS CONTENTS, OR TO MANUFACTURE, USE, OR SELL ANYTHING THAT IT MAY DESCRIBE, IN WHOLE OR IN PART.
#
# FOR U.S. GOVERNMENT CUSTOMERS REGARDING THIS DOCUMENTATION/SOFTWARE
# These notices shall be marked on any reproduction of this data, in whole or in part.
# NOTICE: Notwithstanding any other lease or license that may pertain to, or accompany the delivery of,
# this computer software, the rights of the Government regarding its use, reproduction and disclosure are
# as set forth in Section 52.227-19 of the FARS Computer Software-Restricted Rights clause.
# RESTRICTED RIGHTS NOTICE: Use, duplication, or disclosure by the Government is subject to the
# restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in Technical Data and Computer
# Software clause at DFARS 52.227-7013.
#
#********************************************************
# Set variables
readonly VERSION="1.01 February 8, 2018"
readonly PROG="${0##*/}"
readonly SFHOME="${SFHOME:-/opt/starfish}"
readonly LOGDIR="$SFHOME/log/${PROG%.*}"
readonly NOW=$(date +"%Y%m%d-%H%M%S")
readonly LOGFILE="${LOGDIR}/$(basename ${BASH_SOURCE[0]} '.sh')-$NOW.log"
readonly STARFISH_BIN_DIR="${SFHOME}/bin"
readonly SF="${STARFISH_BIN_DIR}/client"
readonly SF_RSYNC="${STARFISH_BIN_DIR}/rsync_wrapper"
readonly SF_TAR="${STARFISH_BIN_DIR}/tar_wrapper"
# global variables
EMAIL=""
EMAILFROM="root"
MANIFEST_DIR_FLAG=""
EXTS=""
MINSIZE=""
MAXSIZE=""
SIZERANGE=""
MOVE_SRC_FILES=""
MIGRATE_SRC_OPTIONS=""
MODIFIER="a"
DAYS_AGO="30"
SFJOBOPTIONS=""
DRYRUN=0
RSYNC_CMD=""
TAR=0
logprint() {
echo -e "$(date +%D-%T): $*" >> $LOGFILE
}
email_alert() {
(echo -e "$1") | mailx -s "$PROG Failed!" -a $LOGFILE -r $EMAILFROM [email protected],$EMAIL
}
email_notify() {
(echo -e "$1") | mailx -s "$PROG Completed Successfully" -r $EMAILFROM $EMAIL
}
fatal() {
local msg="$1"
echo "${msg}" >&2
exit 1
}
check_parameters_value() {
local param="$1"
[ $# -gt 1 ] || fatal "Missing value for parameter ${param}"
}
check_volume_path() {
local vol_path="$1"
if [[ ! "${vol_path}" =~ ":" ]]; then
fatal "Volume should be passed as <volume>:<path> (but is: ${vol_path})"
fi
}
get_volume_mount() {
local vol="$1"
${SF} volume show "${vol}" --format "humanized_mounts"
}
check_path_exists() {
if [[ ! -d "$1" ]]; then
logprint "Directory $1 does not exist, exiting.."
echo "Directory $1 does not exist. Please create this path and re-run"
exit 1
else
logprint "Directory $1 found"
fi
}
usage() {
local msg="${1:-""}"
if [ ! -z "${msg}" ]; then
echo "${msg}" >&2
fi
cat <<EOF
Starfish copy/move/delete script
$VERSION
This script is a wrapper that invokes the SF job engine to copy/move/delete data using rsync_wrapper by default, or optionally the tar_wrapper.
This script collects data based on the last SF scan of the source volume.
USAGE:
${PROG} <source volume>:<source path> <destination volume>:<destination path> [options]
-h, --help - print this help and exit
Require Parameters:
<source volume>:<source path> - Source volume and path to archive
<destination volume>:<destination path> - Destination volume and path
Optional (rsync_wrapper only):
--days [int] - files older than this will be copied, based on midnight. Default = 30 days.
--mtime - use file modification time for --days. Default = atime.
--ext [extension] - only files that match this extension, if more than one, use "--ext bam --ext fastq"
--migrate - remove files from source after copy (default = no)
--minsize [size] - only files larger than this size (e.g. 100M or 10G). Default = 100M
--maxsize [size] - only files smaller than this size (e.g. 100M or 10G). Default = 100P
NOTE: minsize and maxsize cannot be used together!
Optional (tar only):
--tar - create a tar file in the destination directory of the files processed.
Optional (rsync or tar):
--email <recipients> - Recipient(s) for reports/alerts. Comma separated list.
--from <sender> - Email sender (default: root)
--from-scratch - Run job as if from scratch (do not track internally)
--job-name <jobname> - Specify a job name for the SF job
--dryrun - Do not execute the sf job command (useful for verifying command that will be run)
Examples:
$PROG nfs3:1 nfs4: --email [email protected]
Runs an rsync, copying data older than 30 days between the size range of 100M and 100P from nfs3:1 to nfs4. Email [email protected] with error during job execution
$PROG nfs3:1 nfs4: --days 0 --minsize 100K
Runs an rsync, copying all data up to midnight last night between the size range of 100K and 100P from nfs3:1 to nfs4.
$PROG nfs3:1 nfs4: --days -1 --maxsize 50K
Runs an rsync, copying all data (even data past midnight last night) between the size range of 0B and 50K from nfs3:1 to nfs4.
EOF
exit 1
}
parse_input_parameters() {
logprint "Parsing input parameters"
shift
while [[ $# -gt 0 ]]; do
case $1 in
"--email")
check_parameters_value "$@"
shift
EMAIL="$EMAIL,$1"
;;
"--from")
check_parameters_value "$@"
shift
EMAILFROM=$1
;;
"--mtime")
MODIFIER="m"
;;
"--migrate")
MOVE_SRC_FILES="--remove-source-files"
MIGRATE_SRC_OPTIONS="--no-entry-verification"
;;
"--days")
check_parameters_value "$@"
shift
DAYS_AGO="$1"
if [[ "${DAYS_AGO}" -lt -1 ]]; then
fatal "--days must be -1 or greater! (but is: ${DAYS_AGO})"
fi
;;
"--ext")
check_parameters_value "$@"
shift
EXTS="${EXTS} --ext $1"
;;
"--minsize")
check_parameters_value "$@"
shift
MINSIZE="$1"
;;
"--maxsize")
check_parameters_value "$@"
shift
MAXSIZE="$1"
;;
"--from-scratch")
SFJOBOPTIONS="$SFJOBOPTIONS --from-scratch"
;;
"--job-name")
check_parameters_value "$@"
shift
SFJOBOPTIONS="$SFJOBOPTIONS --job-name $1"
;;
"--dryrun")
DRYRUN=1
;;
"--tar")
TAR=1
;;
*)
logprint "input parameter: $1 unknown. Exiting.."
fatal "input parameter: $1 unknown. Exiting.."
;;
esac
shift
done
if [[ $MINSIZE != "" ]] && [[ $MAXSIZE != "" ]]; then
logprint "Both minsize and maxsize specified. Exiting.."
fatal "Both minsize and maxsize specified. Exiting.."
elif [[ $MINSIZE == "" ]] && [[ $MAXSIZE == "" ]]; then
SIZERANGE="--size 100M-100P"
elif [[ $MINSIZE == "" ]]; then
SIZERANGE="--size 0B-$MAXSIZE"
elif [[ $MAXSIZE == "" ]]; then
SIZERANGE="--size $MINSIZE-100P"
fi
logprint " Modifier: $MODIFIER"
logprint " Migrate Options: $MOVE_SRC_FILES"
logprint " Days: $DAYS_AGO"
logprint " Exts: $EXTS"
logprint " Size: $SIZERANGE"
logprint " Email: $EMAIL"
logprint " Tar: $TAR (if 1, values for Exts, Size, Days, Modifier, and migrate are ignored)"
logprint " Dryrun: $DRYRUN"
if [[ "$SFJOBOPTIONS" != "" ]]; then
logprint "SF job options: $SFJOBOPTIONS"
fi
}
build_and_run_cmd_line() {
# This script builds and runs the command to be executed in one step. Breaking it up into multiple
# steps and executing via $(command) command substitution resulted in parameters meant for
# rsync_wrapper (ie, -migrate) to be interpreted as being for the job engine, which caused the
# script to error out.
local rsync_or_tar
local cmd_options
local errorcode
local joboutput
local jobid
if [[ $TAR -eq 0 ]]; then
TIME="$(date --date "${DAYS_AGO} days ago" +"%Y%m%d")"
TIME_OPT="--${MODIFIER}time 19000101-${TIME}"
rsync_or_tar="${SF_RSYNC} ${MOVE_SRC_FILES}"
cmd_options="${EXTS} ${SIZERANGE} ${TIME_OPT} ${MIGRATE_SRC_OPTIONS}"
elif [[ $TAR -eq 1 ]]; then
rsync_or_tar="${SF_TAR}"
cmd_options=""
fi
if [[ $DRYRUN -eq 0 ]]; then
set +e
logprint "Starting SF job engine"
joboutput="$(${SF} job start "${rsync_or_tar}" ${SRC_VOL_WITH_PATH} ${DST_VOL_WITH_PATH} ${SFJOBOPTIONS} --wait ${cmd_options} 2>&1 | sed -n 1p)"
errorcode=$?
set -e
jobid=`echo "$joboutput" | awk '{print substr($0,length($0)-11,4)}'`
if [[ $errorcode -eq 0 ]]; then
logprint "SF job ID $jobid completed successfully"
else
set +e
logprint "SF job failed with error: $errorcode"
logprint "SF job status: $(sf job show $jobid)"
echo "SF job failed with error: $errorcode"
echo "SF job status: $(sf job show $jobid)"
email_alert "SF job failed. Job status $(sf job show $jobid)"
set -e
exit 1
fi
fi
}
[[ $# -lt 2 ]] && usage "Not enough arguments"
# if first parameter is -h or --help, call usage routine
if [ $# -gt 0 ]; then
[[ "$1" == "-h" || "$1" == "--help" ]] && usage
fi
# Check if logdir and logfile exists, and create if it doesnt
[[ ! -e $LOGDIR ]] && mkdir $LOGDIR
[[ ! -e $LOGFILE ]] && touch $LOGFILE
logprint "---------------------------------------------------------------"
logprint "Script executing"
logprint "$VERSION"
# Check that mailx exists
logprint "Checking for mailx"
if [[ $(type -P mailx) == "" ]]; then
logprint "Mailx not found, exiting.."
echo "mailx is required for this script. Please install mailx with yum or apt-get and re-run" 2>&1
exit 1
else
logprint "Mailx found"
fi
echo "Script starting"
echo "Step 1: Check source volume path"
check_volume_path "$1"
SRC_VOL_WITH_PATH="$1"
logprint "Source Volume & Path: $SRC_VOL_WITH_PATH"
SRC_VOL="${SRC_VOL_WITH_PATH%%:*}"
SRC_PATH="${SRC_VOL_WITH_PATH#*:}"
echo "Step 1 complete"
shift
echo "Step 2: Check destination volume path"
check_volume_path "$1"
DST_VOL_WITH_PATH="$1"
logprint "Destination Volume & Path: $DST_VOL_WITH_PATH"
DST_VOL="${DST_VOL_WITH_PATH%%:*}"
DST_PATH="${DST_VOL_WITH_PATH#*:}"
echo "Step 2 complete"
echo "Step 3: Parse remaining input parameters"
parse_input_parameters $@
echo "Step 3 complete"
echo "Step 4: Get path to the source volume"
SRCROOT="$(get_volume_mount "${SRC_VOL}")"
echo "Step 4 complete"
echo "Step 5: Build and run command line"
build_and_run_cmd_line
echo "Step 5 Complete"
exit 1