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
0 commit comments