forked from andreasfaerber/vzpbackup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vzprestore.sh
executable file
·212 lines (181 loc) · 5.14 KB
/
vzprestore.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
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
#!/bin/bash
#
# vzprestore.sh
#
# A script meant to restore backups that have been taken with
# vzploopbackup. The script will create the relevant directories
# required to restore the container from a tar backup.
#
# Caution has been taken not to overwrite existing directories or
# config files. Please use caution as no detailed testing and error
# handling has been implemented as of yet.
#
# Author: Andreas Faerber, af@maeh.org
##
## DEFAULTS
##
ARCHIVE=
CONTAINER=
CONFIRM=yes
DELETE_BACKUP_SNAPSHOT=no
##
## VARIABLES
##
TIMESTAMP=`date '+%Y%m%d%H%M%S'`
VZDIR=
## VARIABLES END
show_usage() {
echo "Usage: $0 --archive=<Filename> --container=<CTID to restore to> [--vzdir=<Directory to restore VE_PRIVATE and VE_ROOT to>] [--confirm=<yes/no>] [--delete-backup-snapshot=<yes/no>]"
echo "Defaults:"
echo -e "Archive:\t\t\tNONE"
echo -e "Container:\t\t\tNONE"
echo -e "Confirm:\t\t\tYes"
echo -e "Delete Backup Snapshot:\t\tNo"
echo -e "VZ Directory (for VE_ROOT and VE_PRIVATE):\tGlobal Default"
echo
echo "Note: Deleting the backup snapshot causes a switch to and a deletion"
echo " of the snapshot taken during backup. Doing so will cause any"
echo " running container to be rebooted. You will not be able to"
echo " resume the container from a suspended state."
echo
echo "You need to give at least --archive and --container as arguments"
}
for i in "$@"
do
case $i in
--help)
show_usage;
exit 0;
;;
--vzdir=*)
VZDIR=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
;;
--archive=*)
ARCHIVE=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
;;
--container=*)
CONTAINER=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
;;
--confirm=*)
CONFIRM=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
;;
--delete-backup-snapshot=*)
DELETE_BACKUP_SNAPSHOT=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
;;
*)
# Parse CTIDs here
;;
esac
done
ARC_EXT=${ARCHIVE##*.}
if [ "x"$ARCHIVE == "x" -o "x"$CONTAINER == "x" ]; then
show_usage;
exit 0;
fi
if [ ! -f $ARCHIVE ]; then
echo "Archive $ARCHIVE does not exist or is inaccessible"
exit 1;
fi
CTID=$CONTAINER
if [ "x"$VZDIR == "x" ]; then
VE_PRIVATE=$(VEID=$CTID; source /etc/vz/vz.conf; echo $VE_PRIVATE)
VE_ROOT=$(VEID=$CTID; source /etc/vz/vz.conf; echo $VE_ROOT)
else
VE_PRIVATE=$VZDIR/private/$CTID
VE_ROOT=$VZDIR/root/$CTID
fi
VE_DUMP=$(VEID=$CTID; source /etc/vz/vz.conf; echo $DUMPDIR)
echo -e "Archive to restore:\t\t$ARCHIVE"
echo -e "Container to restore to:\t\t$CONTAINER"
echo -e "Confirm restore:\t\t$CONFIRM"
echo -e "Restoring VZ to:\t\t$VZDIR"
echo
echo "Pre-Restore Checks.."
echo -n "Checking if container private directory ($VE_PRIVATE) already exists.."
if [ -d $VE_PRIVATE ]; then
echo "yes, aborting"
exit 0;
else
echo "no"
echo "$VE_PRIVATE directory will be created during restore"
fi
echo -n "Checking if container root directory ($VE_ROOT) already exists.."
if [ -d $VE_ROOT ]; then
echo "yes, aborting"
exit 0;
else
echo "no"
echo "$VE_ROOT directory will be created during restore"
fi
echo -n "Checking if container config file (/etc/vz/conf/$CTID.conf) already exists.."
if [ -d "/etc/vz/conf/$CTID.conf" ]; then
echo "yes, aborting"
exit 0;
else
echo "no"
echo "/etc/vz/conf/$CTID.conf will be restored from backup"
fi
echo
echo "Actions taken for restore:"
echo "mkdir $VE_ROOT"
echo "mkdir $VE_PRIVATE"
echo "cd $VE_PRIVATE"
echo "Extract backup archive into $VE_PRIVATE"
echo "Create container config /etc/vz/conf/$CTID.conf"
echo "Amend container config VE_ROOT: $VE_ROOT"
echo "Amend container config VE_PRIVATE: $VE_PRIVATE"
echo
if [ "x"$CONFIRM == "xyes" ]; then
read -p "Confirm restore (yes/no): " INPUT
if [ "x"$INPUT != "xyes" ]; then
echo "Exiting.."
exit 0;
fi
fi
echo "Creating directory $VE_ROOT"
mkdir $VE_ROOT
echo "Creating direcotry $VE_PRIVATE"
mkdir $VE_PRIVATE
echo "cd into $VE_PRIVATE"
cd $VE_PRIVATE
echo "Extracting backup archive:"
if [ $ARC_EXT == "bz2" ]; then
TAR_ARGS="-xvjf"
elif [ $ARC_EXT == "gz" ]; then
TAR_ARGS="-zxvf"
elif [ $ARC_EXT == "xz" ]; then
TAR_ARGS="-xJf"
else
TAR_ARGS="-xvf"
fi
tar $TAR_ARGS $ARCHIVE
BACKUP_ID=$(cat $VE_PRIVATE/vzpbackup_snapshot)
echo BACKUP_ID: $BACKUP_ID
SRC_VE_CONF="dump/{$BACKUP_ID}.ve.conf"
echo "SRC VE CONF: $SRC_VE_CONF"
mv $SRC_VE_CONF /etc/vz/conf/$CTID.conf.new
egrep -v '^(VE_ROOT|VE_PRIVATE)' /etc/vz/conf/$CTID.conf.new > /etc/vz/conf/$CTID.conf
echo "VE_ROOT=$VE_ROOT" >> /etc/vz/conf/$CTID.conf
echo "VE_PRIVATE=$VE_PRIVATE" >> /etc/vz/conf/$CTID.conf
rm /etc/vz/conf/$CTID.conf.new
for f in $(ls -1 dump/{$BACKUP_ID}.ve.*)
do
echo $f
CONF_EXT=${f##*.}
echo mv $f /etc/vz/conf/$CTID.$CONF_EXT
done
# Look for possible dump
DUMPFILE=${SRC_VE_CONF%.*.*}
echo DUMPFILE: $DUMPFILE
if [ -f $DUMPFILE ]; then
echo "Found possible dump file.. moving it to $VE_DUMP"
mv $DUMPFILE $VE_DUMP/Dump.$CTID
else
echo "No dump file found"
fi
if [ "x"$DELETE_BACKUP_SNAPSHOT == "xyes" ]; then
echo "Deleting backup snapshot.."
vzctl snapshot-switch $CTID --id $BACKUP_ID
vzctl snapshot-delete $CTID --id $BACKUP_ID
fi
vzlist $CTID