-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackupvm2.sh
More file actions
executable file
·265 lines (224 loc) · 6.48 KB
/
Copy pathbackupvm2.sh
File metadata and controls
executable file
·265 lines (224 loc) · 6.48 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
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
#!/bin/bash
# v 0.2
# Author: vitaliy.okulov@gmail.com
# Web: http://vokulov.ru
# License: GPL
# TODO: Rewrite check_tool, create_snapshot, delete_snapshot functions
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/comcom
# Config
VMLIST="testrelo prodrelo"
LXCDIR="/var/lib/lxc"
BACKUPDIR="/backup/vmfiles"
EXCLUDEDIR="/var/lib/mysql"
CURRDATE=$(date +%Y%m%d)
# Set number of FULL backup for each VM in VMLIST that we want to keep in BACKUPDIR
RTNF=5
# Set args vor dar backup tool
DARARGSFULL="-D -Q -z 5"
DARARGSDIFF=$DARARGSFULL" -A"
# Choose: btrfs or lvm
FS="lvm"
LVMDISK="/dev/VG0/LV0"
# Set if u dont have btrfs or lvm partition
DEV=1
function check_tools {
# Lets find btrfs tools binary file
if [ "$FS" == "btrfs" ] && [ -f "/sbin/btrfs" ];then
echo "Btrfs tool binary was found"
fi
# Lets check for dar backup tool
if [ -f "/usr/bin/dar" ];then
echo "Dar binary was found"
else
echo "Please install dar backup tool"
exit 1
fi
# Lets check lvcreate tool
if [ "$FS" == "lvm" ] && [ -f "/sbin/lvcreate" ];then
echo "lvcreate binary was found"
else
echo "Please install lvm"
exit 1
fi
return 0
}
function create_snapshot {
# Local var
# Set VM name to first argument
VM=$1
# Check if $DEV flag is off and we use btrfs tool to create snapshot
if [ $DEV -eq 0 ] && [ $FS == "btrfs" ];then
# Lets check of snapshot directory already created
if [ -d $LXCDIR/snap/$VM ];then
echo "Snapshot for $VM already created, please check snapshot directory"
exit 1
fi
# Lets create read only snapshot
btrfs subvolume snapshot -r $LXCDIR/$VM/rootfs $LXCDIR/snap/$VM || \
{ echo "Snapshot of $VM VM failed"; \
exit 1; }
fi
# Check if $DEV flag is off and we use lvcreate tool to create snapshot
if [ $DEV -eq 0 ] && [ $FS == "lvm" ];then
# Lets check for old lvm snapshots
SNAPSHOT=$(lvdisplay | grep $LVMDISK"_snap")
if [ "$SNAPSHOT" != "" ];then
echo "Snapshot for $VM already created, please check LVM snapshot"
exit 1
fi
# Lets create shapshot
# Freeze VM
lxc-freeze -n $VM || \
{ echo "Cant freeze $VM"; exit 1; }
# Create LVM snapshot
lvcreate -L 5G -n $LVMDISK"_snap" -s $LVMDISK || \
{ lxc-unfreeze -n $VM; echo "Snapshot of $VM VM failed"; \
exit 1; }
# Unfreeze VM
lxc-unfreeze -n $VM || \
{ echo "Lets try to unfreeze $VM second time"; lxc-unfreeze -n $VM; }
# Mount LVM to SNAPSHOT directory
mount $LVMDISK"_snap" $LXCDIR/snap/ || \
{ echo "Cant mount LVM snapshot to snap folder"; exit 1; }
# Lets sycn fs state
sync
sleep 3
fi
}
function delete_snapshot {
# Local var
# Set VM name to first argument
VM=$1
# Check if $DEV flag is off and we use btrfs tool to create snapshot
if [ $DEV -eq 0 ] && [ $FS == "btrfs" ];then
# Lets check of snapshot directory already created
if [ -d $LXCDIR/snap/$VM ];then
echo "Found $VM snapshot directory, lets try to delete it"
btrfs subvolume delete $LXCDIR/snap/$VM || \
{ echo "Cant delete snapshot for $VM"; \
exit 1; }
else
echo "Strange, but $VM snapshot directory not found, please check snapshot directory"
exit 1
fi
fi
# Check LVM snapshot
if [ $DEV -eq 0 ] && [ $FS == "lvm" ];then
# Lets check for snapshot
SNAPSHOT=$(lvdisplay | grep $LVMDISK"_snap")
if [ "$SNAPSHOT" != "" ];then
echo "Found snapshot for $VM, lets try to delete it"
# Lets umount snapshot
umount $LXCDIR/snap/
# Remove snapshot
lvremove -f $LVMDISK"_snap" || \
{ echo "Cant delete snapshot for $VM"; \
exit 1; }
else
echo "Strange, but $VM snapshot not found, please check LVM"
exit 1
fi
fi
}
function check_archive_file {
# Local vars
VM=$1
TYPE=$2
# Lets find archive with current date name, or dar will rise error if archive already exist
BACKUP=$(ls $BACKUPDIR | sort | grep $VM | grep $TYPE | grep $CURRDATE)
if [ "$BACKUP" != "" ];then
echo "Backup file for $VM and for $CURRDATE already created. Please check $BACKUPDIR"
return 1
else
return 0
fi
}
function create_archive {
# Local var
VM=$1
TYPE=$2
# Lets create FULL type of archive
if [ "$TYPE" == "FULL" ];then
cd $LXCDIR/snap/ && \
dar $DARARGSFULL -c $BACKUPDIR/$VM"_"$CURRDATE"_"$TYPE -P $VM$EXCLUDEDIR
return $?
else
# We have only 2 types of backups: FULL or DIFF
TYPE="DIFF"
# Lets find lastt FULL backup
LASTFULL=$(ls $BACKUPDIR | sort | grep $VM | grep "FULL" | tail -n 1 | cut -d "." -f 1)
# Dar need name of last full backup without slice name and extension
DATEOFFULLBACKUP=$(echo $LASTFULL | cut -d "_" -f 2)
if [ "$LASTFULL" != "" ];then
cd $LXCDIR/snap/ && \
dar $DARARGSDIFF $BACKUPDIR/$LASTFULL -c $BACKUPDIR/$VM"_"$CURRDATE"_"$TYPE"BACKUPFOR_"$DATEOFFULLBACKUP -P $VM$EXCLUDEDIR
return $?
else
echo "Cant find last FULL backup for $VM. Please run FULL backup first"
return 1
fi
fi
}
function delete_old_archives {
# Local var
VM=$1
cd $BACKUPDIR
# Create reverse ordered array of all FULL archives for that VM
FULL=($(ls -r | grep $VM | grep "FULL"))
# Count size of array
FULLNUM=${#FULL[*]}
if [ $FULLNUM -gt $RTNF ];then
# Slice array for more than RTNF number of FULL archives, and remove FULL and DIFF archives with that date
for OLDARCH in ${FULL[*]:$RTNF};do
FULLDATE=$(echo $OLDARCH | cut -d "_" -f 2)
# Keep last $RTNF files in backp directory
find $BACKUPDIR -type f -name $VM"*"$FULLDATE"*.dar" \
| sort \
| xargs --no-run-if-empty rm -f || \
{ echo "Cant delete old archive $OLDARCH"; };
done
fi
return 0
}
function main {
# Local vars
TYPE=$1
for VM in $VMLIST;do
create_snapshot $VM
check_archive_file $VM $TYPE
if [ $? -eq 0 ];then
create_archive $VM $TYPE
fi
delete_snapshot $VM
# Remove old backup files
if [ -d "$BACKUPDIR" ]; then
delete_old_archives $VM
else
echo "Cant access backup directory"
exit 1
fi;
done
}
# Main cycle
# Lets sync fs
sync && sleep 1
check_tools
case $1 in
"FULL")
main "FULL"
;;
"DIFF")
main "DIFF"
;;
*)
echo "Usage:"
echo "FULL - for full backups"
echo "DIFF - for diff backups"
echo ""
echo "You can create cron jobs for this script:"
echo "0 3 * * 7 cd /usr/local/comcom; ./backupvm2.sh FULL"
echo "0 3 * * 1-6 cd /usr/local/comcom; ./backupvm2.sh DIFF"
exit 1
;;
esac
exit 0