Skip to content

Commit 0eddae9

Browse files
committed
add some tools
1 parent a12e5f7 commit 0eddae9

File tree

4 files changed

+691
-1
lines changed

4 files changed

+691
-1
lines changed

ext_transfer.sh

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
#!/usr/bin/bash
2+
3+
# Transfer Tools From External
4+
5+
## set -x
6+
7+
RED=$(tput bold; tput setaf 1)
8+
BLUE=$(tput bold; tput setaf 4)
9+
GREEN=$(tput bold; tput setaf 2)
10+
YELLOW=$(tput bold; tput setaf 3)
11+
PURPLE=$(tput bold; tput setaf 5)
12+
ENDCOLOR=$(tput sgr0)
13+
14+
clear
15+
echo -e "${PURPLE}-----------------------------------"
16+
echo -e "===| Migration Tools cPanel Server |==="
17+
echo -e "-----------------------------------${ENDCOLOR}\n\n"
18+
19+
# Check if file exists
20+
if [ ! -f $HOME/.who ]; then
21+
echo "Nama Agent : "
22+
read who
23+
echo "$who" > $HOME/.who
24+
else
25+
who=$(cat $HOME/.who)
26+
fi
27+
28+
reason_why() {
29+
# Display options for migration reasons
30+
echo "${BLUE}Choose a migration reason:${ENDCOLOR}"
31+
echo "${YELLOW}1.${ENDCOLOR} From Outside"
32+
echo "${YELLOW}2.${ENDCOLOR} Restore Terminate Hosting"
33+
34+
# Read user input for the migration reason choice
35+
read -p "${GREEN}Enter the number of your migration reason choice: ${ENDCOLOR}" choice
36+
37+
case $choice in
38+
1)
39+
reason="From Outside"
40+
;;
41+
2)
42+
reason="Restore Terminate Hosting"
43+
;;
44+
*)
45+
# Display an error message for an invalid choice and restart the function
46+
echo "${RED}Error: Invalid choice. Please select a number from 1 to 2.${ENDCOLOR}"
47+
reason_why # Restart the function to begin from the start
48+
return
49+
;;
50+
esac
51+
52+
# Display the selected migration reason
53+
echo -e "\n${BLUE}Selected migration reason:${ENDCOLOR} ${YELLOW}$reason${ENDCOLOR}"
54+
}
55+
timestart(){
56+
# timestamp start
57+
timestart=$(date)
58+
timestart_formated=$(date +"%Y-%m-%d %H:%M:%S")
59+
echo -e "${YELLOW}$timestart${ENDCOLOR}"
60+
}
61+
timefinish(){
62+
#timestap finish
63+
timefinish=$(date)
64+
timefinish_formated=$(date +"%Y-%m-%d %H:%M:%S")
65+
echo -e "${YELLOW}$timefinish${ENDCOLOR}"
66+
}
67+
68+
69+
## Define Variable
70+
slack_url=
71+
slack_url_failed=
72+
whmcs_url=https://panel.masdzub.com/admin/index.php?rp=/admin
73+
74+
timestart
75+
echo -e "${PURPLE}\n===| Asking the reason ${ENDCOLOR}\n"
76+
reason_why
77+
78+
79+
echo -e "${PURPLE}\n===| Asking the URL ${ENDCOLOR}\n"
80+
read -p "Enter URL backup : " url_backup
81+
echo ""
82+
83+
echo -e "${PURPLE}\n===| Get Information From URL ${ENDCOLOR}\n"
84+
85+
# Extract the filename from the URL
86+
filename=$(basename "$url_backup")
87+
echo -e "Nama File : $filename"
88+
89+
get_username() {
90+
# Pola regex untuk mengekstrak nama USER
91+
patterns=(
92+
"cpmove-(.*?)\.tar\.gz"
93+
"cpmove-(.*?)\.tar"
94+
"_([[:alnum:]]+)\.tar\.gz$"
95+
"_([[:alnum:]]+)\.tar$"
96+
"(.+)\.tar\.gz$"
97+
"(.+)\.tar$"
98+
)
99+
100+
for pattern in "${patterns[@]}"; do
101+
if [[ $filename =~ $pattern ]]; then
102+
echo "${BASH_REMATCH[1]}"
103+
return 0
104+
fi
105+
done
106+
107+
return 1
108+
}
109+
110+
username=$(get_username "$filename")
111+
echo -e "Username : $username"
112+
113+
echo -e "${PURPLE}\n===| Asking the destination server ${ENDCOLOR}\n"
114+
115+
# input destination server
116+
echo ""
117+
read -p "Enter the destination server (e.g. cpanel_server): " destination_server
118+
echo ""
119+
120+
# validate SSH connection to destination server
121+
echo -e "\nValidating SSH connection to $destination_server.."
122+
ssh -l root $destination_server exit >/dev/null 2>&1
123+
if [ $? -ne 0 ]; then
124+
echo "Failed to establish SSH connection to destionation server."
125+
exit 1
126+
fi
127+
echo -e "SSH connection to destionation server successful.\n"
128+
129+
echo -e "${PURPLE}\n===| Progress restoration / migration ${ENDCOLOR}\n"
130+
131+
ssh -l root $destination_server "grep -w -q $username /etc/trueuserdomains"
132+
if [ $? -eq 0 ]; then
133+
echo -e "\nUser found on the destination server."
134+
exit 1
135+
else
136+
# download backup
137+
ssh -l root $destination_server "wget -c --no-check-certificate --progress=bar:force $url_backup -P /web"
138+
echo -e "\nBackup downloaded successfully."
139+
# Restore from the URL using /script/restorepkg
140+
ssh -l root $destination_server "/scripts/restorepkg --force /web/$filename"
141+
if [ $? -eq 0 ]; then
142+
echo -e "\nRestoration completed successfully."
143+
echo -e "\nOpen username into browser at WHMCS."
144+
xdg-open "$whmcs_url/services&username=$username"
145+
timefinish
146+
migration=success
147+
else
148+
echo -e "\nFailed to restore backup."
149+
# Send failure notification to Slack
150+
#curl -X POST -H 'Content-type: application/json' --data "" $slack_url_failed
151+
migration=failed
152+
exit 1
153+
fi
154+
fi
155+
156+
# Duration
157+
start_epoch=$(date -d "$timestart_formated" +%s)
158+
end_epoch=$(date -d "$timefinish_formated" +%s)
159+
duration_seconds=$((end_epoch - start_epoch))
160+
161+
# Calculate hours, minutes, and seconds
162+
hours=$((duration_seconds / 3600))
163+
minutes=$(( (duration_seconds % 3600) / 60 ))
164+
seconds=$((duration_seconds % 60))
165+
166+
if [ "$hours" -gt 0 ]; then
167+
duration_formatted=$(printf "%02d hours %02d minutes %02d seconds" "$hours" "$minutes" "$seconds")
168+
else
169+
duration_formatted=$(printf "%02d minutes %02d seconds" "$minutes" "$seconds")
170+
fi
171+
172+
# Summary Success
173+
summary_success() {
174+
cat <<EOF
175+
{
176+
"who": "$who",
177+
"username": "$username",
178+
"url_backup": "$url_backup",
179+
"new_server": "$destination_server",
180+
"reason": "$reason",
181+
"timestart": "$timestart",
182+
"timefinish": "$timefinish",
183+
"duration": "$duration_formatted"
184+
}
185+
EOF
186+
}
187+
188+
backup(){
189+
echo -e "Copying $filename into /sharedfs/support/\n"
190+
ssh -l root $destination_server "rsync --no-owner -Paz /web/$filename /sharedfs/support/$filename"
191+
ssh -l root $destination_server "rm -f /web/$filename"
192+
}
193+
194+
if [ "$migration" = "success" ]; then
195+
echo -e "${PURPLE}\n===| Change The reseller / owner into Root ${ENDCOLOR}\n"
196+
ssh -l root $destination_server "/usr/sbin/whmapi1 modifyacct --output=jsonpretty user='$username' owner=root" | /usr/bin/jq '{metadata: .metadata, messages: .metadata.output.messages}'
197+
198+
echo -e "${PURPLE}\n===| Send Notification to Slack ${ENDCOLOR}\n"
199+
curl -X POST -H 'Content-type: application/json' --data "$(summary_success)" $slack_url | jq
200+
201+
echo -e "${PURPLE}\n===| Backup in progress.. ${ENDCOLOR}\n"
202+
backup
203+
else
204+
echo -e "Migration failed"
205+
#curl -X POST -H 'Content-type: application/json' --data "" $slack_url_failed
206+
exit 1
207+
fi

