-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotatebackup.sh
executable file
·137 lines (106 loc) · 3.47 KB
/
rotatebackup.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
#!/bin/bash
dir=`dirname $0`
cd $dir
# include config
[ -f $dir/config ] && . $dir/config
for i in "$@"
do
case $i in
-s=*|--server=*)
server="${i#*=}"
;;
--backupfs=*)
backupfs="${i#*=}"
;;
--savepath=*)
savepath="${i#*=}"
;;
--prefix=*)
prefix="${i#*=}"
;;
--sizeback=*)
let sizeback="${i#*=}"*1024*1024
;;
--countback=*)
countback="${i#*=}"
;;
"--sudo=yes")
sudo="sudo -E"
;;
-h|--help)
help=1
;;
*)
echo $i unknown option
;;
esac
done
printhelp() {
cat << EOF
params: need: description:
-s|--server yes servername set local if backup localhost filesystem
-prefix no prefix servername - need to human readable save path
--backupfs yes one filesystem (not coma separated), if rsync type - backupfs is modulename cfg file
--savepath yes path to local backup dir
--sizeback no GB. Max backup size on hard drive. Need if not define countback.
--countback no Count max backups on hard drive. Need if not define sizeback.
--sudo no Set yes if need use local sudo rsync
-h|--help no print this help
EOF
exit
}
[[ $help -eq 1 ]] && printhelp
#check params
for i in "$server" "$backupfs" "$savepath" "$backupfs"
do
cnt=$((cnt+1))
if [[ "$i" == "" ]]; then
echo Mandatory param $cnt is empty. Need params: "server backupfs savepath backupfs"
exit 1
fi
done
if [[ $backupfs == *,* ]]; then
echo This script only works with one file system at a time.
exit 1
fi
. `dirname @0`/function
for backup in `echo $backupfs | sed "s/,/\ /g"`; do
fs=`fsname $backup`
#rotate by countback
if [ ! -z $countback ]; then
printf "%s" "Rotation backup by COUNT SRV:$fservername FS:$fs..."
countback_current=$(echo $savepath/$fservername/$fs-* |tr ' ' \\n|wc -l)
if [ "$countback_current" -gt "$countback" ]; then
let diff=$countback_current-$countback
for rotate in `echo $savepath/$fservername/$fs-* `; do
if [ "$diff" -gt "0" ]; then
rm -rf $rotate
let diff=$diff-1
fi
done
printf "%s" "Done!"
else
printf "%s\n" "Not need rotation $fs by COUNT! Total backups: $countback_current."
fi
fi
# rotate by sizeback
if [ ! -z $sizeback ]; then
printf "%s" "Rotation by SIZE $fs..."
ducount "$savepath/$fservername/latest-$fs/ $savepath/$fservername/$fs-* " "$savepath/$fservername/latest-$fs/du-all.txt"
sizeback_current=`cat $savepath/$fservername/latest-$fs/du-all.txt`
if [ "$sizeback_current" -ge "$sizeback" ]; then
for rotate in `echo $savepath/$fservername/$fs-* `; do
ducount "$savepath/$fservername/latest-$fs $savepath/$fservername/$fs-*" "$savepath/$fservername/latest-$fs/du-all.txt"
sizeback_current=`cat $savepath/$fservername/latest-$fs/du-all.txt`
if [ "$sizeback_current" -ge "$sizeback" ]; then
echo rotate $rotate
rm -rf $rotate $savepath/$fservername/latest-$fs/du-all.txt $rotate
fi
done
printf "%s" "Done!"
else
printf "%s\n" "Not need rotation $fs by SIZE! Current catalog size: `echo $sizeback_current | awk '{print $1/1024/1024"GB" }'`"
fi
fi
echo
done