Account transfers in DirectAdmin
DirectAdmin is a web hosting control panel that we work with frequently. It certainly isn’t the most popular control panel out there, but with its clean and simple interface, spee, and price point, its a worthy contender. I recently ran into one of its shortcomings while doing a server migration.
I have been spoiled by WHM/Cpanel’s transfer scripts. They allow you to transfer sites pretty easily between servers. The scripts are automated to the point of saving and restoring user’s files, email accounts, forwarders, mysql databases, and all other relevant settings almost flawlessly. When moving sites between server’s its almost as simple as point-and-click. DirectAdmin has pretty good backup and restore features that can be used for transfers. The backup scripts packages a user’s account into a nice little tarball encapsulating all of their files, email accounts, settings, passwords, etc, much like cPanel’s scripts. The only problem is that when restoring these backups, all of the files are restored using the user’s permissions.
This becomes a problem when site owners and server admins set files to be owned by the Apache user, so that php apps can write to certain files. I contacted DirectAdmin about this and they say this is not a bug, and they wouldn’t want to restore files as root for security reasons. A server admin could then restore a malicious backup file, and if restored with root permissions, could drop a trojan or exploit on the server.
To work around this, I threw together some quick and dirty scripts to assist with a DirectAdmin server migration. I also got some random errors when DirectAdmin was importing MySQL databases, so these scripts will handle that as well
1. Export script
On the “old” server that we are moving sites away from, make a directory called /root/export and put this script in that directory, naming it export.sh:
#!/bin/bash
#
#
mysqluser="da_admin"
mysqlpass="YOUR.PASS.HERE"
cd /root/export
for i in `ls /var/lib/mysql/ | grep $1`;
do
mysqldump -Q --opt --user=$mysqluser --password=$mysqlpass $i > $i.sql
done
cd /home/$1
tar cvfpz /root/bin/$1-domains.tar.gz domains
cd /root/export
tar cvfz $1.tar.gz $1-domains.tar.gz $1*sql
rm -f $1*sql
rm $1-domains.tar.gz
scp $1.tar.gz root@10.1.1.1:import
rm -f $1.tar.gz
This script relies on you having passwordless ssh authentication setup to copy files via scp from oldserver to newserver.
2. Import script
On the new server that we are restoring sites to, make a directory called /root/import and place this script there named import.sh
#!/bin/bash
#
#
mysqluser="da_admin"
mysqlpass="YOUR.PASS.HERE"
tar xfz $1.tar.gz
cd /home/$1
rm -rf domains
mv /root/import/$1-domains.tar.gz .
tar xvfpz $1-domains.tar.gz
rm $1-domains.tar.gz
cd /root/bin
for i in `ls *sql | grep $1`;
do
dbname=`echo $i | awk -F. '{print $1}'`
echo $dbname
mysql --user=$mysqluser --password=$mysqlpass $dbname < $dbname.sql
rm -f $dbname.sql
done
chmod +x both scripts. Here is the process I follow when moving sites:
- Create a list of sites from the old server to move
- From the Reseller panel on the old server, create backups of all of the usernames you want to move
- For each username, run “export.sh” on the old server with the username as an argument (i.e. ./export.sh joeuser)
- The export.sh script will copy our custom tarball to the new server. You also need to manually copy all of the backup files from Step 2 to the reseller’s backup directory on the new server.
- Restore all of the users from the Reseller panel on the new server
- Import all of the custom backups on the new server. For each username, run import.sh with the username as the argument.
That should be it, happy transferring!