-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackup_received.sh
More file actions
executable file
·57 lines (55 loc) · 1.27 KB
/
backup_received.sh
File metadata and controls
executable file
·57 lines (55 loc) · 1.27 KB
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
#!/bin/sh
bdir=$1
if [[ ! $bdir ]]; then
echo "Error: no parameters given!"
exit 3
fi
if [[ ! -d $bdir ]]; then
mkdir -p $bdir
if [[ $? -gt 0 ]]; then
echo "Failed to create directory $bdir" 1>&2
exit 3
fi
fi
shift
bn=$1
shift
cleanup_old=$1
shift
ferr=$bdir/$bn.stderr
cd $bdir 2>>$ferr
echo "parameters: $bdir $bn $cleanup_old $@" >> $ferr
for f in "$@" ; do
# these files are bigger and should have slower turnover -- keep only the last 5 archives
if [[ $f = $bn* ]]; then
fb="${f/$bn/}" # created by current cron job
c=0
for fn in $( ls -1t *$fb ) ; do
(( c++ ))
if [[ $fn != $f && $c -gt 5 ]]; then
#echo "$fn (..to delete)"
/bin/rm -f $fn
#else
# echo "$fn <-- will NOT be deleted!"
fi
done
else
#not a current file, but from a previous backup
if [[ -f $f ]]; then
touch $f
else # file not found!
host=$(hostname -s)
echo "Error on $host: file $f not found among current backups!" >> $ferr
fi
fi
done
numfiles=$(ls -1 | wc -l)
if [[ $numfiles -lt 6 ]]; then
#don't cleanup when there are no previous backups
cleanup_old=0
fi
if [[ $cleanup_old -gt 0 ]]; then
#echo "Cleaning up backup files older than 30 days.." 2>>$ferr
nice -n19 find . -maxdepth 1 -mtime +30 -exec /bin/rm -rf {} \;
fi
cat $ferr