Auto Backup?

Discussion in 'Site & Server Administration' started by Richie_Ni, Apr 16, 2007.

  1. #1
    Well,I have Cpanel.
    Does anyone know how to backup the entire home directory automatically on the server or to another server and send me a mail notification?
    Do I need to set up a cron job?
    Any guidelines?

    Thanks for your help!
     
    Richie_Ni, Apr 16, 2007 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    
    <?php
    function backup( $username, $password, $hostname, $output = null, $port = 2082 )
    {
     	$total = null;
    	if( is_null( $output ) ) $output = basename( $hostname );
    	
    	$read = fopen( sprintf( 'http://%s:%s@%s:%s/getbackup/backup-%s-%s.tar.gz', $username, $password, $hostname, $port, $hostname, date( "m-d-Y" ) ), 'r' );
    	
    	if( !$read )
    		die( sprintf( 'Cannot open %s with username / password combination', $hostname ) );
    	else
    		print( "Opened connection, starting backup\r\n" );
    		
    	$write = fopen( $output, 'wb' );
    	if( !$write )
    		die( sprintf( 'Cannot open %s for writing', $output ) );
    	
    	while( !feof( $read ) ):
    		if( !( $written = fwrite( $write, fread( $read, 1024 ) ) ) ) :
    			die( sprintf( 'Cannot write to %s', $output ) );
    		else :
    			$total += $written; echo ".";
    		endif;
    	endwhile;
    	
    	echo "\r\n";
    	
    	fclose( $read );
    	fclose( $write );
    	
    	return $total ;
    }
    if( !( $written = backup( 'un', 'pw', 'hostname', 'backup.tar.gz') ) ):
    	echo "Failed to write backup";
    else:
    	echo "Backup complete";
    	// mail( ) etc
    endif;
    ?>
    
    PHP:
    if you choose an output location chmod that location to 777 before execution, if you omit one, then chmod the directory the script is in to 777

    want the rest ???
     
    krakjoe, Apr 16, 2007 IP
    Richie_Ni likes this.
  3. agnivo007

    agnivo007 Peon

    Messages:
    4,290
    Likes Received:
    289
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I guess he has a cpanel backup package too... ;)
     
    agnivo007, Apr 16, 2007 IP
  4. jcinacio

    jcinacio Peon

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    HAHAHA php 4tw! :D

    Cheers
     
    jcinacio, Apr 16, 2007 IP
  5. Richie_Ni

    Richie_Ni Illustrious Member

    Messages:
    10,721
    Likes Received:
    1,175
    Best Answers:
    0
    Trophy Points:
    410
    #5
    @Joe........do you mean that I need to create a php file......?
    @agnivo007 .......Not sure what you mean?

    Are there any other ways of doing this?
     
    Richie_Ni, Apr 16, 2007 IP
  6. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #6
    yeah it's a php script you can execute as a cron job every X amount of hours.... want rest? I stopped writing theres no point finishing if it's not useful
     
    krakjoe, Apr 17, 2007 IP
  7. Richie_Ni

    Richie_Ni Illustrious Member

    Messages:
    10,721
    Likes Received:
    1,175
    Best Answers:
    0
    Trophy Points:
    410
    #7
    Yea Joe... I need more info.
    I created a php file with your code...What to do next?
    Do I need to change something in the code?
    Where to place the file? home directory?
     
    Richie_Ni, Apr 17, 2007 IP
  8. inworx

    inworx Peon

    Messages:
    4,860
    Likes Received:
    201
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Is it posible to place the file in home dir and then execute by cron?
     
    inworx, Apr 17, 2007 IP
  9. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #9
    you can place the file anywhere on the server you have permission to write to, if you're on shared hosting that's likely to be your ftp/http root.

    change :

    // mail( ) etc

    to

    mail( 'you@yourdomain.com', 'Backup on ' . date( 'dS F y h:m:s' ), 'wrriten ' . $written . ' bytes' );

    then execute the code with cron......
     
    krakjoe, Apr 17, 2007 IP
  10. Richie_Ni

    Richie_Ni Illustrious Member

    Messages:
    10,721
    Likes Received:
    1,175
    Best Answers:
    0
    Trophy Points:
    410
    #10
    Thank you,Joe.

    But how to execute it?I mean what command to type in while setting up the cron job?
     
    Richie_Ni, Apr 17, 2007 IP
  11. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #11
    php /fille/filesystem/path/to/file.php
     
    krakjoe, Apr 17, 2007 IP
  12. chilli_source

    chilli_source Active Member

    Messages:
    58
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    #12
    yeh i use the backup functions in WHM to do my site backups, which works great.. and then have a small shell script which copies those backup archives to a remote ftp
     
    chilli_source, Apr 17, 2007 IP
  13. Richie_Ni

    Richie_Ni Illustrious Member

    Messages:
    10,721
    Likes Received:
    1,175
    Best Answers:
    0
    Trophy Points:
    410
    #13
    I've cpanel too.....they can backup your sites automatically?
    Did you set up a cron job?
     
    Richie_Ni, Apr 18, 2007 IP
  14. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #14
    If you have access to root whm or your server admin has allowed you access to backup functions then yes you can use whm directly to backup your accounts automatically.
     
    krakjoe, Apr 18, 2007 IP
  15. hmc

    hmc Peon

    Messages:
    354
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Why not download custom script from cpanel forums? -there are many solutions(scripts) of this problem allready.
     
    hmc, Apr 19, 2007 IP
  16. Richie_Ni

    Richie_Ni Illustrious Member

    Messages:
    10,721
    Likes Received:
    1,175
    Best Answers:
    0
    Trophy Points:
    410
    #16
    Can you please tell me the url?
     
    Richie_Ni, Apr 19, 2007 IP
  17. eddy2099

    eddy2099 Peon

    Messages:
    8,028
    Likes Received:
    568
    Best Answers:
    0
    Trophy Points:
    0
    #17
    eddy2099, Apr 19, 2007 IP
    Artisan likes this.
  18. chilli_source

    chilli_source Active Member

    Messages:
    58
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    #18
    http://forums.cpanel.net/
    you'll need to register for access, but its an invaluable source for anyone using cpanel/whm :)
     
    chilli_source, Apr 19, 2007 IP