Bash script for auto transfer cpanel accounts to directadmin

FaЯshid

New member
Joined
Sep 11, 2019
Messages
3
Hi everyone

I have created a bash script to automatically transfer cpanel user accounts to directadmin, which I will share with you.
Hope it will be useful.

The steps to execute the script are as follows:

Creat a full backup of account on cpanel. (This full backup does not include the public_html directory)
Transfer backup to directadmin server and restore that.
After the full backup in directadmin restores the user public_html directory information in cpanel transfer to the user public_html directory in directadmin.
Deletes cpanel full backups from both servers in order to prevent space occupancy.
Changes the account ip in the cpanel to the directadmin server ip to load the site from the directadmin server.
Suspends the user in cpanel server so that the user does not have access to the previous panel.
These steps are performed respectively for each cpanel user account.

You can see the script below
Code:
#!/bin/sh
#You can get the script updates from  https://bit.ly/2ktJVHn

#Note that you need to add the DirectAdmin IP server from the "Add a New IP Address" section on the WHM panel before running the script.
#If you do not intend to change the IP after transferring the account, remove the following line from the script:
#/usr/local/cpanel/bin/setsiteip -u $USER $DEST

DEST="192.168.1.1" #Your DirectAdmin Server IP
PORT="22" #Your Directadmin Server ssh Port
cd /var/cpanel/users
for USER in *; do
if [ "$USER" != "system" ]
then
       echo "starting transfer process for $USER"
	sleep 2

        #backup cpanel account (public_html excludes) and transfer to directadmin server.
        /scripts/pkgacct $USER --skippublichtml
        rsync -av -e "ssh -p $PORT" /home/cpmove-$USER.tar.gz root@$DEST:/backup/

	#Restore backup in directadmin server.
	ssh -p $PORT root@$DEST "
		chown admin.admin /backup/
		chown admin.admin /backup/cpmove-$USER.tar.gz
		echo 'action=restore&ip%5Fchoice=select&ip=$DEST&local%5Fpath=%2Fbackup&owner=admin&select%30=cpmove-$USER.tar.gz&type=admin&value=multiple&when=now&where=local' >> /usr/local/directadmin/data/task.queue
		echo "backup is restoring in DirectAdmin!"
		sleep 2
		while \$(grep -Fq "action=restore" /usr/local/directadmin/data/task.queue 2>/dev/null); do printf "%s""$i" .; sleep 2; done;
		while \$(test ! -d /home/$USER/public_html); do printf "%s""$i" .; sleep 2; done;
		printf '\n'
		echo "backup restoration completed in DirectAdmin!"
		sleep 2
	"
	
	#transfer public_html files and dirs to destinaton server	
	rsync -av -e "ssh -p $PORT" /home/$USER/public_html/ root@$DEST:/home/$USER/public_html/
	ssh -p $PORT root@$DEST "
		chown -R $USER.$USER /home/$USER/public_html/
		find /home/$USER/public_html/ -type d -exec chmod 755 {} \;
	"

	#remove backup files in order to free space
	ssh -p $PORT root@$DEST "rm /backup/cpmove-$USER.tar.gz"
	rm /home/cpmove-$USER.tar.gz

	#change user ip in order to transfer site loding to destination server.
	/usr/local/cpanel/bin/setsiteip -u $USER $DEST	

	/scripts/suspendacct $USER "User transferred to Direct Admin."

	echo "transfer process for $USER has been completed!"
	sleep 2
	printf '\n'
fi
done
 

FaЯshid

New member
Joined
Sep 11, 2019
Messages
3
YW

Note that the connection between servers must be established via the ssh key.

Important note that none of cpanel accounts should be suspended! If so, the script will not work correctly.
unsuspend all suspended cpanel accounts before executing the script in your server.
 

M7Web

Verified User
Joined
Jul 13, 2019
Messages
95
Why is the public_html backup separate? Can I migrate resellers and their accounts separately or only work for all accounts?
 

FaЯshid

New member
Joined
Sep 11, 2019
Messages
3
Why is the public_html backup separate? Can I migrate resellers and their accounts separately or only work for all accounts?
If your website is large in size, backing up, moving and restoring it to another server will naturally take time and also more space usage.
Because all files have to be compressed and then uncompressed.
File transfer by rsync will be faster and hassle free.

for the other question you can transfer users of a list. use the below script instead of the first one

you should put users in /root/userlist.txt (one user per line)

Code:
#!/bin/sh
#You can get the script updates from  https://bit.ly/2lE9QMH

DEST="192.168.1.1" #Your DirectAdmin Server IP
PORT="22" #Your Directadmin Server ssh Port

while read USER ; do

       echo "starting transfer process for $USER"
	sleep 2

        #backup cpanel account (public_html excludes) and transfer to directadmin server.
        /scripts/pkgacct $USER --skippublichtml
        rsync -av -e "ssh -p $PORT" /home/cpmove-$USER.tar.gz root@$DEST:/backup/

	#Restore backup in directadmin server.
	ssh -p $PORT root@$DEST "
		chown admin.admin /backup/
		chown admin.admin /backup/cpmove-$USER.tar.gz
		echo 'action=restore&ip%5Fchoice=select&ip=$DEST&local%5Fpath=%2Fbackup&owner=admin&select%30=cpmove-$USER.tar.gz&type=admin&value=multiple&when=now&where=local' >> /usr/local/directadmin/data/task.queue
		echo "backup is restoring in DirectAdmin!"
		sleep 2
		while \$(grep -Fq "action=restore" /usr/local/directadmin/data/task.queue 2>/dev/null); do printf "%s""$i" .; sleep 2; done;
		while \$(test ! -d /home/$USER/public_html); do printf "%s""$i" .; sleep 2; done;
		printf '\n'
		echo "backup restoration completed in DirectAdmin!"
		sleep 2
	"
	
	#transfer public_html files and dirs to destinaton server	
	rsync -av -e "ssh -p $PORT" /home/$USER/public_html/ root@$DEST:/home/$USER/public_html/
	ssh -p $PORT root@$DEST "
		chown -R $USER.$USER /home/$USER/public_html/
		find /home/$USER/public_html/ -type d -exec chmod 755 {} \;
	"

	#remove backup files in order to free space
	ssh -p $PORT root@$DEST "rm /backup/cpmove-$USER.tar.gz"
	rm /home/cpmove-$USER.tar.gz

	#change user ip in order to transfer site loding to destination server.
	/usr/local/cpanel/bin/setsiteip -u $USER $DEST	

	/scripts/suspendacct $USER "User transferred to Direct Admin."

	echo "transfer process for $USER has been completed!"
	sleep 2
	printf '\n'

done < /root/userlist.txt
 

jwn

Verified User
Joined
Jul 11, 2019
Messages
18
Great job,

Better if also include transfer all file with Rsync
 
Top