Jump to content

Recommended Posts

Everyone should be backing up their own websites. Remember your data is your responsibility. But how should you backup? What should you backup? Why should you backup?

 

Many people will tell you different ways to backup you web site. This is just one way, there are others.

 

How and what to backup?

 

If you run a static web site, any time you make a change you should make a backup and download it to your machine or FTP it to another server than the one your site is located on. I recommend running a full backup of your site from cPanel. Yes, you can do a home backup and would be able to restore it yourself but you will probably never need the full backup unless there was a problem beyond the control of TCH.

 

To restore a full backup you would upload your backup into your web space and open a ticket with the help desk and ask them to restore it for you.

 

You can restore a home backup yourself through cPanel but the home backup does not backup your databases, email aliases and other things. So if you choose to use the home backup be sure to make a home backup of your alias files, cron jobs, etc...

 

If you are running a database driven site you should backup your databases every day. You can use a script called dbsender (see below) that can be run with a cron job to dump your database, zip it up and email it to yourself. The intructions below can be used to set it up.

 

The FTP functions do not work in dbsender but the email function works flawlessly. The FTP doesn't work because TCH does not have ncftpput on the servers.

 

To configure dbsender you only need to be concerned with the following. Just make the bold changes in your copy.

 

// configure your database variables below:

$dbhost = 'localhost'; // Server address of your MySQL Server

$dbuser = 'yourcpanelname_dbuser'; // Username to access MySQL database

$dbpass = 'dbuserpassword'; // Password to access MySQL database

$dbname = 'yourcpanelname_dbname'; // Database Name

 

// Optional Options You May Optionally Configure

 

$use_gzip = "yes"; // Set to No if you don't want the files sent in .gz format

$remove_sql_file = "yes"; // Set this to yes if you want to remove the .sql file after gzipping. Yes is recommended.

$remove_gzip_file = "yes"; // Set this to yes if you want to delete the gzip file also. I recommend leaving it to "no"

 

// Configure the path that this script resides on your server.

 

$savepath = "/home/yourcpanelname/dbsender"; // Full path to this directory. Do not use trailing slash!

 

$send_email = "yes"; // Do you want this database backup sent to your email? Fill out the next 2 lines

$to = "you@a-valid-domain.com"; // Who to send the emails to - you can also use a GMail address

$from = "you@a-valid-domain.com"; // Who should the emails be sent from?

 

$senddate = date("j F Y");

 

$subject = "MySQL Database Backup - $senddate"; // Subject in the email to be sent. - you can change this

$message = "Your MySQL database has been backed up and is attached to this email"; // Brief Message. - you can change this

 

That is all the editing you need to do. If you need to backup mutliple databases with different dbusers and passwords you can rename the script.

 

To install on TCH and use do the following:

  1. Create a folder in your /home/cpanelname folder called dbsender
  2. Change permissions of dbsender folder to 770
  3. Upload edited dbsender.php script to dbsender folder you created
  4. Go into cPanel
  5. Select Cron Jobs - use the standard interface
  6. Command /usr/bin/php -q /home/yourcpanelname/dbsender/dbsender.php - or change the file name dbsender.php to what ever you renamed it
  7. Select 0 in the minute box - or choose a different minute of the hour to run the script
  8. Select 2 in the hour box - or choose a different hour to run the script
  9. Select Every Day in the days box
  10. Select Every Month in the month box
  11. Select Every Weekday in the last box
  12. Save Cron Job

You're done.

 

Why should you backup?

 

As stated above, your data is your responsibility. Yes, TCH does make weekly backups of the servers but this is more for the protection of the entire server in case of a complete failure of the hardware.

 

Take the time to implement a backup strategy and you will rest easy.

dbsender.zip

Link to post
Share on other sites
  • 3 weeks later...

Many folks have asked how to remotely backup a single directory to a remote server. We have created a script that can be used to email (not recommended) or remote ftp a directory from your site to a remote server.

 

Note: This script is designed to be run from a cron job. Not a browser.

 

><?php
// +----------------------------------------------------------------------+
// | PHP Version 4														|
// +----------------------------------------------------------------------+
// | Copyright © 2006 Bruce Richards / Andy Beckett					 |
// |																	  |
// | Purpose: To backup a folder structure to a zip file and then FTP	 |
// |		  the file to a remote server								 |
// |																	  |
// | Requires:  zip be installed on the server and that you are able	  |
// |			to use the passthru command of PHP, the TCH servers	   |
// |			do have zip installed and passthru can be used.		   |
// |																	  |
// | WARNING: Depending on what you are backing up the files can be quite |
// |		  large. Be sure you have enough space for what you are	   |
// |		  backing up. Also, sending huge files via email is not	   |
// |		  recommended.												|
// |																	  |
// +----------------------------------------------------------------------+
// | Author: Bruce Richards / Andy Beckett								|
// +----------------------------------------------------------------------+
//

