-
Notifications
You must be signed in to change notification settings - Fork 1
/
rotate-byspace.sh
69 lines (55 loc) · 1.58 KB
/
rotate-byspace.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
#!/bin/sh
# script for remove old backup files v0.01 pfzim (c) 2010 (44f5709e242b975305161941b30d1573)
# First files (sort by name) if free space less that defined
# File name template: backup-YYYY-MM-DD-HHMMSS-*
storage_path=''
storage_minspace=0
expired_cmd='rm -f'
awkcmd='awk'
while [ $# -gt 0 ]; do
key="$1"
case $key in
-p|--path)
storage_path="$2"
shift
shift
;;
-s|--space)
((storage_minspace=$2 * 1073741824))
shift
shift
;;
-h|--help)
storage_minspace=0
break
;;
*)
echo "Unknown argument: $1"
exit 1
;;
esac
done
echo 'Script for remove old backup files if free space less v0.02 pfzim (c) 2010'
if [ -z "${storage_path}" -o -z "${storage_minspace}" -o "${storage_minspace}" -le 0 ] ; then
echo 'Usage: rotate.sh -p /var/backups -s 30'
echo 'Options:'
echo ' -p|--path - path to stored backups'
echo ' -s|--space - free space required (GB)'
echo ' -h|--help - this help'
exit 1
fi
#echo "Path : ${storage_path}"
#echo "Min space : ${storage_minspace}"
for filename in `find ${storage_path} -type f -name 'backup-*' -print | sort`; do
free_space=`df --output=avail --block-size=1 ${storage_path} | tail --lines=1`
#echo "Free space: ${free_space}"
#echo "Min space : ${storage_minspace}"
if [ ${free_space} -ge ${storage_minspace} ] ; then
exit 0
fi
#filename=`basename $filename`
filename=`echo $filename | ${awkcmd} -F/ '{ print $NF; }'`
echo "Deleting file: $filename"
${expired_cmd} "${storage_path}/${filename}"
done
exit 0