forked from QROkes/webinoly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bkp
177 lines (154 loc) · 5.16 KB
/
bkp
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
#!/bin/bash
bkp_local_db() {
check_for_mysql
[[ -z $wp ]] && read -p "${gre}WordPress site: ${end}" wp
[[ -z $destination ]] && read -p "${gre}Destination: ${end}" destination
if [[ -z $wp || -z $destination || ! -d $destination || $(is_wp $wp) != "true" || $(echo "${destination}" | rev | cut -c-1) == "/" ]]; then
echo "${red}[ERROR] Please, enter a valid WP site and destination path!${end}"
exit 1
fi
wp_dbdata $wp
if [[ $wp_dbhost != "localhost" ]]; then
echo "${red}[ERROR] Database host is not localhost!${end}"
exit 1
fi
local filename="webinoly-backup-db_${wp}_$(date +%F)-$(date +%T).sql"
local adminpass=$( echo $(conf_read mysql-admin) | openssl enc -d -a -salt )
sudo mysqldump --user=admin --password=$adminpass --single-transaction --lock-tables --quick --databases $wp_dbname > $destination/$filename
if [[ -s $destination/$filename ]]; then
echo "${gre}Database local backup successfully done!${end}"
else
echo "${red}[ERROR] Database backup failed!${end}"
exit 1
fi
}
bkp_s3_profile() {
if [[ ! -s /root/.aws/credentials ]]; then
echo "${red}[ERROR] AWS S3 Credentials not found!${end}"
exit 1
fi
if [[ $profile == "true" || -z $profile ]]; then
echo ""
read -p "${gre}Profile name: ${end}" profile
if [[ -z $profile ]]; then
echo "${red}[ERROR] Profile name is empty!${end}"
exit 1
fi
fi
if [[ -n $run ]]; then
if [[ -d /root/.duply/$profile ]]; then
sudo duply $profile backup_verify_purge --force --allow-source-mismatch
else
echo "${red}[ERROR] Backup profile not found!${end}"
exit 1
fi
elif [[ -n $delete ]]; then
if [[ -d /root/.duply/$profile ]]; then
sudo rm -rf /root/.duply/$profile
echo "${gre}Backup profile ${blu}'$profile'${gre} was successfully deleted!${end}"
else
echo "${red}[ERROR] Backup profile not found!${end}"
exit 1
fi
elif [[ -n $restore ]]; then
if [[ $restore == "true" ]]; then
echo ""
read -p "${gre}Restore destination folder: ${end}" restore
fi
if [[ ! -d $restore ]]; then
echo "${red}[ERROR] Restore folder not found!${end}"
exit 1
fi
sudo duply $profile restore $restore
else
if [[ -d /root/.duply/$profile ]]; then
echo "${red}[ERROR] Can not create profile${blu} '$profile' ${red}because already exists!${end}"
exit 1
fi
[[ -z $bucket ]] && read -p "${gre}S3 Bucket endpoint: ${end}" bucket
[[ -z $source ]] && read -p "${gre}Source path: ${end}" source
if [[ -z $bucket || -z $source || ! -d $source ]]; then
echo "${red}[ERROR] Please, enter a valid source and endpoint!${end}"
exit 1
fi
# Bucket sintax validation
tar_scheme=$( echo $bucket | sed 's#://#|#' | cut -d'|' -f 1 -s)
tar_url=$( echo $bucket | sed 's#://#|#' | cut -d'|' -f 2 -s)
# Fix URL in case was entered without scheme ej. 's3.amazonaws.com/bucket/folder'
if [[ -z $tar_scheme && -z $tar_url && -n $bucket ]]; then
tar_scheme="https"
tar_url=$bucket
fi
# Validation
if ! [[ $tar_scheme =~ ^(http|https|s3)$ ]]; then
echo "${red}[ERROR] Invalid target/destination!${end}"
exit 1
elif [[ $( echo $tar_url | cut -d'.' -f 1 -s) != "s3" || $(is_url $tar_url) != "true" ]]; then
echo "${red}[ERROR] Invalid S3 path!${end}"
exit 1
fi
sudo duply $profile create
[[ -z $max_age ]] && max_age="1M"
sudo sed -i -E "/^[#]?GPG_KEY=/c GPG_KEY='disabled'" /root/.duply/$profile/conf
sudo sed -i -E "/^[#]?GPG_PW=/c #GPG_PW='_GPG_PASSWORD_'" /root/.duply/$profile/conf
sudo sed -i -E "/^[#]?TARGET=/c TARGET='s3://${tar_url}'" /root/.duply/$profile/conf
sudo sed -i -E "/^[#]?SOURCE=/c SOURCE='$source'" /root/.duply/$profile/conf
sudo sed -i -E "/^[#]?MAX_AGE=/c MAX_AGE=$max_age" /root/.duply/$profile/conf
echo "${gre}Backup profile ${blu}'$profile'${gre} was successfully created!${end}"
fi
}
bkp_s3_list() {
echo ""
if [[ -d /root/.duply ]]; then
for f in /root/.duply/*
do
[[ -d $f ]] && pro=$(echo $f | cut -f 4 -d "/" -s)
[[ -f $f/conf ]] && fail="" || fail="${red}(fail)${end}"
[[ -n $raw || $list == "raw" ]] && outlist="$pro" || outlist=" ${gre}+ $pro ${end}${fail}"
if [[ -n $outlist ]]; then
echo "$outlist"
nonemptylist=true
fi
done
fi
[[ -z $nonemptylist && -z $raw && $list != "raw" ]] && echo "${blu}[Empty] No profiles were found!${end}"
echo ""
}
bkp_wizard() {
echo "${gre}"
echo " ***********************************"
echo " ************ Backups ************"
echo " ***********************************"
echo "${blu}"
echo " 1 - Add AWS S3 Credentials"
echo " 2 - AWS S3 directory backup"
echo " 3 - WordPress Database local backup"
echo " 4 - Restore backup from S3"
echo " 5 - Run S3 backup"
echo " 6 - Delete profile"
echo " 7 - List profiles"
echo "${gre}"
read -p "What do you want to do? ${end}" wzd
echo ""
if [[ $wzd == 1 ]]; then
webinoly -aws-s3-credentials
elif [[ $wzd == 2 ]]; then
bkp_s3_profile
elif [[ $wzd == 3 ]]; then
bkp_local_db
elif [[ $wzd == 4 ]]; then
restore="true"
bkp_s3_profile
elif [[ $wzd == 5 ]]; then
run="true"
bkp_s3_profile
elif [[ $wzd == 6 ]]; then
delete="true"
bkp_s3_profile
elif [[ $wzd == 7 ]]; then
bkp_s3_list
else
echo "${red}[ERROR] Please, enter a valid option!${end}"
exit 1
fi
}