forked from hestiacp/hestiacp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: DirectAdmin import (hestiacp#4177)
* Created v-directadmin-import script to import directadmin backups in HestiaCP * Fix linting --------- Co-authored-by: Ruben <[email protected]> Co-authored-by: Jaap Marcus <[email protected]>
- Loading branch information
1 parent
1990f2d
commit 7bda00d
Showing
2 changed files
with
330 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,306 @@ | ||
#!/bin/bash | ||
# info: Import DirectAdmin backup to a new user | ||
# | ||
# example: v-import-directadmin /backup/backup.tar.gz | ||
# | ||
# Based on sk-da-importer | ||
# Credits: Maks Usmanov (skamasle), Jaap Marcus (jaapmarcus) and contributors: | ||
# Thanks to <https://github.com/Skamasle/sk_da_importer/graphs/contributors> | ||
|
||
# This script is provided whitout any warranty | ||
# Run at your own risk | ||
# Version 0.1 | ||
# This script restore backups from DA to Hestiacp | ||
|
||
# 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" | ||
|
||
if [ ! -e /usr/bin/rsync ] || [ ! -e /usr/bin/file ]; then | ||
echo "#######################################" | ||
echo "rsync not installed, try install it" | ||
echo "This script need: rsync, file" | ||
echo "#######################################" | ||
if [ -e /etc/redhat-release ]; then | ||
echo "Run: yum install rync file" | ||
else | ||
echo "Run: apt-get install rsync file" | ||
fi | ||
exit 3 | ||
fi | ||
# Put this to 0 if you want use bash -x to debug it | ||
debug=1 | ||
hestia_package=default | ||
tmp_dir='tmp_dir_da_backup' | ||
time=$(echo "$time_n_date" | cut -f 1 -d \ ) | ||
date=$(echo "$time_n_date" | cut -f 2 -d \ ) | ||
|
||
if [ -f "$1" ]; then | ||
backup_file="$1" | ||
fi | ||
|
||
if [ -z "$BACKUP_TEMP" ]; then | ||
BACKUP_TEMP=$BACKUP | ||
else | ||
echo "File does not exists" | ||
exit 1 | ||
fi | ||
|
||
delete_tmp() { | ||
echo "Removing tmp files" | ||
rm -rf /backup/${tmp_dir} | ||
} | ||
|
||
tput setaf 3 | ||
echo "#######################################" | ||
echo "# START WITH IMPORT " | ||
echo "#######################################" | ||
tput sgr0 | ||
|
||
tput setaf 2 | ||
echo "Checking provided file..." | ||
tput sgr0 | ||
if file $backup_file | grep -q -c "gzip compressed data,"; then | ||
tput setaf 2 | ||
echo "OK - Gziped File" | ||
tput sgr0 | ||
if [ ! -d /backup/${tmp_dir} ]; then | ||
echo "Creating tmp.." | ||
mkdir /backup/${tmp_dir} | ||
fi | ||
echo "Extracting backup..." | ||
if [ "$debug" != 0 ]; then | ||
tar xzvf $backup_file -C /backup/${tmp_dir} 2>&1 \ | ||
| while read extracted_file; do | ||
ex=$((ex + 1)) | ||
echo -en "wait... $ex files extracted\r" | ||
done | ||
else | ||
tar xzf $backup_file -C /backup/${tmp_dir} | ||
fi | ||
if [ $? -eq 0 ]; then | ||
tput setaf 2 | ||
echo "Backup extracted whitout errors..." | ||
tput sgr0 | ||
else | ||
tput setaf 1 | ||
echo "Error on backup extraction, check your file, try extract it manually" | ||
tput sgr0 | ||
delete_tmp | ||
exit 1 | ||
fi | ||
else | ||
tput setaf 1 | ||
echo "Error 3 not-gzip - no standard gziped backup provided of file not installed ( Try yum install file, or apt-get install file )" | ||
tput sgr0 | ||
delete_tmp | ||
exit 3 | ||
fi | ||
cd /backup/${tmp_dir}/ | ||
main_dir=$(pwd) | ||
echo "Access tmp directory $main_dir" | ||
directadmin_user=$(grep username backup/user.conf | cut -d "=" -f 2) | ||
directadmin_usermail=$(grep email backup/user.conf | cut -d "=" -f 2 | grep @) | ||
echo "Get User: $directadmin_user" | ||
if [ -z $directadmin_usermail ]; then | ||
directadmin_usermail=$(grep domain backup/user.conf | cut -d "=" -f 2 | head -n 1) | ||
fi | ||
|
||
check_sysuser=$(cut -f 1 -d : /etc/passwd | grep "^$directadmin_user$") | ||
if [ -n "$check_sysuser" ] || [ -e "$HESTIA/data/users/$directadmin_user" ]; then | ||
delete_tmp | ||
check_result "$E_EXISTS" "user $directadmin_user exists" | ||
fi | ||
|
||
echo "Generate random password for $directadmin_user and create Hestiacp Account ..." | ||
new_password=$(generate_password) | ||
$BIN/v-add-user $directadmin_user $new_password $directadmin_usermail $hestia_package | ||
if [ "$?" -ne 0 ]; then | ||
tput setaf 1 | ||
echo "Error: Unable to create user" | ||
tput sgr0 | ||
exit 1 | ||
fi | ||
|
||
tput setaf 3 | ||
echo "#######################################" | ||
echo "# DATABASE " | ||
echo "#######################################" | ||
tput sgr0 | ||
|
||
# Restore databases | ||
mysql -e "SET GLOBAL max_allowed_packet=1073741824;" | ||
# start with databases | ||
tput setaf 2 | ||
echo "Start with databases" | ||
tput sgr0 | ||
|
||
echo "Get local databases" | ||
mysql -e "SHOW DATABASES" > server_dbs | ||
da_db_list=$(ls -1 backup/ | grep ".conf") | ||
function run_da_db() { | ||
for da_db in $da_db_list; do | ||
|
||
database_name=${da_db::-5} | ||
grep -w $database_name server_dbs | ||
if [ $? == "1" ]; then | ||
if [ -e "backup/${database_name}.sql" ]; then | ||
|
||
#Get the database name | ||
db=$(grep db_collation backup/${da_db} | tr '&' '\n ' | grep SCHEMA_NAME | cut -d "=" -f 2) | ||
|
||
tput setaf 2 | ||
echo " Create and restore ${db} " | ||
tput sgr0 | ||
mysql -e "CREATE DATABASE $db" | ||
mysql ${db} < backup/${db}.sql | ||
#Get all the users of the database | ||
while IFS= read -r line; do | ||
|
||
selectdb_line=$(echo $line | grep passwd) | ||
if [ ! -z "$selectdb_line" ]; then | ||
|
||
db_user=$(echo $selectdb_line | tr '&' '\n ' | grep ${directadmin_user} | cut -d "=" -f 1) | ||
md5=$(echo $selectdb_line | tr '&' '\n ' | grep passwd | cut -d "=" -f 2) | ||
|
||
echo "DB: $db" | ||
echo "udb: $db_user" | ||
echo "Password: ${md5}" | ||
|
||
echo "DB='$db' DBUSER='$db_user' MD5='$md5' HOST='localhost' TYPE='mysql' CHARSET='UTF8' U_DISK='0' SUSPENDED='no' TIME='$time' DATE='$data'" >> /usr/local/hestia/data/users/$directadmin_user/db.conf | ||
fi | ||
|
||
done < "backup/${da_db}" | ||
|
||
# Leave hestia restore passwords and create users | ||
tput setaf 2 | ||
echo "Rebuild databases files for $directadmin_user" | ||
tput sgr0 | ||
$BIN/v-rebuild-databases $directadmin_user | ||
fi | ||
else | ||
tput setaf 1 | ||
echo "Error: Cant restore database $db alredy exists in mysql server" | ||
tput sgr0 | ||
fi | ||
done | ||
} | ||
|
||
if [[ -z $da_db_list ]]; then | ||
echo "No database found" | ||
else | ||
run_da_db | ||
echo "Run" | ||
fi | ||
|
||
tput setaf 3 | ||
echo "#######################################" | ||
echo "# DOMAINS " | ||
echo "#######################################" | ||
tput sgr0 | ||
|
||
# Start with domains | ||
tput setaf 2 | ||
echo "Start with domains" | ||
tput sgr0 | ||
directadmin_domain_list=$(ls -1 domains/) | ||
for directadmin_domain in $directadmin_domain_list; do | ||
tput setaf 2 | ||
echo "Add $directadmin_domain if not exists" | ||
tput sgr0 | ||
$BIN/v-add-domain ${directadmin_user} $directadmin_domain | ||
if [ $? -ne 0 ]; then | ||
tput setaf 4 | ||
echo "Domain $directadmin_domain already added in some account, skip..." | ||
tput sgr0 | ||
elif [ -d /home/${directadmin_user}/web/${directadmin_domain} ]; then | ||
echo "Domain $directadmin_domain added, restoring files" | ||
echo $directadmin_domain >> restored_domains | ||
rm -f /home/$directadmin_user/web/$directadmin_domain/public_html/index.html | ||
rm -f /home/$directadmin_user/web/$directadmin_domain/public_html/robots.txt | ||
|
||
public_sync_count=0 | ||
rsync -av domains/${directadmin_domain}/public_html/ /home/$directadmin_user/web/$directadmin_domain/public_html 2>&1 \ | ||
| while read file_dm; do | ||
public_sync_count=$(($public_sync_count + 1)) | ||
echo -en "-- $public_sync_count restored files\r" | ||
done | ||
|
||
chown ${directadmin_user}:${directadmin_user} -R /home/${directadmin_user}/web/${directadmin_domain}/public_html | ||
chmod 751 /home/${directadmin_user}/web/${directadmin_domain}/public_html | ||
|
||
if [[ -L "domains/${directadmin_domain}/private_html" && -d "domains/${directadmin_domain}/private_html" ]]; then | ||
echo "private_html is a symlink to public_html so we don't need to copy it." | ||
else | ||
private_sync_count=0 | ||
|
||
rsync -av domains/${directadmin_domain}/private_html/ /home/$directadmin_user/web/$directadmin_domain/private 2>&1 \ | ||
| while read file_dm; do | ||
private_sync_count=$(($private_sync_count + 1)) | ||
echo -en "-- $private_sync_count restored files\r" | ||
done | ||
chown ${directadmin_user}:${directadmin_user} -R /home/${directadmin_user}/web/${directadmin_domain}/private | ||
chmod 751 /home/${directadmin_user}/web/${directadmin_domain}/private | ||
fi | ||
else | ||
echo "Ups.. cant restore or add domain: $directadmin_domain" | ||
fi | ||
done | ||
echo "Domains restored!" | ||
|
||
tput setaf 3 | ||
echo "#######################################" | ||
echo "# E-MAIL " | ||
echo "#######################################" | ||
tput sgr0 | ||
tput setaf 2 | ||
echo "Start restoring mails" | ||
tput sgr0 | ||
function da_restore_imap_pass() { | ||
#DirectAdmin passw is SHA512-CRYPT | ||
da_orig_pass=$(grep -w $1 backup/$2/email/passwd | tr ':' ' ' | cut -d " " -f2) | ||
echo ${da_orig_pass} | ||
USER_DATA=$HESTIA/data/users/${3}/ | ||
update_object_value "mail/${2}" 'ACCOUNT' "${1}" '$MD5' "{SHA512-CRYPT}$da_orig_pass" | ||
echo "Password for $1@$2 restored" | ||
} | ||
echo cat restored_domains | ||
if [ -e restored_domains ]; then | ||
cat restored_domains | while read da_mail_domain; do | ||
if [ "$(ls -A imap/${da_mail_domain}/)" ]; then | ||
tput setaf 2 | ||
echo "Found Imap for ${da_mail_domain}" | ||
tput sgr0 | ||
ls -1 imap/${da_mail_domain}/ | while read da_imap; do | ||
tmp_pass=$(generate_password) | ||
$BIN/v-add-mail-account $directadmin_user $da_mail_domain $da_imap tmp_pass | ||
if [ "$debug" != 0 ]; then | ||
rsync -av imap/${da_mail_domain}/${da_imap}/Maildir/ /home/${directadmin_user}/mail/${da_mail_domain}/${da_imap} 2>&1 \ | ||
| while read backup_file_dm; do | ||
sk_sync=$((sk_sync + 1)) | ||
echo -en "-- $sk_sync restored files\r" | ||
done | ||
echo " " | ||
else | ||
rsync imap/${da_mail_domain}/${da_imap}/Maildir/ /home/${directadmin_user}/mail/${da_mail_domain}/${da_imap} | ||
fi | ||
chown ${directadmin_user}:mail -R /home/${directadmin_user}/mail/${da_mail_domain}/${da_imap} | ||
find /home/$directadmin_user/mail/$da_mail_domain -type f -name 'dovecot*' -delete | ||
da_restore_imap_pass $da_imap $da_mail_domain $directadmin_user | ||
done | ||
|
||
$BIN/v-rebuild-mail-domain $directadmin_user $da_mail_domain | ||
fi | ||
done | ||
fi | ||
delete_tmp | ||
tput sgr0 | ||
tput setaf 2 | ||
echo "#######################################" | ||
echo "DirectAdmin account $directadmin_user restored" | ||
echo "Review your content and report any fail" | ||
tput sgr0 | ||
exit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters