Cron job - Backup cPanel and DBs

Hello guys and girls!

I’m sitting with a server, with lots of websites, and I’m really tired of taking backup manually on very single cPanel account once a week, and every day for websites that is under development.

I found this nice script: » cPanel Automated Backup Script :: v-nessa.net :: Programming is Sexy - Edited it to running SSL, pasted my login info into it, and placed the cpanel_backup.php in the Root of each cpanel account (right before public_html).

I was going to cron-jobs in cPanel, set it should take backup every night at 00.00… It runs the cron job, and I recieve a mail that the cron has been run… But no backups are generated.

Do anyone know how to solve it?

Or is there a better way to backup cpanel + DB’s the same way as cPanel itselfs do?, generating same type of backup and so on?

Thank you!

and best regards,
Lucas

Do you have root access to the server?

I have access to WHM at least - But else I can just contact my dude, and he will give me access :stuck_out_tongue:

If you have root and command line access, then I would take a look at the /scripts/pkgacct script included with cPanel.

This allows you to create a tarball of each account. There are a lot of different parameters you can pass to the script. Something like:

/scripts/pkgacct <ACCOUNT> /backup backup --nocompress

Would backup <ACCOUNT> and place a tarball (named <ACCOUNT>.tar) in the /backup directory on the server.

The --nocompress flag just tells the system not to gzip the tarball. If you can spare the disk space, I recommend this because gzip compression can load down a server, making your server sluggish while performing the backup.

Thank you alot for the answer! But I recommend using Cron and PHP for doing the job, but just wondering why it’s not working, placing the file the right place, making the cronjob to run at 00.00 but no backup generated.

About the gzip compression, I’m not worried about that - When doing a backup it uses like 1.5-2.0% of the CPU (normal is 0.00-0.10%) - And it’s dedicated, and around 00.00 we don’t really have any visitors, because it’s mainly people within EU that is using the sites, and the sites stored at the moment have activity to around 10pm and starts again at 5am.

But if any have a PHP & CronJob or just Cronjob way to do it! I would be happy!

Thank you

I would point out that the link you gave is from a post back in 2007. The script may be outdated.

Basically, this script appears to just log into your cPanel account and use the Backup function from there.

In my opinion, the /scripts/pkgacct method is easier and more streamlined (The PHP script basically loads an instance of PHP, then loads a cPanel instance, which essentially runs the /scripts/pkgacct script anyway, it’s a round-about way of doing the same thing). However the /scripts/pkgacct would require you to have root-level access.

Can I do, so the backups will be stored in the useraccounts root like if I do a normal cpanel backup, it places the backup in the root of the user I’m logged into.

Maybe it already does that! but yeah :stuck_out_tongue:

And I was wondering, how do I then say how often the script should be executed? Best regards

You can.

Are you wanting to have a rolling backup, meaning one single backup that just gets overwritten each time?

Or are you wanting to keep backdated backups?

It would be nice to have like “backup-DD.MM.YYYY_Hour-Minutes-Seconds_account.tar.gz” or just “backup_account_DD_MM_YYYY_Hour_Minutes.tar.gz”

So I can kinda keep track on when backup is generated, and what account it’s for - Cuz’ I download them to a folder on my external, and then upload to Amazon s3 to be sure.

You would probably need to create a script that does all of this for you.

Create a bash script somewhere on the server (for example /root/backup.sh):

#!/bin/bash

DATE=`/bin/date +%d.%m.%Y_%k-%M-%S`

if [ -f "/var/cpanel/users/${1}" ]
then
        mkdir -p /backup
        /scripts/pkgacct ${1} /backup backup &gt;/dev/null 2&gt;/dev/null
        /bin/mv /backup/${1}.tar.gz /home/${1}/backup-${DATE}-_${1}.tar.gz
fi

Then change the permissions on this script so the execute bit is set:

chmod 700 /root/backup.sh

Then in your cron or on the command line execute:

/root/backup.sh &lt;ACCOUNT&gt;

Replacing <ACCOUNT> with the actually username of the account.

This script will execute /scripts/pkgacct for that user, creating a backup (initially in /backup). Then it will move that backup file into the user’s home directory (/home/${1}/backup-${DATE}-_${1}.tar.gz)

Note, all of this has to be executed as root for this to work right.

Pretty awesome :o Thank you alot!

To be honest i’m wondering what made you feel to avoid WHM backup option.

If you choose daily backup option you will get monthly backup and weekly also and you can make it incremental so that it wont eat your space also. Also you can choose backup accounts option also.

Thank you a lot! I’m moving server next month, and I will get a full WHM, (It’s a little limited now for some reason :S)

Then I will be able to do it right!

Best regards,
Lucas Rolff, and thank you - Problem solved.

When you start using WHM pkgacct backups (through cpbackup) - check out the postcpbackup script at http://whmscripts.net/backups/2010/postcpbackup-auto-cpbackup-rotation/ which automatically rotates backups for you, keeping a history - eg 5 daily, 2 weekly, and 2 monthly - or whatever you set it to.