Automated Backups in CPanel

Submitted by Josh on
I'm a middleman when it comes to my own webserver. My sites collectively are too large to exist as separate shared hosting with a webhost, but I also want to keep them all organized together and have the flexibility of managing each individual site that would come with a colocated or private server. If you're familiar with non-enterprise web hosting at all, you've probably just said to yourself "he must be on a VPS, then," and you're exactly right.
 
I host only a half-dozen sites or so (though I'm looking for more clients!), so I've got enough going on that backups would be a chore to keep up manually. Because of that, I've got a couple helper scripts that I can run automatically via crontab on my VPS to make sure that things get backed up regularly and safely. Both of these scripts below are fairly simple bash scripts that I can run on my machine that itself runs CentOS 7 with Apache, WHM, and CPanel; the first script backs up databases, the second full CPanel accounts, and both FTP the results to an offsite server for later permanent storage.
 
The database backup script automates mysqldump to create dumps of one or more MySQL databases on the server. The script serves as a wrapper to process one or more database, tarball and gzip the results, and FTP them away, cleaning up after itself after. This allows me to process a number of small databases with infrequent changes all at once, or to more often process the larger databases for repeated backups. The script uses standard FTP to send the files over, but could easily be amended to use vsftp or similar executables depending on the source system and the target FTP server's capabilities.
 
The second script does the same thing, but uses the CPanel pkgacct script to roll up an entire CPanel account at a time. This is most useful for multi-account setups like those running under WHM, but could also be used for single-account instances as long as the user has access to bash and sufficient permissions to run pkgacct. Pkgacct is useful because it will collect everything relevant to the account, including email, databases, homedir, and various settings - the original use for pkgacct was to transfer a user account from one CPanel installation to another.
 
There are certainly other ways to do this, and folks should feel free to fork what I've got up on Bitbucket, but this is a method that's served me well for over a half-decade to keep my content safely offsite.