-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpurge_sftp
executable file
·98 lines (85 loc) · 2.55 KB
/
purge_sftp
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
#!/bin/bash
configfile=server.conf
usage() { echo "Usage: $0 [ -d ] [ -i ] [ -r <recent_to_keep> ] [-c <configfile>] [filter [...]]" 1>&2; exit $1; }
dryrun=0
ignoredownload=0
while getopts "r:c:dih" o; do
case "${o}" in
c) configfile=${OPTARG}
if [[ ! -r ${configfile} ]]; then
echo "Cannot read ${configfile}" 1>&2
usage 1
fi
;;
d) dryrun=1 ;;
i) ignoredownload=1 ;;
r) recent=${OPTARG}
if [[ ! ${recent} =~ ^20[[:digit:]]{2}-?([01][[:digit:]]-?([0-3][[:digit:]])?)?$ ]]; then
echo "Wrong format ${recent}" 1>&2
usage 2
fi
;;
h) usage 0 ;;
*) usage 1 ;;
esac
done
shift $((OPTIND-1))
. ${configfile}
: ${fileserver:?}
# ${srvport}
: ${expname:?}
: ${basedir:=$(pwd)}
: ${download:?}
if [[ $( < ~/.netrc ) =~ machine[[:space:]]+${fileserver}[[:space:]]?login[[:space:]]+([^[:space:]]+) ]]; then
username="${BASH_REMATCH[1]}"
elif [[ $( < ~/.ssh/config ) =~ Host[[:space:]]+${fileserver}[[:space:]]+ ]]; then
username=
else
echo "cannot find login for machine ${fileserver} in ~/.netrc" >&2
exit 1
fi
du_option=
if (( ${#@} )); then
dir=( "${@/#/ /${expname}/}" )
source="${dir[*]}"
if (( ${#dir[@]} > 1)); then
du_option='c'
fi
else
source="/${expname}/"
fi
if [[ -z "${recent}" ]]; then
echo -e "\e[31;1mWarning, no date limit set to purge ${fileserver}\e[0m"
else
echo -e "\e[37;1mPurging older than ${recent} from ${fileserver}\e[0m"
fi
purgelist=( )
for d in $(lftp -c "connect sftp://${username:+${username}@}${fileserver}${srvport:+:${srvport}}; cls -q1 ${source};"); do
b=$(basename "${d}")
if [[ ! -d "${basedir}/${download}/${d}" ]] && (( ! ignoredownload )); then
echo "${d} not present!"
continue
elif [[ ! "${b}" =~ ^(20[[:digit:]]{2})-?([01][[:digit:]])-?([0-3][[:digit:]]) ]]; then
echo "${b} cannot parse"
continue
elif [[ -n "${recent}" && "${b}" > "${recent}" ]]; then # lexicographic order, so it works with subsets
echo "${b} skip"
else
purgelist+=( "${d}" )
echo "${b} to be purged"
fi
done
if (( ${#purgelist[@]} > 0 )) && [[ -n "${purgelist[*]}" ]]; then
if [[ "${purgelist[*]}" =~ [\;\"\'] ]]; then
echo "<${purgelist[*]}> invalid character ${BASH_REMATCH[0]}"
exit 1
fi
if (( dryrun )); then
echo "Dry-run, not actually deleting"
exit 0
fi
exec lftp -c "connect sftp://${username:+${username}@}${fileserver}${srvport:+:${srvport}}; du -s${du_option}h ${source}; rm -r ${purgelist[*]}; sleep 5s; du -s${du_option}h ${source};"
else
echo "Nothing to do"
exec lftp -c "connect sftp://${username:+${username}@}${fileserver}${srvport:+:${srvport}}; du -s${du_option}h ${source};"
fi