-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprepare.bash
94 lines (74 loc) · 3.9 KB
/
prepare.bash
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
#!/bin/bash
#Only to be run if an incremental backup has been taken, if no incremental then prepare backup manually
#To run do
#bash prepare.bash /path/to/backup/directory
#include --export for single table restores ($2 variable)
#script will then look for fullbackup folder and loop through the incremental backups in order
#do not run if no incremental backups have been taken, run prepare manually
# Set variables for the full backup folder and the incremental backup folder
backupdir="$1"
exportoption="$2"
FULL_BACKUP_DIR=$backupdir/fullbackup/
restartfulldir=$FULL_BACKUP_DIR/*
INCREMENTAL_BACKUP_DIR=$backupdir/incr/*
incrementaldir=$backupdir/incr/
preparelog=$backupdir/prepare.log
#reset backup directory if prepare script has been ran before
full_backup_file=$backupdir/full_backup.gz
echo $full_backup_file
if [ -f $full_backup_file ]; then
echo "$(date +'%Y-%m-%d %H:%M:%S') Prepare script has been ran before. resetting directory for next prepare run"
echo "$(date +'%Y-%m-%d %H:%M:%S') Emptying the $FULL_BACKUP_DIR directory"
rm -rf $restartfulldir
echo "$(date +'%Y-%m-%d %H:%M:%S') Moving full backup zip file back to $FULL_BACKUP_DIR"
mv $backupdir/full_backup.gz $FULL_BACKUP_DIR
echo "$(date +'%Y-%m-%d %H:%M:%S') Archiving prepare file"
archivelog=$backupdir/old-prepare-$(date +'%H:%M:%S').log
mv $preparelog $archivelog
echo "$(date +'%Y-%m-%d %H:%M:%S') Directory reset, starting prepare process as normal"
else
echo "$(date +'%Y-%m-%d %H:%M:%S') First time running prepare. running process as normal"
fi
# Change directory, unzip file and prepare fullbackup
cd $FULL_BACKUP_DIR
unpigz -c $FULL_BACKUP_DIR/* | mbstream -x
mariabackup --prepare --target-dir=$FULL_BACKUP_DIR 2>> $preparelog
mv $FULL_BACKUP_DIR/full_backup.gz ..
# Loop through incremental backup folders, unzip and apply them to the full backup
for DIR in $INCREMENTAL_BACKUP_DIR
do
checkstatus=$(tail -n 2 $preparelog | grep -c "completed OK")
if [[ $checkstatus -eq 1 ]]; then
cd $DIR
gunzip -c $DIR/* | mbstream -x
echo "$(date +'%Y-%m-%d %H:%M:%S') Applying $DIR incremental updates to fullbackup"
mariabackup --prepare --target-dir=$FULL_BACKUP_DIR --incremental-dir=$DIR 2>> $preparelog
else
echo "$(date +'%Y-%m-%d %H:%M:%S') Last incremental failed to run, please check $preparelog for more details"
echo "$(date +'%Y-%m-%d %H:%M:%S') Check incremental folder for compressed file. Backup might be corrpted, prepare to last good incremental" >> $preparelog
fi
done
#delete incrmental uncompressed files after they are appiled to fullbackup to save space
for DIR in $INCREMENTAL_BACKUP_DIR
do
cd $DIR
find $DIR/* ! -name 'incremental.backup.gz' | xargs rm -rf
echo "$(date +'%Y-%m-%d %H:%M:%S') Deleted uncompressed files for incremental $DIR" >> $preparelog
echo "$(date +'%Y-%m-%d %H:%M:%S') Deleting uncompressed $DIR leaveing zipped file alone"
done
lastcheckstatus=$(grep -c "completed OK" $preparelog)
incbackups=$(find $incrementaldir -mindepth 1 -maxdepth 1 -type d | wc -l)
includefull=$(($incbackups + 1))
#if number of prepared backups = number of backups process was succesfully
if [[ $lastcheckstatus -eq $includefull ]]; then
echo "$(date +'%Y-%m-%d %H:%M:%S') Prepare completed successfully and ready to restore from backup directory $FULL_BACKUP_DIR"
echo "Restore with either command:"
echo "mariabackup --copy-back --target-dir=$FULL_BACKUP_DIR"
echo "mariabackup --move-back --target-dir=$FULL_BACKUP_DIR"
else
echo "$(date +'%Y-%m-%d %H:%M:%S') Prepare failed, check $preparelog for more details. number of backups did not equal number of prepared backups"
fi
if [[ $2 == "--export" ]]; then
echo "$(date +'%Y-%m-%d %H:%M:%S') Export option select, preparing the full backup with .cfg files for tablespace import"
mariabackup --prepare --export --target-dir=$FULL_BACKUP_DIR 2>> $preparelog
fi