I just scratched an itch I’ve had for a long time – a proper way to backup cPanel accounts from a reseller account. Up until now I’ve been scripting w get to grab the daily-generated backup of specific accounts every week (imitating what you can do manually by logging into cPanel and navigating to the Backup tab). It was annoying because I had to manually put in the username and password of each account I wanted to backup. What I really wanted was a solution to backup all accounts with minimal fuss.
The solution is a cron job that runs from my reseller account (you don’t need SSH access to do this as cPanel gives you access to your crontab). The solution looks like this:
nice -n 19 tar czhf - ~/backups | ssh user@server.com "cat > /cPanel_backups/$(date +\%A).tgz"
My reseller server puts the daily-generated backups into ~/backups. So, all we have to do is tar this up and copy it over to a backup server.
Notes:
- We give tar the h option so that it stores the contents of symlinked directories in the archive, not real symlinks (the /backups directory in the primary reseller account contains symlinks inside sub-account subdirectories to the real location of eg. mysql snapshots etc..)
- We compress the entire directory and pipe it across the wire via ssh
- The backup is stored with the current day of the week in the name, effectively giving us 7-day rotation
- You should set up password-less SSH access using public/private keys so that the command isn’t prompted for a password
- The above command should be run from cron
- The backslash escaping of the percentage sign in the date command is necessary when the command is run from cron
- UPDATE: nice‘d the command so that it doesn’t consume too many resources and get killed by my provider
As usual, I’ve documented this for future reference in Paddypedia.