finddb_conf.sh

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/bin/bash
2+
3+
# Define color codes
4+
GREEN='\033[0;32m'
5+
RED='\033[0;31m'
6+
NC='\033[0m' # No Color
7+
8+
while getopts ":u:f:h" opt; do
9+
case $opt in
10+
u)
11+
user="$OPTARG"
12+
;;
13+
f)
14+
file="$OPTARG"
15+
;;
16+
h)
17+
echo -e "Usage: $0 -u <username> OR -f <file_path>"
18+
echo -e "Options:"
19+
echo -e " -u <username> Specify a single username to search for"
20+
echo -e " -f <file_path> Specify a file containing multiple usernames (one per line)"
21+
echo -e " -h Display this help message"
22+
echo -e "\nExample:"
23+
echo -e " $0 -u john_doe # Search for files related to a single username"
24+
echo -e " $0 -f usernames.txt # Bulk search for files for multiple usernames from a file"
25+
exit 0
26+
;;
27+
\?)
28+
echo -e "${RED}Invalid option: -$OPTARG${NC}" >&2
29+
exit 1
30+
;;
31+
:)
32+
echo -e "${RED}Option -$OPTARG requires an argument.${NC}" >&2
33+
exit 1
34+
;;
35+
esac
36+
done
37+
38+
if [ -z "$user" ] && [ -z "$file" ]; then
39+
echo -e "${RED}Error: Username or file path is required. Use '-h' for help.${NC}"
40+
exit 1
41+
fi
42+
43+
# Function to search for files related to a username
44+
search_files() {
45+
local username="$1"
46+
local document_root="$2"
47+
48+
matches=$(grep -ril -e "${username}_" --include="*.php" --include=".env" --include="config*.json" --include="config*.js" --include="server*.js" --exclude="*.js" --exclude="*.sql" --exclude="*.zip" --exclude="*.txt" --exclude="*.log" --exclude=error_log --exclude=".htaccess" "$document_root")
49+
50+
if [ -n "$matches" ]; then
51+
echo -e "${GREEN}Files found in $document_root:${NC}"
52+
echo -e "$matches\n"
53+
fi
54+
}
55+
56+
# If usernames are provided as a file
57+
if [ -n "$file" ]; then
58+
if [ ! -f "$file" ]; then
59+
echo -e "${RED}Error: File '$file' not found.${NC}"
60+
exit 1
61+
fi
62+
63+
while IFS= read -r username; do
64+
echo -e "${GREEN}Searching for files related to username: $username${NC}"
65+
66+
# Perform the search for each username in the file
67+
docs=$(uapi --user="$username" DomainInfo domains_data --output=json | jq -r -c '.result.data | .main_domain, .sub_domains[], .addon_domains[] | .documentroot' | paste -sd " ")
68+
69+
for i in $docs; do
70+
search_files "$username" "$i"
71+
done
72+
73+
done < "$file"
74+
75+
# If a single username is provided
76+
elif [ -n "$user" ]; then
77+
echo -e "${GREEN}Searching for files related to username: $user${NC}"
78+
79+
# Perform the search for the single username
80+
docs=$(uapi --user="$user" DomainInfo domains_data --output=json | jq -r -c '.result.data | .main_domain, .sub_domains[], .addon_domains[] | .documentroot' | paste -sd " ")
81+
82+
for i in $docs; do
83+
search_files "$user" "$i"
84+
done
85+
fi

mysql_size_tools.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22

33
# Tool for check MySQL size
4-
# (c) 2023 Dzub DomaiNesia
4+
# (c) 2023 Dzubayyan Ahmad
55

66
# Colors
77
RED='\033[0;31m'

0 commit comments

Comments
 (0)