/
v-import-cwp
302 lines (272 loc) · 10.8 KB
/
v-import-cwp
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/bin/bash
# Import Backup From Centos Web Panel
# TODO:
# Restore CRONs and some tests
VERSION=0.2-BETA
if [[ $1 == version ]] || [[ $1 == --version ]]; then
printf "Version: %s\n" "$VERSION"
exit
fi
# shellcheck source=/usr/local/hestia/func/main.sh
source $HESTIA/func/main.sh
# shellcheck source=/etc/hestiacp/hestia.conf
source /etc/hestiacp/hestia.conf
# load config file
source_conf "$HESTIA/conf/hestia.conf"
GREEN=$(tput setaf 2)
RED=$(tput setaf 1)
YELLOW=$(tput setaf 3)
COLOROFF=$(tput sgr0)
if [ $# -lt 1 ]; then
echo "usage: bash $0 cpanel-backup.tar.gz"
echo "or"
echo "usage: bash $0 cpanel-backup.tar.gz ALL # Default opción is ALL can be omited "
echo "usage: bash $0 cpanel-backup.tar.gz MAIL DB DOMAIN CRON MX"
exit 1
fi
if [ ! -e /usr/bin/rsync ] || [ ! -e /usr/bin/file ]; then
printf "%sWARNING:%s rsync not installed, try install it\n" "$YELLOW" "$COLOROFF"
printf "%sWARNING:%s This script need: rsync, file" "$YELLOW" "$COLOROFF"
printf "%sINFO:%s Run: apt-get install rsync file" "$GREEN" "$COLOROFF"
exit 3
fi
if [ -f "$1" ]; then
CWP_BACKUP="$1"
fi
if [ -z "$BACKUP_TEMP" ]; then
BACKUP_TEMP=$BACKUP
else
printf "%sWARNING:%s File does not exists\n" "$YELLOW" "$COLOROFF"
exit 1
fi
# Lets check if have space in home to restore all
BACKUP_SIZE=$(stat -c %s "$CWP_BACKUP")
NEED_SPACE=$((BACKUP_SIZE * 2 + BACKUP_SIZE / 2))
HOME_SPACE=$(df -B1 /home | awk 'NR==2 {print $4}')
if [[ "$HOME_SPACE" -ge "$NEED_SPACE" ]]; then
printf "%sINFO:%s Detected sufficient space to restore the backup.\n" "$GREEN" "$COLOROFF"
else
printf "%sERROR:%s Dtected insufficient free space to import the backup, need %s bytes have %s bytes in home\n" "$RED" "$COLOROFF" "$NEED_SPACE" "$HOME_SPACE"
exit 4
fi
# Creating temporary directory
TMPDIR=$(mktemp -p "$BACKUP_TEMP" -d)
printf "%sINFO:%s TMPDIR is %s\n" "$GREEN" "$COLOROFF" "$TMPDIR"
cleanup_and_exit() {
EXIT_CODE=$1
printf "%sINFO:%s Exit and clean %s\n" "$GREEN" "$COLOROFF" "$TMPDIR"
if [ -d "$TMPDIR" ]; then
rm -rf "$TMPDIR"
fi
exit "$EXIT_CODE"
}
trap 'cleanup_and_exit $?' EXIT
printf "%sINFO:%s Checking provided file...\n" "$GREEN" "$COLOROFF"
if ! file "$CWP_BACKUP" | grep -q -c "gzip compressed data,"; then
printf "%sError 3 not-gzip - no stantard cpanel backup provided of file not installed ( Try yum install file, or apt-get install file )%s\n" "$RED" "$COLOROFF"
exit 3
fi
printf "%sINFO:%s OK - Gzipped File Detected, Initiating Extraction:\n" "$GREEN" "$COLOROFF"
pv "$CWP_BACKUP" | tar xz -C "$TMPDIR"
if [[ $? -eq 0 ]]; then
printf "%sINFO:%s Backup extracted without errors...\n" "$GREEN" "$COLOROFF"
else
printf "%sERROR!! %s on backup extraction, check your file, try extract it manually\n" "$RED" "$COLOROFF"
printf "%sINFO:%s Removing %s\n" "$GREEN" "$COLOROFF" "$TMPDIR"
exit 1
fi
cd $TMPDIR
MAIN_DIR=$(pwd)
printf "%sINFO:%s Access tmp directory, working DIR is %s\n" "$GREEN" "$COLOROFF" "$MAIN_DIR"
sed -i 's/\\//g' conf.json contentBackup.json domains.json subdomains.json
CONF_JSON=$(cat conf.json)
MAIN_DOMAIN=$(echo $CONF_JSON | jq -r '.domain')
MYSQL_DATABASES_JSON=CHECK
if [[ ! -z contentBackup.json ]]; then
MYSQL_DATABASES_JSON=$(cat contentBackup.json)
MYSQL_DATABASES_JSON=$(echo $MYSQL_DATABASES_JSON | jq -r '.mysql[]')
fi
if [[ $MYSQL_DATABASES_JSON != CHECK ]]; then
NEW_USER=$(echo $MYSQL_DATABASES_JSON | tr '_' ' ' | awk '{ print $1 }')
else
NEW_USER=$2
fi
printf "%sINFO:%s Get User: %s\n" "$GREEN" "$COLOROFF" "$NEW_USER"
CHECK_SYSUSER=$(cut -f 1 -d : /etc/passwd | grep "^$NEW_USER$")
if [ -n "$CHECK_SYSUSER" ] || [ -e "$HESTIA/data/users/$NEW_USER" ]; then
if [[ $NO_CHECK_USER == ON ]]; then
printf "%sWarning:%s %s Alredy exists in this server, but you select import backup in any way.\n" "$YELLOW" "$COLOROFF" "$NEW_USER"
NO_CREATE_USER=yes
else
check_result "$E_EXISTS" "user $user exists"
printf "%sERROR:%s User allready exists\n" "$RED" "$COLOROFF"
exit 2
fi
fi
TMP_PASSWD=$(generate_password)
EMAIL=$(echo $CONF_JSON | jq -r '.email')
if [ -z "$EMAIL" ]; then
# Hestia does not like email to be set to an empty string
EMAIL="info@"$(hostname)
fi
$BIN/v-add-user "$NEW_USER" "$TMP_PASSWD" "$EMAIL" "default"
if [ "$?" -ne 0 ]; then
printf "\n%sERROR%s: Unable to create user" "$GREEN" "$COLOROFF"
exit 1
fi
# Restore user password
printf "\n%sINFO%s: Importing user password.\n" "$GREEN" "$COLOROFF"
update_user_value "$NEW_USER" 'MD5' "$(cat passwd)"
$BIN/v-rebuild-user "$NEW_USER"
is_there() {
unset IS_THERE
OBJECT=$1
TYPE=$2
USER=$3
if [[ $TYPE == DB ]]; then
if $BIN/v-list-databases $USER | grep -w -q $OBJECT ; then
IS_THERE=yes
fi
elif [[ $TYPE == DOMAIN ]]; then
if $BIN/v-list-web-domains $USER | grep -w -q "$OBJECT" ; then
IS_THERE=yes
fi
elif [[ $TYPE == MAIL ]]; then
if $BIN/v-list-mail-accounts $USER | grep -w -q "$OBJECT" ; then
IS_THERE=yes
fi
fi
}
restore_databases(){
# Restore databases
mysql -e "SET GLOBAL max_allowed_packet=1073741824;"
sed -i 's/\\//g' mysql/user_grants.sql
sed -i "s/\`/'/g" mysql/user_grants.sql
printf "\n%sINFO%s: Start with Databases\n" "$GREEN" "$COLOROFF"
## User / Password
grep "GRANT USAGE ON" mysql/user_grants.sql | awk -F "'" '{ print $2, $6 }' | uniq > user_password_db
# User and database
grep "GRANT" mysql/user_grants.sql | grep -v "USAGE ON" > u_db
cat u_db | awk -F "'" '{ print $2, $4 }' | sort | uniq > uni_u_db
# Fix mysql 8 to mariadb problems here:
sed -i "s/utf8mb4_0900_ai_ci/utf8mb4_unicode_ci/g" mysql/*
mysql -e "SHOW DATABASES" > server_dbs
for db in $MYSQL_DATABASES_JSON; do
grep -w $db server_dbs
if [ $? == "1" ]; then
printf "%sINFO%s: Create and import %s\n" "$GREEN" "$COLOROFF" "${db}"
mysql -e "CREATE DATABASE $db"
mysql ${db} < mysql/${db}.sql
else
printf "%sERROR%s: Cant import database %s alredy exists in mysql server\n" "$RED" "$COLOROFF" $db
fi
done
time=$(echo "$time_n_date" | cut -f 1 -d \ )
date=$(echo "$time_n_date" | cut -f 2 -d \ )
cat uni_u_db | while read db userdb; do
grep -w $userdb user_password_db | while read user end_user_pass; do
if [ "$userdb" == "$user" ] && [ "$userdb" != "$NEW_USER" ]; then
printf "DB='%s' DBUSER='%s' MD5='%s' HOST='localhost' TYPE='mysql' CHARSET='UTF8' U_DISK='0' SUSPENDED='no' TIME='%s' DATE='%s'\n" \
"$db" "$userdb" "$end_user_pass" "$time" "$data" >> /usr/local/hestia/data/users/"$NEW_USER"/db.conf
fi
done
done
# Leave hestia restore passwords and create users
printf "%sINFO:%s Rebuild databases files for %s\n" "$GREEN" "$COLOROFF" "$NEW_USER"
$BIN/v-rebuild-databases $NEW_USER
## end mysql
}
restore_databases
domain_import() {
DOMAIN=$1
BACKUP_PATH=$2
is_there "$DOMAIN" "DOMAIN" "$NEW_USER"
if [[ IS_THERE != yes ]]; then
printf "%sINFO:%s Importing domain: %s \n" "$GREEN" "$COLOROFF" "$DOMAIN"
$BIN/v-add-domain "$NEW_USER" "$DOMAIN"
rm -f /home/$NEW_USER/web/$DOMAIN/public_html/index.html
rm -f /home/$NEW_USER/web/$DOMAIN/public_html/robots.txt
SYNC_COUNT=0
rsync -av $BACKUP_PATH /home/$NEW_USER/web/$DOMAIN/public_html 2>&1 \
| while read file_dm; do
SYNC_COUNT=$(($SYNC_COUNT + 1))
printf "\r%sINFO:%s Importing -- %s files" "$GREEN" "$COLOROFF" "$SYNC_COUNT "
done
printf "\n"
chown $NEW_USER:$NEW_USER -R /home/$NEW_USER/web/$DOMAIN/public_html
chown $NEW_USER:www-data /home/$NEW_USER/web/$DOMAIN/public_html
chmod 751 /home/$NEW_USER/web/$DOMAIN/public_html
else
printf "%sWARNING:%s Domain %s already added, skip..\n" "$YELLOW" "$COLOROFF" "$CP_DOMAIN"
fi
}
start_domain() {
printf "\n%sINFO:%s Start Importing Domains\n" "$GREEN" "$COLOROFF"
MAIN_DOMAIN_JSON=$(cat conf.json)
MAIN_DOMAIN=$(echo $MAIN_DOMAIN_JSON| jq -r '.domain')
domain_import $MAIN_DOMAIN "home/public_html"
if [[ ! -z domains.json ]]; then
ADD_ON_DOMAIN_JSON=$(cat domains.json)
for JSON_OB in $(echo $ADD_ON_DOMAIN_JSON | jq -c '.[]'); do
DOMAIN=$(echo $JSON_OB | jq -r '.domain')
DOM_PATH=$(echo $JSON_OB | jq -r '.domain_path')
DOM_PATH=$(echo $DOM_PATH | sed -e "s#/home/$NEW_USER/##" )
domain_import "$DOMAIN" "home/${DOM_PATH}"
done
fi
if [[ ! -z subdomains.json ]]; then
SUB_DOMAIN_JSON=$(cat subdomains.json)
for JSON_OB in $(echo $SUB_DOMAIN_JSON | jq -c '.[]'); do
SUB_DOMAIN=$(echo $JSON_OB | jq -r '.subdomain')
DOMAIN=$(echo $JSON_OB | jq -r '.domain')
DOM_PATH=$(echo $JSON_OB | jq -r '.path')
DOM_PATH=$(echo $DOM_PATH | sed -e "s#/home/$NEW_USER/##" )
domain_import "${SUB_DOMAIN}.${DOMAIN}" "home/${DOM_PATH}"
done
fi
}
start_domain
quota_convert() {
BYTES=$1
if [[ $BYTES == 0 ]] || [[ $BYTES == -1 ]] || [[ $BYTES == unlimited ]]; then
QUOTA=unlimited
else
QUOTA=$(echo "scale=0; $BYTES / 1024 / 1024" | bc)
fi
}
restore_mail() {
sed -i '/^$/d' postfix/mailbox.sql
printf "\n%sINFO:%s Start Importing Mails\n" "$GREEN" "$COLOROFF"
cat postfix/mailbox.sql | tr '()""' ' ' | awk '{ print $8 , $11 , $13 , $15 , $17}' | while read -r PASSWORD MAIL_PATH QUOTA MAIL_ACCOUNT MAIL_DOMAIN
do
printf "%sINFO:%s Importing mails for domain: %s \n" "$GREEN" "$COLOROFF" $MAIL_DOMAIN
MAIL_ACCOUNT_COUNT=$(find . -maxdepth 1 -mindepth 1 -type d \( ! -name cur ! -name new ! -name tmp \) | wc -l)
printf "%sINFO:%s Import mail account: %s@%s\n" "$GREEN" "$COLOROFF" "$MAIL_ACCOUNT" "$MAIL_DOMAIN"
# Doesn't really matter but we don't know the unhashed one
TMP_PASSWD=$(generate_password)
quota_convert $QUOTA
$BIN/v-add-mail-account $NEW_USER $MAIL_DOMAIN $MAIL_ACCOUNT $TMP_PASSWD $QUOTA
COUNT="0"
if [[ -d "vmail/${MAIL_PATH}" ]]; then
rsync -av vmail/${MAIL_PATH} /home/$NEW_USER/mail/${MAIL_DOMAIN}/${MAIL_ACCOUNT} | while read line
do
((COUNT++))
printf "\r%sINFO:%s Importing: %s mails in %s@%s" "$GREEN" "$COLOROFF" "$COUNT" "$MAIL_ACCOUNT" "$MAIL_DOMAIN"
done
printf "\n"
else
printf "%sWARN%s: Account %s@%s exist but no have email\n" "$YELLOW" "$COLOROFF" "$MAIL_ACCOUNT" "$MAIL_DOMAIN"
fi
find /home/$NEW_USER/mail/$MAIL_DOMAIN/ -type f -exec chmod 660 {} \;
find /home/$NEW_USER/mail/$MAIL_DOMAIN/ -type d -exec chmod 700 {} \;
chown -R $NEW_USER:mail /home/$NEW_USER/mail/$MAIL_DOMAIN/
find /home/$NEW_USER/mail/$FOLDER -type f -name 'dovecot*' -delete
NEW_LINE2="ACCOUNT='${MAIL_ACCOUNT}' ALIAS='' AUTOREPLY='no' FWD='' FWD_ONLY='' MD5='$PASSWORD' QUOTA='$QUOTA' U_DISK='0' SUSPENDED='no' TIME='$time' DATE='$date'"
escaped=$(printf '%s\n' "$NEW_LINE" | sed -e 's/[\/&]/\\&/g')
escaped2=$(printf '%s\n' "$NEW_LINE2" | sed -e 's/[\/&]/\\&/g')
sed -i "s/^${MAIL_ACCOUNT}:.*/$escaped/g" /home/${NEW_USER}/conf/mail/${MAIL_DOMAIN}/passwd
sed -i "s/^ACCOUNT='${MAIL_ACCOUNT}.*/$escaped2/g" /usr/local/hestia/data/users/${NEW_USER}/mail/${MAIL_DOMAIN}.conf
$BIN/v-rebuild-mail-domain $NEW_USER $MAIL_DOMAIN
done
}
restore_mail