I have over 50 sites on a host that provides Cpanel for each site.
Through the Cpanel, I can do manual backups of the public_html directory, was well as the MySQL databases for each site, and have had those backups mailed to me quite successfully, but to do each site manually on a weekly basis takes a tremendous amount of time.

There is also a cronjob option, which I have tried to use - but I obviously haven't a clue of how to get it to work correctly. I find any "instructions" concerning the cron job on Cpanel to be either severly lacking or well over my newbie head.

What I would like to do is set up a cron job for each of the sites that backs up not only the databases, but also the public_html directories, zip or tar those, and email them to me on a weekly basis.

I've found scripts for the databases, but not for the public_html directories - but also, those databases just save to a folder on the server, I am not emailed the databases themselves.

Does ANYONE have such a script available that would backup both the databases and specified directories, condense them and email them on a weekly basis?

Or am I just shooting the moon at this point?
Thanks for your time concerning my request.

Recommended Answers

All 10 Replies

i don't think this would be to hard. you can use opendir and readdir to find directories and files and then add them to a zip file (I just saw a few good zip classes online, search "php zip class" in google). then have that emailed to you, which isn't too hard to do, i made an email class that will send attachments and there are many online as well. then have your database backed up to a sql file and send it in an email as well.

when i get back home i will work on this, because i can use it to. i will let you know of my results and hopefully post some code for you.

Thanks, I look forward to seeing what you come up with.

ok, the files you need are attached. it required me to use a few classes, one of mine (email) and the other I found online (zip). it will loop through a directory and add the directories/files to a zip file and send it to you as an attachment.

put the attached files somewhere above the directory you are wanting to backup. make sure you change the email to yours and the directory to the directory you are wanting to backup. i put comments in the backup.php for you.

use this command for the cron job: php -q ~/path/to/backup.php

let me know how it goes.

i can make this into a backup class, but i figured you needed this soon, so I didn't take the time to create it.

ok, the files you need are attached. it required me to use a few classes, one of mine (email) and the other I found online (zip). it will loop through a directory and add the directories/files to a zip file and send it to you as an attachment.

put the attached files somewhere above the directory you are wanting to backup. make sure you change the email to yours and the directory to the directory you are wanting to backup. i put comments in the backup.php for you.

use this command for the cron job: php -q ~/path/to/backup.php

let me know how it goes.

i can make this into a backup class, but i figured you needed this soon, so I didn't take the time to create it.

Wow, thanks for the time you spent on this. But I must be some idiot because I haven't been able to get the scripts to work. I get no feedback - not even an error email - from the cron job. I have installed different cron jobs for different tasks, and get feedback that the jobs were completed, so I know that if I could get it to work, I would get something back via email. Can you "take me by the hand" and walk me through this? This is what I'm doing:

I have edited and uploaded the respective files to a folder called backup.

I log in to my cpanel and click on the cronjob button.
I click on the Standard button, then place the command:php -q ~/home/mywebsiteusername/backup/backup.php
Then I click on the day and time I want the cron job to be done.

I have also simply put in /home/mywebsiteusername/backup/backup.php as the command, since I already have used that command successfully for a different cronjob, but nothing comes back from either command.

I apologize, but call me CLUELESS.
Again, appreciate any thoughts on what I may be doing wrong.

put the script in a web accessible area and run it from the browser. just try to backup a folder in your public_html folder to see if it works correctly on your server. I know it works on 3 others.

let me know the results.

Remember I said, "Call Me Clueless?" - yep, I'm still doing something wrong - I am misinterpreting your instructions, that much is obvious.

so here's what I did.
I moved the backups directory into the public_html folder (because that is the web accessible folder), and changed the folder requested for backup to '/images/' and reuploaded the backup.php file again to the backups folder. Then went to my browser and typed in mywebsite.com/backups/backup.php and this is what I get:
Warning: opendir(/images/) [function.opendir]: failed to open dir: No such file or directory in /home/websiteusername/public_html/backups/backup.php on line 18
Unable to open directory: /images/

See? C-L-U-E-L-E-S-S.
So sorry. There is no help for Stupid.

actually, i think i created the problem when I put the files into another folder to organize them. put the backup.php and the classes in the public_html folder. this should solve the problem. if it works, then move them outside of the web root and then direct your cronjob to the backup.php

kkeith29 you have done some great work here, Thank you,

When I run it through the browser it sends everything fine but there is nothing in the zipped file. the file structure is all there but no actual files.

An when I run it outside of the public_html folder with a cron I get an email stating "No input file specified."

Could you help?

Thank you,

It wouldn't output right if accessed through the browser. Its not sending the correct headers and the zip file isn't linked to it. The zip is also deleted at the end of the script.

What version of php are you running? I will try the script out once more since its been awhile.

As for the "No input file specified", you need to change your path to the backup page. I usually put the absolute path.

I am running 5.2.9
How do you write an absolute path to the file when the file is not in the public_html folder?

Thank you for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.