in Challenge, Crash Course, Interesting

How to backup and restore one or multiple cpanel accounts via command line / ssh

Here are a couple of one-liners that come handy when you’re about to do a big dedicated server move. The first one-liner packages each one of your accounts into its own “cpmove” file, which you then transfer to /home/ on your new/destination server. Be sure to execute this as root via SSH/Bash:

mkdir /home/backup.logs; cd /home; ls -1|while read a; do /scripts/pkgacct ${a} &> /home/backup.logs/${a}.log; done &

Depending on the number of accounts that you have and/or the amount of data to be backed up, the above command can take from a minute to several minutes or hours.

We wrote these one-liners because a client of ours needed help transferring his old server to his new fully managed unixy node. We provide fully managed servers and clusters in four of our locations around the world and are very happy to go above and beyond to make a client’s day. We LOVE helping our clients!

You can follow the progress of the above task by looking up directory /home/backups.logs/. There you’ll see the log files being created and updated as the command progresses through all accounts. So, how do you know whether the job completed? Run this shell command: jobs. If you see output similar to this it means the backup is still in progress:

# jobs
[1]+ Running ls –color=tty -1 | while read a; do
/scripts/pkgacct ${a} >&/home/backup.logs/${a}.log;
done &

Once the above one-liner finishes running, jobs won’t show any active program. In fact, the second it finishes it will show Done and will exit normally. So it completes, use scp or rsync to transfer the files over to the destination/new server. You can use this command:

scp -oPort=22 /home/cpmove*.tar.gz root@destination_server_ip:/home/

The scp command will show a visual on its progress and will tell you when it’s done transferring the files. Once it’s done, ssh in to the destination server and load up the account with this one-liner:

 mkdir /home/restore.logs; cd /home; ls -1|grep cpmove|while read a; do /scripts/restorepkg ${a} &> /home/restore.logs/${a}; done &

Monitor the progress of this command by listing the content of folder /home/restore.logs/. There you can tell where it’s at and if there are any errors (grep -i error /home/restore.logs/*).

That’s all folks!