Is there any module which can take complete back of my full drupal site on daily basis ? Will really help me as my host only take backup on weekly basis.

Comments

pwolanin’s picture

Are you on a linux host with access to cron?

If so, the easiest thing (what I do) is to invoke daily a shell script.
I have this script in /sites (e.g. /sites/bachup.sh). I created a folder /sites/backup, and every day the backup is saved there. Note, that every day this erases the previous day's backup. With a little work, you could make it leave a week's work of backups, or some such.

If you search here on d.o, you'll find some other similar scripts posted.

Make sure you chmod 700 the script, and substitute below the relevant values for your mysql account for <USERNAME> <PASSWORD> <DATABASE>, and change "mysite" to your site name (if desired).

#01-23-2007 by PWolanin
#a simple script to execute via Cron to make a downloadable
#archive of all website files.  Switched to using zip for better PC compatibility

umask 0077;

DT=`date "+%m%d%y"`;

PREFIX='mysite-backup';

FN=$PREFIX$DT;

cd ~/public_html;

rm -f sites/backup/$PREFIX*;

DBFN='mysite-backup'$DT'.sql';

mysqldump -u<USERNAME> -p<PASSWORD>  --add-drop-table <DATABASE> > $DBFN;

zip -q -r sites/backup/$FN . -x sites/backup/* 

rm -f $DBFN;

---
Work: BioRAFT

Pushkar Gaikwad’s picture

yes I am on Linux server but I am already using one cron on cpanel. Can you tell me how to set multiple crons through cpanel (I don't have ssh access). I hope the script is safe :). the "-f" option is always scarier to me..the last thing I want is to completely erase my site

Regards

pwolanin’s picture

I'm also doing it on cpanel- it should be as easy as visiting the "Coron jobs" page (pick "standard")- there will be another blank spot to add a new one each time you visit it (at least for me there is).

Then just put in something like this for the command:

~/public_html/sites/backup.sh

And set it to run once a day late at night or early morning.

---
Work: BioRAFT

gurukripa’s picture

I know very little about software. Drupal is such an amazing software that its easy to learn. Please tell me if there is a module for 5.1 which will back up by itself. I dont know php or mysql or chmod etc..i see these terms being used :)

I can follow basic instructions...so is there something for a dummy like me :)

Backup for dummies....i would prefer to back the entire site and database everyday...on the server itself..and make a local copy on my computer once a week....and a system which deletes old copies a week old..and installs the latest one is a great idea..

someone please help me on this.

thanks.

Pushkar Gaikwad’s picture

Actually I am myself not able to solve my cpanel problem. I am still working on it. If I could do something about it than will surely post it

Cheers

Dirty Bird’s picture

everytime I try to implement the php back up script (Im not even to adding a cron job yet!) I get the following error: Warning: filesize() [function.filesize]: stat failed for /home/antsocom/backup.24.tar.gz in /home/antsocom/public_html/dirtybirddesignlab.com/backup.php on line 5

I really dont know what to do and any help would be much appreciated.
Thank you

vm’s picture

Before you go to this kind of trouble, insure that your host doesn't already make backups for you. Esepcially of your files..... Many do. If yours however does not continue to read on.

USE AT YOUR OWN RISK

What I use:

for website files

Here is a php script that will create a compressed backup of your website files, creating a different file each day for a month (then start over) so you have 30 days of backups. It will email you with a backup confirmation. Run it every night using cron to kick it off. You should download the backup files from the server on a regular basis.

<?php 
$emailaddress = "XXXXXX@yourdomain.com"; 
$target = "/home/".get_current_user()."/backup.".date(d).".tar.gz"; 
if (file_exists($target)) unlink($target); 
system("tar --create --preserve --gzip  --file=".$target." ~/public_html ~/mail",$result); 
$size = filesize($target); 
switch ($size) { 
  case ($size>=1048576): $size = round($size/1048576) . " MB"; break; 
  case ($size>=1024);    $size = round($size/1024) . " KB"; break; 
  default:               $size = $size . " bytes"; break; 
} 
$message = "The website backup has been run.\n\n"; 
$message .= "The return code was: " . $result . "\n\n"; 
$message .= "The file path is: " . $target . "\n\n"; 
$message .= "Size of the backup: " . $size . "\n\n"; 
$message .= "Server time of the backup: " . date(" F d h:ia") . "\n\n"; 
mail($emailaddress, "Website Backup Message" , $message, "From: Website <>");  
?> 

If you want to exclude a directory, add the --exclude parm to the tar command as shown in the example below (which will exlcude the public_html/audio directory):
system("tar --create --preserve --gzip --file=".$target." --exclude ~/public_html/audio ~/public_html ~/mail",$result);

Please note that this script is very resource intensive. Do not run it more than once a day, and do not run it at peak times. If you have a large site the script may timeout depending on the server settings, but the backup may still be created.
Before using cron, run the script from your browser to make sure it works as anticipated.
Set a cron job to run the script once a day (at an odd time - like 4:23am - to minimize server load issues).
This example backs up the public_html and mail directories.
The backup file (named backup.XX.tar.gz) will be created in your root directory (above public_html).

for MySQL

Here is a php script that will backup your MySQL database, creating a different file each day for a month (then start over) so you have 30 days of backups. It will email you with a backup confirmation. Run it every night using cron to kick it off. Replace the X's with your information. You should also download the backup files from the server on a regular basis.

<?php 
$emailaddress = "XXXXXX@yourdomain.com"; 
$host="XXXXXX"; // database host 
$dbuser="XXXXXX"; // database user name 
$dbpswd="XXXXXX"; // database password 
$mysqldb="XXXXXX"; // name of database 
$filename = "/full_server_path_to_file_goes_here/backup" . date("d") . ".sql"; 
if ( file_exists($filename) ) unlink($filename); 
system("mysqldump --user=$dbuser --password=$dbpswd --host=$host $mysqldb > $filename",$result); 
$size = filesize($filename); 
switch ($size) { 
  case ($size>=1048576): $size = round($size/1048576) . " MB"; break; 
  case ($size>=1024): $size = round($size/1024) . " KB"; break; 
  default: $size = $size . " bytes"; break; 
} 
$message = "The database backup for " . $mysqldb . " has been run.\n\n"; 
$message .= "The return code was: " . $result . "\n\n"; 
$message .= "The file path is: " . $filename . "\n\n"; 
$message .= "Size of the backup: " . $size . "\n\n"; 
$message .= "Server time of the backup: " . date(" F d h:ia") . "\n\n"; 
mail($emailaddress, "Database Backup Message" , $message, "From: Website <>");  
?> 

You can restore the database using the following line in a php script:

system( "mysql --user=$dbuser --password=$dbpswd --host=$host $mysqldb < $filename",$result);

Note that hosts may have different paths for the mysql commands. For example /usr/local/bin/mysqldump could be required.

If you want the backup file to be compressed, then change two lines (the $filename variable and the system() command) as follows:

$filename = "/full_server_path_to_file_goes_here/backup" . $day . ".sql.gz"; 
system( "mysqldump --user=$dbuser --password=$dbpswd --host=$host $mysqldb | gzip > $filename",$result); 
Pushkar Gaikwad’s picture

Thanks alot. So I have creatd a backup folder in my public_html folder and than createa bacckup.php file with 755 permissions. Than edited it appropriately by putting my database info. Now I am going to put it in cron. That's it. Right ?

Anything I need to be careful of ?

Edit - Ok, I just executed the php script and got the following message in my mailbox

The database backup for mysite_drpl1 has been run.

The return code was: 1

The file path is: /home/mysite/public_html/backup/05.sql

Size of the backup: 0 MB

Server time of the backup:  March 05 03:53pm

Obviously theew was no database created. Is there any permission problems here ? because everything seems to be OK.

Pushkar Gaikwad’s picture

also when I open the page http://mysite.com/backup/backup.php , it gives the error

Warning: filesize() [function.filesize]: Stat failed for /home/mysite/public_html/backup/05.sql (errno=2 - No such file or directory) in /home/mysite/public_html/backup/backup.php on line 10

vm’s picture

you can probably remove the mail backup, since I have no idea where your mail folder is stored.

witout seeing how you altered the paths, there is no way for me to bebug why this won't work in your environment. There are no guarantees that it will work for you. I store my backups above the public root, not in it. This was written to work in my environment and may need some tweaking to work in yours. Maybe posting it to your hosts support forum would be fruitful.

as stated above:

Note that hosts may have different paths for the mysql commands. For example /usr/local/bin/mysqldump could be required.

If your host is site5.com these scripts should work without issue. if you are on a different host, There could be bumps in road, or they just may not work at all.

Mguel’s picture

Hi, I've wanted to have a daily backup for only one week of public_html and of database. I found this old thread and wanted to post what I did so maybe it helps others.

I've created to bash scripts wich I put on the root of my site with executable privileges, and call them to run once a day during low traffic hours from cron job.

And the scripts are:

#!/bin/bash
DATESTAMP=`date +%Y%m%d`
mysqldump --opt -h 127.0.0.1 -u MYDATABASEUSER -MYPASS MYDATABASE | gzip -c > db_auto-backup`date "+%Y%a"`.gz

and:

#/bin/bash
/bin/tar czPf /home/MYSITE/public_html-auto-backup_`date "+%Y%a"`.tar.gz /home/MYSITE/public_html

%a stands for short weekday, and as I omitted day and month, I only get backup of the last week only.

Cheers,
Mguel

PS: any correction or improvement is welcome since I'm not a programmer nor nothing near.

mttjn’s picture

Thanks for posting these scripts!
Small clarification: you put the scripts in the root directory for your site, and where are files created? Are they also created in the root directory?
For example, if you just have one site and it's in public_html, you would put these scripts in public_html, and then where will the files get created?