-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpbs-linux-backup.sh
executable file
·254 lines (220 loc) · 5.56 KB
/
pbs-linux-backup.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
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
#!/bin/bash
# PBS Linux Backup Script V1.0
function f_log {
echo "$(date +"%Y-%m-%d-%H-%M-%S"):$1" | tee -a $LOGFILE
if [ "$2" = "error" ]
then
echo "$(date +"%Y-%m-%d-%H-%M-%S"):UNSUCCESSFULLY FINISHED BACKUP OF $HOST $JOBNAME ON $TIMESTAMP"
fi
}
function f_print {
echo "$(date +"%Y-%m-%d-%H-%M-%S"):$1"
}
function f_mail {
if [ $SENDMAILS = 1 ]
then
if [ "$1" = "error" ]
then
sendemail -f $MAILFROM -t $MAILTO -u "ERROR: PBS-LINUX-BACKUP $HOST $JOBNAME ON $TIMESTAMP" -m ":(" -s $MAILHOST -xu $MAILUSER -xp $MAILPASSWORD -o tls=$MAILTLS -a $LOGFILE
fi
if [ "$1" = "success" ] && [ $SENDMAILSONSUCCESS = 1 ]
then
sendemail -f $MAILFROM -t $MAILTO -u "SUCCESS: PBS-LINUX-BACKUP $HOST $JOBNAME ON $TIMESTAMP" -m ":)" -s $MAILHOST -xu $MAILUSER -xp $MAILPASSWORD -o tls=$MAILTLS -a $LOGFILE
fi
if [ "$1" = "warning" ]
then
sendemail -f $MAILFROM -t $MAILTO -u "WARNING: PBS-LINUX-BACKUP $HOST $JOBNAME ON $TIMESTAMP" -m ":/" -s $MAILHOST -xu $MAILUSER -xp $MAILPASSWORD -o tls=$MAILTLS -a $LOGFILE
fi
fi
}
function f_error {
f_log "$1" "error"
f_mail "error"
f_cleanup
exit $2
}
function f_cleanup {
f_log "Cleaning up"
if [ $MOUNTED = 1 ]
then
f_log "Unmounting LVM Snapshot"
umount -f $MOUNTPATH | tee -a $LOGFILE
if [ $PIPESTATUS -ne 0 ]
then
f_log "Warning: Could not unmount LVM Snapshot."
WARNING=1
fi
MOUNTED=0
f_log "Finished unmounting LVM Snapshot"
fi
if [ $SNAPSHOTTED = 1 ]
then
f_log "Deleting LVM Snapshot"
lvremove -f $SNAPSHOTPATH | tee -a $LOGFILE
if [ $PIPESTATUS -ne 0 ]
then
f_log "Warning: Could not delete LVM Snapshot."
WARNING=1
fi
SNAPSHOTTED=0
f_log "Finished deleting LVM Snapshot"
fi
if [ $LOCKED = 1 ]
then
f_log "Removing lock"
rm $LOCKFILE | tee -a $LOGFILE
if [ $PIPESTATUS -ne 0 ]
then
f_log "Warning: Could not remove lock."
WARNING=1
fi
LOCKED=0
f_log "Finished removing lock"
fi
f_log "Finished cleaning up"
}
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <configfile> (located in folder ./config)" >&2
exit 1
fi
##INPUT
JOBNAME=$1
##INPUT
WORKINGDIRECTORY=$(dirname "$BASH_SOURCE")
CONFIGFILE="$WORKINGDIRECTORY/config/$JOBNAME"
TIMESTAMP="$(date +"%Y-%m-%d-%H-%M-%S")"
#QUIT IF CONFIGFILE DOES NOT EXIST
if [ ! -f $CONFIGFILE ]
then
f_print "Error: Configfile not found, aborting."
exit 2
fi
chmod +x $CONFIGFILE
. $CONFIGFILE
#SET ENVIRONMENT VARIABLES
export PBS_REPOSITORY=$PBSREPOSITORY
export PBS_FINGERPRINT=$PBSFINGERPRINT
export PBS_PASSWORD=$PBSPASSWORD
export PBS_ENCRYPTION_PASSWORD=$PBSENCRYPTIONPASSWORD
#SET HOST
HOST=$(hostname)
#SET STATUS VARIABLES FOR CLEANUP
LOCKED=0
SNAPSHOTTED=0
MOUNTED=0
WARNING=0
#SET VARIABLES FOR LVM, IF USED
if [ $BACKUPMETHOD = "lvm" ] || [ $BACKUPMETHOD = "lvm-image" ]
then
SNAPSHOTNAME="pbs-linux-backup-$JOBNAME"
LVPATH="/dev/$VOLUMEGROUP/$LOGICALVOLUME"
SNAPSHOTPATH="/dev/$VOLUMEGROUP/$SNAPSHOTNAME"
MOUNTPATH="$WORKINGDIRECTORY/mounts/$JOBNAME"
fi
#Set Backup Path
if [ $BACKUPMETHOD = "lvm-image" ]
then
BKUPPATH=$SNAPSHOTPATH
FILETYPE=img
fi
if [ $BACKUPMETHOD = "lvm" ]
then
BKUPPATH=$MOUNTPATH
FILETYPE=pxar
fi
if [ $BACKUPMETHOD = "simple" ]
then
BKUPPATH=$BACKUPPATH
FILETYPE=pxar
fi
NS=""
if [ -v NAMESPACE ]
then
NS="--ns $NAMESPACE"
fi
#FILE PATHS
TIMESTAMPFILE="$WORKINGDIRECTORY/timestamps/$JOBNAME"
LOCKFILE="$WORKINGDIRECTORY/locks/$JOBNAME"
KEYFILE="$WORKINGDIRECTORY/keyfiles/$JOBNAME"
#CREATE FOLDER STRUCTURE
DIRTIMESTAMPFILE="$WORKINGDIRECTORY/timestamps/"
DIRLOCKFILE="$WORKINGDIRECTORY/locks/"
DIRLOGFILE="$WORKINGDIRECTORY/logs/"
DIRKEYFILE="$WORKINGDIRECTORY/keyfiles/"
mkdir -p $DIRTIMESTAMPFILE $DIRLOCKFILE $DIRLOGFILE $MOUNTPATH $DIRKEYFILE
#LOGFILE HANDLING
if [ $USE_SAVELOG = 0 ]
then
LOGFILE="$WORKINGDIRECTORY/logs/$JOBNAME-$TIMESTAMP"
fi
if [ $USE_SAVELOG = 1 ]
then
LOGFILE="$WORKINGDIRECTORY/logs/$JOBNAME"
savelog -n -c $LOG_GENERATIONS $LOGFILE
fi
#Logfile erstellen
touch $LOGFILE
f_log "----------STARTING BACKUP OF $HOST $JOBNAME ON $TIMESTAMP----------"
##QUIT IF LOCKFILE EXISTS
if [ -f $LOCKFILE ]
then
f_error "Error: Lockfile found." 3
fi
##SET VARIABLE FOR VERBOSE
VERBOSE=""
if [ $PBCVERBOSE = 1 ]
then
VERBOSE="--verbose 1"
fi
##CREATE LOCKFILE
touch $LOCKFILE
LOCKED=1
##LVM (IMAGE) HANDLING:CREATE LVM SNAPSHOT
if [ $BACKUPMETHOD = "lvm" ] || [ $BACKUPMETHOD = "lvm-image" ]
then
f_log "Creating LVM Snapshot."
lvcreate -L${SNAPSHOTSIZE} -s -n $SNAPSHOTNAME $LVPATH | tee -a $LOGFILE
if [ $PIPESTATUS -ne 0 ]
then
f_error "Error: Snapshot could not be created." 4
fi
SNAPSHOTTED=1
f_log "Finished creating LVM Snapshot"
fi
##LVM HANDLING: MOUNT SNAPSHOT
if [ $BACKUPMETHOD = "lvm" ]
then
f_log "Mounting LVM Snapshot."
mount $SNAPSHOTPATH $MOUNTPATH | tee -a $LOGFILE
if [ $PIPESTATUS -ne 0 ]
then
f_error "Error: Snapshot could not be mounted." 5
fi
MOUNTED=1
f_log "Finished mounting LVM Snapshot"
fi
##DO BACKUP
f_log "Running Backup Job."
$PBCLOCATION backup $JOBNAME.$FILETYPE:$BKUPPATH $VERBOSE --skip-lost-and-found $PBCSKIPLOSTANDFOUND --all-file-systems $PBCBACKUPALLFILESYSTEMS --keyfile $KEYFILE --backup-type host --backup-id $HOST $NS 2>&1 >/dev/null | tee -a $LOGFILE
PBCERRORLEVEL=$PIPESTATUS
if [ $PBCERRORLEVEL -ne 0 ]
then
f_error "Error: proxmox-backup-client returned $PBCERRORLEVEL." 6
fi
f_log "Finished backup job."
##CLEANUP
f_cleanup
##FINISH
if [ $WARNING = 1 ]
then
f_log "Finished pbs-linux-backup with warnings" | tee -a $LOGFILE
f_mail "warning"
fi
if [ $WARNING = 0 ]
then
f_log "Successfully finished pbs-linux-backup" | tee -a $LOGFILE
f_mail "success"
fi
rm $TIMESTAMPFILE
touch $TIMESTAMPFILE
exit 0