If you familiar in administrating cPanel, you should know a script/tool called pkgacct. This is the backup tools being used by cPanel in order to create and manage cPanel user’s account backup. By using this tool, we can take advantage by create a centralized backup server where cPanel account backup will be sent over to another server via FTP on weekly basis.
Following picture shows the architecture of the centralized backup I made:
1. I will store the cPanel backup in a Windows 2008 R2 server and I will be using FileZilla as the FTP server. Download the installer from here and follow the installation wizard. Just accept all defaults value during the installation process.
2. Add FTP required port into Windows Firewall:
Start > Administrative Tools > Windows Firewall with Advanced Security > Inbound Rules > New Rule > Port > Next > under Specific local ports enter this value: 20, 21 > Next > Allow the connection > Next > tick all for Domain, Private, Public > Next > put a name like FileZilla FTP > Finish.
3. Create FTP user and assign a directory called centralized_backup under C:\ partition:
FileZilla > Users > Add > enter username and password for respective user. Then go to Shared folders > Add Shared folders > C:\centralized_backup and tick all permissions on files and directories.
4. Make sure you can telnet port 21 from the cPanel servers:
$ telnet cpbackup.mypeng.org 21 Trying 126.96.36.199... Connected to cpbackup.mypeng.org (188.8.131.52). Escape character is '^]'. 220-FileZilla FTP server 220 version 0.9.40 beta
5. Setup a schedule task so it will delete files longer than 30 days in the centralized backup directory:
Start > All Programs > Accessories > System Tools > Task Scheduler > Create Task. Enter following information:
General > Name: Delete_old_backup
General > Security options: SYSTEM
Triggers: Weekly every Sunday of every week
Actions: Start a program
Actions > Program/scripts: forfiles
Actions > Add arguments (optional):
/p "c:\central_backup" /s /d -30 /M *.tar.gz /c "cmd /c del @file : date >= 30 days >NUL"
Click OK to complete the setup. Screenshot of the Actions setup is as below:
1. I will use BASH script to automate the process. In each cPanel server, create a file under /root/scripts directory:
$ mkdir -p /root/scripts $ touch /root/scripts/centralbackup
Copy and paste following contents:
#!/bin/bash # Generate backups using cpbackup and then transfer them to a central server # Author: SecaGuy @ blog.secaserver.com # Local server configuration LHOSTNAME=`hostname` TEMPPATH='/root/tmp' # FTP server configuration CHOST='cpbackup.mypeng.org' CUSERNAME='centralftp' CPASSWORD='ByyPo3$d' # Dont change line below FTP=`which ftp` if [ ! -d /var/cpanel/users ] then echo "cPanel users not found. Aborted!" exit 1 else eof=`ls /var/cpanel/users | egrep -v '^\..$' | egrep -v '^\...$' | wc -l` echo "$eof cPanel user(s) found in this server" [ ! -d $TEMPPATH ] && mkdir -p $TEMPPATH || : for (( i=1; i<=$eof; i++ )) do CPUSER=`ls /var/cpanel/users | egrep -v '^\..$' | egrep -v '^\...$' | head -$i | tail -1` echo "Creating backup for user $CPUSER.." /usr/local/cpanel/scripts/pkgacct $CPUSER $TEMPPATH userbackup echo "Backup done. Transferring backup to FTP server.." FILENAME=`ls $TEMPPATH | grep tar.gz` $FTP -n $CHOST <<END_SCRIPT quote USER $CUSERNAME quote PASS $CPASSWORD binary passive mkdir $LHOSTNAME cd $LHOSTNAME lcd $TEMPPATH put $FILENAME quit END_SCRIPT echo "Removing temporary files.." rm -Rf $TEMPPATH/backup-* USERDIR=`cat /var/cpanel/users/$CPUSER | grep HOMEDIRPATHS | sed 's/HOMEDIRPATHS=//g'` rm -Rf $USERDIR/backup-* echo "Backup for $CPUSER complete!" done echo "Process complete!" exit 0 fi
2. Change the permission to executable:
$ chmod 755 /root/scripts/centralbackup
Schedule the Backup
Once all cPanel servers have been setup, I can schedule them using cron job on weekly basis (every Sunday at 12:00 AM). So I will add following line into cron job list:
$ crontab -e
Add following line:
0 0 * * 0 /root/scripts/centralbackup
Save the files and restart cron daemon:
$ service crond restart
Notes: You can use cpbackup-exclude.conf as refer to cPanel documentation page, to exclude certain files or directories from being included in this backup.