-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdruml-remote-ac-restorefrombackup.sh
executable file
·109 lines (88 loc) · 2.67 KB
/
druml-remote-ac-restorefrombackup.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
#!/bin/bash
# Get Druml dir.
SCRIPT_DIR=$1
shift
# Load includes.
source $SCRIPT_DIR/druml-inc-init.sh
# Display help.
if [[ ${#ARG[@]} -lt 1 || -z $PARAM_SITE || -n $PARAM_HELP ]]
then
echo "usage: druml remote-ac-restorefrombackup [--config=<path>] [--docroot=<path>]"
echo " [--jobs=<number>] [--delay=<seconds>]"
echo " --site=<subsite> | --list=<list>"
echo " [--type=ondemand|daily]"
echo " [--server=<number>]"
echo " <environment>"
exit 1
fi
# Read parameters.
SUBSITE=$(get_site_alias $PARAM_SITE)
ENV=$(get_environment ${ARG[1]})
# Set variables.
DRUSH=$(get_drush_command)
DRUSH_ALIAS=$(get_drush_alias $ENV)
SSH_ARGS=$(get_ssh_args $ENV $PARAM_SERVER)
DRUSH_SUBSITE_ARGS=$(get_drush_subsite_args $SUBSITE)
PROXY_PARAM_SERVER=$(get_param_proxy "server")
FILTER_TYPE=$PARAM_TYPE
# Say Hello.
echo "=== Restore '$SUBSITE' DB from backup on $ENV"
echo ""
if [[ $FILTER_TYPE != "" && $FILTER_TYPE != "ondemand" && $FILTER_TYPE != "daily" ]]; then
echo "FILTER_TYPE parameter is not valid!"
exit 1
fi
OUTPUT=$(ssh -Tn $SSH_ARGS "$DRUSH $DRUSH_ALIAS $DRUSH_SUBSITE_ARGS ac-database-instance-backup-list $SUBSITE" 2>&1)
RESULT="$?"
TASK=$(echo $OUTPUT | awk '{print $2}')
# Eixt upon an error.
if [[ $RESULT > 0 ]]; then
echo "Error getting list of backups."
exit 1
fi
ID=""
TYPE=""
STARTED=""
LATEST_ID=""
LATEST_STARTED=""
while read -r LINE; do
KEY=$(echo $LINE | awk '{print $1}')
VAL=$(echo $LINE | awk '{print $3}')
if [[ "$KEY" = "id" ]]; then
ID=$VAL
fi
if [[ "$KEY" = "type" ]]; then
TYPE=$VAL
fi
if [[ "$KEY" = "started" ]]; then
STARTED=$VAL
if [[ $STARTED -ge $LATEST_STARTED ]]; then
if [[ $FILTER_TYPE == "" || $FILTER_TYPE == $TYPE ]]; then
LATEST_STARTED=$STARTED
LATEST_ID=$ID
LATEST_TYPE=$TYPE
fi
fi
fi
done <<< "$OUTPUT"
if [[ $LATEST_ID == "" ]]; then
echo "Backup has not been found!"
exit 1
fi
echo "Restroting from $LATEST_TYPE DB backup, ID=$LATEST_ID, STARTED=$LATEST_STARTED."
R_OUTPUT=$(ssh -Tn $SSH_ARGS "$DRUSH $DRUSH_ALIAS ac-database-instance-backup-restore $SUBSITE $LATEST_ID" 2>&1)
R_RESULT="$?"
TASK=$(echo $R_OUTPUT | awk '{print $2}')
# Eixt upon an error.
if [[ $? > 0 ]]; then
echo "Error removing backup."
fi
echo "$R_OUTPUT"
echo "Restoring from backup has been scheduled."
# Check task status.
run_script remote-ac-status $PROXY_PARAM_SERVER $ENV $TASK
if [[ $? > 0 ]]; then
echo "Restoring from backup failed!"
exit 1
fi
echo "Restoring from backup completed!"