// setup variables
$remove_file = "yes";  // remove file when done -- recommended!

$path= '/home/your-account/public_html/';  // Full path to backup

$zipfile = "your.zip";  // backup file name
$zippath = "/home/your-account/backups";  // path to save backup file 

$send_email = "no";  // Do you want this backup sent to your email? Fill out the next 2 lines
$to	  = "you@yourdomain.ext";  // Who to send the emails to
$from	= "backup@yourdomain.ext"; // Who should the emails be sent from?

$senddate = date("j F Y");

$subject = "Backup - $senddate"; // Subject in the email to be sent.
$message = "Your backup is attached to this email"; // Brief Message.

$use_ftp = "yes"; // Do you want this backup uploaded to an ftp server? Fill out the next 4 lines
$ftp_server = "localhost"; // FTP hostname
$ftp_user_name = ""; // FTP username
$ftp_user_pass = ""; // FTP password
$ftp_path = "/"; // This is the path to upload on your ftp server!

// Do not Modify below this line! 

$date = date("mdy-hia");
$zipname = "$zippath/$date-$zipfile";
passthru("nice -n 16 zip -q -r $zipname $path ");

$filename2 = "$zipname";
  
if($send_email == "yes" ){
	$fileatt_type = filetype($filename2);
	$fileatt_name = "".$date."-".$zipfile."";
	
	$headers = "From: $from";
	
	// Read the file to be attached ('rb' = read binary)
	$file = fopen($filename2,'rb');
	$data = fread($file,filesize($filename2));
	fclose($file);

	// Generate a boundary string
	$semi_rand = md5(time());
	$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

	// Add the headers for a file attachment
	$headers .= "\nMIME-Version: 1.0\n" ."Content-Type: multipart/mixed;\n" ." boundary=\"{$mime_boundary}\"";

	// Add a multipart boundary above the plain message
	$message = "This is a multi-part message in MIME format.\n\n" ."--{$mime_boundary}\n" ."Content-Type: text/plain; charset=\"iso-8859-1\"\n" ."Content-Transfer-Encoding: 7bit\n\n" .
	$message . "\n\n";

	// Base64 encode the file data
	$data = chunk_split(base64_encode($data));

	// Add file attachment to the message
	$message .= "--{$mime_boundary}\n" ."Content-Type: {$fileatt_type};\n" ." name=\"{$fileatt_name}\"\n" ."Content-Disposition: attachment;\n" ." filename=\"{$fileatt_name}\"\n" ."Content-Transfer-Encoding: base64\n\n" .
	$data . "\n\n" ."--{$mime_boundary}--\n";

	// Send the message
	$ok = @mail($to, $subject, $message, $headers. "-f" .$from);
	if ($ok) {
		echo "<h4><center>Backup created and sent! File name $filename2</center></h4>";
	} else {
		echo "<h4><center>Mail could not be sent. Sorry!</center></h4>";
	}
}

if($use_ftp == "yes"){
	$conn = ftp_connect($ftp_server);
	if(!$conn) {
		exit("Could not connect to server: $ftp_server\n");
	}

	if(!ftp_login($conn,$ftp_user_name,$ftp_user_pass)) {
		ftp_quit($conn);
		exit("Could not log in\n");
	}

	ftp_chdir($conn,$ftp_path);

	$remotefile = "".$date."-".$zipfile."";

	if(!ftp_put($conn, $ftp_path.$remotefile, $filename2, FTP_BINARY)) {
			echo "Could not upload $filename2\n";
	}

	ftp_quit($conn);
	echo "<h4><center>$filename2 Was created and uploaded to your FTP server!</center></h4>";

}

if($remove_file=="yes"){
	exec("rm -r -f $filename2");
}
?>

 

Copy this and create a PHP file, upload it to your server and create a cron job to run it or you can run it from your web browser.

 

Don't forget this could create huge files and I could not get a 22MB file to email to GMail but smaller zip files emailed just fine. The FTP function works just fine with large files.

Link to post
Share on other sites
Guest
This topic is now closed to further replies.
×
×
  • Create New...