This repository has been archived by the owner on Mar 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
restore.sh
executable file
·108 lines (96 loc) · 3.32 KB
/
restore.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
#!/bin/bash
# shellcheck disable=2029
# shellcheck disable=2069
if ! test -f ~/vars.sh; then
echo "ERROR, secrets missing !"
exit 1
fi
chmod +x ~/vars.sh
source ~/vars.sh
# Do not edit below
CURRENT_DATE=$(date +%s)
LOG_FILE="/tmp/restore_${CURRENT_DATE}.log"
clear
exec 2>&1 &> >(tee "${LOG_FILE}")
echo "$LOG_FILE"
restore_db ()
{
echo "I will now restore database."
sql_file=$1.sql
sql_file=$(basename "$sql_file")
sql_location=$BACKUP_LOCATION/$sql_file
echo "$sql_location"
echo "$sql_file"
echo "Drop/Create database"
ssh "$SSH_DETAILS" "mysql -u $SQL_USER -p${SQL_PASSWORD} $SQL_DB <<< 'DROP DATABASE $SQL_DB;'"
ssh "$SSH_DETAILS" "mysql -u $SQL_USER -p${SQL_PASSWORD} <<< 'CREATE DATABASE $SQL_DB;'"
echo "Upload $sql_location..."
scp "$sql_location" "$SSH_DETAILS:~"
echo "Restauration of $sql_file into $SQL_DB ..."
ssh "$SSH_DETAILS" "mysql -u $SQL_USER -p${SQL_PASSWORD} $SQL_DB < ~/$sql_file"
echo "Delete $sql_file"
ssh "$SSH_DETAILS" "rm $sql_file"
echo "Database restaured !"
}
while :
do
echo "Which one to restore ? (ctrl-c to quit)"
iterator=0
for file in ${BACKUP_LOCATION}/*; do
if [[ $file =~ ^.*\.sql$ ]]; then
continue
fi
iterator=$((iterator + 1))
echo -n "${iterator} - "
if test -d "$file"; then
echo "Restore last incremental backup"
else
filename=$(basename "$file")
format_date=$(date -d @"${filename%%.*}")
echo "$format_date"
fi
done
iterator_max=$iterator
echo -n "Your choice : "
read -r user_choice
if [[ ! $user_choice =~ ^[0-9]+$ ]]; then
echo "Not a number. Try again."
continue
fi
if [[ "$user_choice" -lt 1 || "$user_choice" -gt "$iterator_max" ]]; then
echo "Not a valid choice. Try again."
else
break
fi
done
iterator=0
for file in ${BACKUP_LOCATION}/*; do
if [[ $file =~ ^.*\.sql$ ]]; then
continue
fi
iterator=$((iterator + 1))
if [[ $user_choice -eq $iterator ]]; then
restauration_file=$file
fi
done
ssh "$SSH_DETAILS" sudo chmod -R 2770 "$RESTAURATION_LOCATION"
ssh "$SSH_DETAILS" rm -rf "$RESTAURATION_LOCATION && mkdir -p $RESTAURATION_LOCATION"
if test -f "$restauration_file"; then
filename=$(basename "$restauration_file")
echo "I will restore $restauration_file"
scp "$restauration_file" "${SSH_DETAILS}:${RESTAURATION_LOCATION}/"
ssh "$SSH_DETAILS" "tar xf ${RESTAURATION_LOCATION}/$filename -C ${RESTAURATION_LOCATION} && rm -f ${RESTAURATION_LOCATION}/$filename"
echo "Files restaured !"
ssh "$SSH_DETAILS" sudo chown -R www-data "$RESTAURATION_LOCATION"
ssh "$SSH_DETAILS" sudo chmod -R 2770 "$RESTAURATION_LOCATION"
restore_db "${restauration_file%%.*}"
else
echo "I will restore last incremental backup : $file"
scp -r "${restauration_file}"/* "${SSH_DETAILS}:${RESTAURATION_LOCATION}*"
ssh "$SSH_DETAILS" sudo chown -R www-data "$RESTAURATION_LOCATION"
ssh "$SSH_DETAILS" sudo chmod -R 2770 "$RESTAURATION_LOCATION"
mysqldump -u "$SQL_LOCAL_USER" -p"$SQL_LOCAL_PASSWORD" "$SQL_LOCAL_DB" --databases --single-transaction > "$BACKUP_LOCATION/restore.sql"
restore_db "$BACKUP_LOCATION/restore"
rm "$BACKUP_LOCATION/restore.sql"
fi
echo "Restauration successful !"