-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_restorer.sh
157 lines (130 loc) · 4.96 KB
/
db_restorer.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
#!/bin/bash
PATH=$PATH:/usr/local/xtrabackup/bin
#Restoring mysqldb
#Martin Podhola 12062018
DATE=`date +\%Y-\%m-\%d`
DATE2=`date +\%Y-\%m-\%d-\%H-\%M`
TMPDB=/backups/tmp
LOG=/var/log/partial_restore.log
CAS=`date`
#RESTOREFROM=$1
#RESTOREDAY=$2
#RESTOREHOUR=$3
#cleaning up tmp
help () {
printf "Skript na restore databaze k danemu datu hodine. \n Skript provede restore podle pozadavku zastaveni a zpusteni vfxrestorer mysql! \n Spoustet skript se musi s parametry: \n -f databaze z ktere budete restorovat \"vfxdb01 nebo vfxdb02\" \n -d den ze kdy se bude restorovat format: YY-MM-DD \n -g hodina do ktere se bude restore provadet 1-23 \n"
}
sep()
{
echo "*********************" | tee > $LOG
}
testrv()
{
if [ "$RV" == "0" ]; then echo "OK."| tee >> $LOG; else echo "PROBLEM!"| tee >> $LOG; exit; fi
}
cleanup () {
sep
echo "$CAS" | tee >> $LOG
echo "Clean up restore temp directory" | tee >> $LOG
A=`ls $TMPDB |wc -l`
if [ $A -gt 0 ]; then
rm -rf $TMPDB/* > /dev/null 2>&1; RV=$?; testrv
fi
}
#copy actual backup to tmp
copy_backup () {
echo "Cloning backup for partial restore" | tee >> $LOG
cp -rf /backups/$RESTOREFROM/${DATE}/FULL $TMPDB && echo "kopirovani fullbackupu OK!">> $LOG #&& cp -rf /backups/$RESTOREFROM/${DATE}/inc{1..$RESTOREHOUR} $TMPDB
echo "ttttt" >> $LOG
for i in $(seq 1 $RESTOREHOUR); do
echo "$i" >> $LOG
cp -rf /backups/$RESTOREFROM/${DATE}/inc$i $TMPDB ; echo "kopirovani inc$i bylo dokonceno" >> $LOG
done
}
restore()
{
echo `date '+%Y-%m-%d %H:%M:%S:%s'`": Decompressing the FULL backup" | tee >> $LOG
xtrabackup --decompress --remove-original --parallel=4 --socket=/var/lib/mysql/mysql.sock --target-dir=$TMPDB/FULL
echo `date '+%Y-%m-%d %H:%M:%S:%s'`": Decompressing Done !!!"| tee >> $LOG
echo `date '+%Y-%m-%d %H:%M:%S:%s'`": Prepareing FULL Backup ..."| tee >> $LOG
xtrabackup --prepare --apply-log-only --socket=/var/lib/mysql/mysql.sock --target-dir=$TMPDB/FULL
echo `date '+%Y-%m-%d %H:%M:%S:%s'`": FULL Backup Preparation Done!!!"| tee >> $LOG
P=1
while [ -d $TMPDB/inc$P ] && [ -d $TMPDB/inc$(($P+1)) ]
do
echo `date '+%Y-%m-%d %H:%M:%S:%s'`": Decompressing incremental:$P"| tee >> $LOG
xtrabackup --decompress --remove-original --parallel=4 --socket=/var/lib/mysql/mysql.sock --target-dir=$TMPDB/inc$P
echo `date '+%Y-%m-%d %H:%M:%S:%s'`": Decompressing incremental:$P Done !!!"| tee >> $LOG
echo `date '+%Y-%m-%d %H:%M:%S:%s'`": Prepareing incremental:$P" |tee >> $LOG
xtrabackup --prepare --apply-log-only --socket=/var/lib/mysql/mysql.sock --target-dir=$TMPDB/FULL --incremental-dir=$TMPDB/inc$P
echo `date '+%Y-%m-%d %H:%M:%S:%s'`": incremental:$P Preparation Done!!!"|tee >> $LOG
P=$(($P+1))
done
if [ -d $BACKUP_DIR/inc$P ]
then
echo `date '+%Y-%m-%d %H:%M:%S:%s'`": Decompressing the last incremental:$P"| tee >> $LOG
xtrabackup --decompress --remove-original --parallel=4 --socket=/var/lib/mysql/mysql.sock --target-dir=$TMPDB/inc$P
echo `date '+%Y-%m-%d %H:%M:%S:%s'`": Decompressing the last incremental:$P Done !!!"| tee >> $LOG
echo `date '+%Y-%m-%d %H:%M:%S:%s'`": Prepareing the last incremental:$P" | tee >> $LOG
xtrabackup --prepare --target-dir=$BACKUP_DIR/FULL --socket=/var/lib/mysql/mysql.sock --incremental-dir=$TMPDB/inc$P
echo `date '+%Y-%m-%d %H:%M:%S:%s'`": Last incremental:$P Preparation Done!!!"|tee >> $LOG
fi
}
copyfiles (){
#for a in $(seq 1 1 10);do
if systemctl stop mysqld ; then
#systemctl stop mysqld
sep;
echo "mysql is down backuping old files" | tee >> $LOG
tar -czvf /root/backupmysql_$DATE2.tar.gz /var/lib/mysql > /dev/null 2>&1; RV=$?; testrv
echo "copy backup to db"|tee >> $LOG
cp -rf $TMPDB/FULL/ib* /var/lib/mysql/ && cp -rf $TMPDB/FULL/vfx /var/lib/mysql/ && cp -rf $TMPDB/FULL/fm_import /var/lib/mysql/ && cp -rf $TMPDB/FULL/redmine /var/lib/mysql/ && cp -rf $TMPDB/FULL/openfire /var/lib/mysql/
chown -R mysql:mysql /var/lib/mysql
echo "startdb" |tee >> $LOG
systemctl start mysqld > /dev/null 2>&1; RV=$?; testrv
else
echo "DB se nepovedlo zastavit pro to konec!!" |tee >> $LOG
exit 1
fi
#done
}
while getopts ":vhf:d:g:" optname
do
case "$optname" in
"v")
version
exit 0;
;;
"h")
help
exit 0;
;;
"?")
echo "Unknown option $OPTARG"
exit 0;
;;
":")
echo "No argument value for option $OPTARG"
exit 0;
;;
"f")
RESTOREFROM=$OPTARG
;;
"d")
RESTOREDAY=$OPTARG
;;
"g")
RESTOREHOUR=$OPTARG
;;
*)
echo "Nezname parmetry prosim opakujte znova -h"
exit 0;
;;
esac
done
shift $(($OPTIND - 1))
cleanup
copy_backup
restore
copyfiles
exit 0