1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
   | #!/bin/php 
 
 
<?php
 
 
/* Backup a whole website and one database */
 
 
// General settings:
$to = '@gmail.com';											// Where to send the emails?
$zippath = '/home/domaine/public_html/backup/filesender/tmp';		// Path to temporarily save backup file - Select an empty directory
$piecesize = 9 * 1024 * 1024;												// In Megabyte - Gmail doesn't allow attachments more than 10 MB.
 
 
// Domain settings:
$backupsite = 'yes';						// Want to backup your site? 'yes' or 'no'
$sitepath = '/home/domaine/public_html';	// Which directory to backup? Don't add trailing slash
$zipfile = 'domaineZIP';						// backup file name - not too long name (DON'T INCLUDE ZIP EXTENTION, GMAIL MAY DELETE IT IF EXE INSIDE)
$deletezipsite = 'no';						// Do you want to delete the zip file in the tmp directory after sending emails? 'yes' or 'no'
 
 
// Database settings:
$backupdb = 'yes';					// Want to backup a database? 'yes' or 'no'
$dbhost = 'localhost';				// Server address of your MySQL Server
$dbuser = 'domaine_dbuser';		// Username to access MySQL database
$dbpass = '****';		// Password to access MySQL database
$dbname = 'domaine_DB';		// Database Name
$deletezipdb = 'no';				// Do you want to delete the zip file in the tmp directory after sending emails? 'yes' or 'no'
 
// Sender email account settings:
$from = '@backup.domaine.info';				// Who should the emails be sent from?
$mailserver = 'box.domaine.com';							// SMTP server address
$mailport = '465';												// SMTP server port address
$mailencryption = 'ssl';										// SMTP server encryption protocol
$smtpuser = '@domaine.info'; 			// SMTP server username
$smtpass = '*****';										// SMTP server password
 
 
// ******************************
// Do not Modify below this line!
// ******************************
 
 
echo "FileSender.php script started on " . date("l j F Y") . " - " . date("G:i:s") . br();
echo " ".br();
 
 
// Create the tmp directory
	if(!file_exists($zippath)) {
		if(mkdir($zippath)) {
			echo "Created target folder $zippath".br();
		}
	}
// Delete any previous file from tmp directory
exec("rm -r -f $zippath/*");
 
require_once 'lib/swift_required.php';
 
// Backup the site
if ($backupsite == "yes") {
	$zipname = $zippath . "/" . $zipfile . ".zip";
	echo "Backup of the site just started $sitepath".br();
	echo " ".br();
	echo "Zipping directory: $sitepath".br();
	passthru("nice -n 16 zip -q -r $zipname $sitepath ");
 
	if (file_exists($zipname)==false){
	exit ("<h4><center><font color=\"#FF0000\">Server failed to zip $sitepath! <br><br>Hint: Make sure $zippath directory is CHOMD 777 and $sitepath path exists.<br>Quiting... :( </font></center></h4>") ;
	}
 
	$EstimateSplittedFiles = intval(exec("zipsplit -t -n $piecesize -b $zippath $zipname"));
 
	if ($EstimateSplittedFiles<=0){
	exit ("<h4><center><font color=\"#FF0000\">Server failed to split zip file $zipname! <br>Try to increase desired spilt size in \$piecesize variable.<br>Quiting... :( </center></h4>") ;
	}
 
	echo "Estimated number of splitted files: " . $EstimateSplittedFiles .br();
	echo "Splitting $zipname into $piecesize Mb files ".br();
	exec("nice -n 16 zipsplit -n $piecesize -b $zippath $zipname");
 
	$i = 1;
	while (file_exists($zippath . "/" . $zipfile . FormatFileNumber($i) . ".zip")) {
					rename("$zippath/$zipfile" . FormatFileNumber($i) . ".zip", "$zippath/$zipfile" . FormatFileNumber($i));
					$tmpFileName = "$zippath/$zipfile" . FormatFileNumber($i);
 
					echo "Processing: $tmpFileName". br();
 
					// Swift mail
					$senddate = date("l j F Y");
					$mailsubject = "WEB Backup - $senddate - " . date("G:i:s") . " - Part " . FormatFileNumber($i) . " / " . $EstimateSplittedFiles ; // Subject in the email to be sent
					$mailbody = "Your web directory backup is attached to this email"; // Brief Message
					$transport = Swift_SmtpTransport::newInstance()
						->setHost($mailserver)
						->setPort($mailport)
						->setEncryption($mailencryption)
						->setUsername($smtpuser)
						->setPassword($smtpass);
					$mailer = Swift_Mailer::newInstance($transport);
					$message = Swift_Message::newInstance($mailsubject)
						->setFrom($from)
						->setTo($to)
						->setBody($mailbody);
					$attachment = Swift_Attachment::fromPath($tmpFileName, 'application/zip');
					$message->attach($attachment);
					$result = $mailer->send($message);
 
					echo "Just Sent: $tmpFileName".br();
					exec("rm -r -f $tmpFileName");
					sleep(7);
					$i++;
 
					flush;
	}
 
	if ($deletezipsite == "yes") {
		echo " ".br();
		echo "$zipname deleted".br();	
		exec("rm -r -f $zipname");
		}
	echo " ".br();
	echo "Finished to send all emails of the backup.".br();	
}
 
// Backup the database
if ($backupdb == "yes") {
	echo " ".br();
	echo " ".br();
	echo "Backup of the database $dbname just started".br();
	echo " ".br();
	$zipdbname = $zippath . "/" . $dbname . ".zip";
	echo "Zipping database: $dbname".br();
	passthru("mysqldump --host=$dbhost --user=$dbuser --password=$dbpass $dbname | gzip -c > $zipdbname");
	$buffer = 1024;
	$current = 0;
	$splitnum = 1;
 
	echo "Estimated number of splitted files: " . ceil(filesize($zipdbname)/$piecesize) .br();
 
	if(!$handle = fopen($zipdbname, "rb")) {
		exit("Unable to open $zipdbname for read! Make sure you edited filesender.php correctly!".br());
	}
 
	$base_filename = basename($zipdbname);
	$piece_name = $zippath.'/'.$base_filename.'.'.str_pad($splitnum, 3, "0", STR_PAD_LEFT);
 
	if(!$fw = fopen($piece_name,"w")) {
		exit("Unable to open $piece_name for write. Make sure target folder is writeable.".br());
	}
 
	echo "Splitting $base_filename into $piecesize Mb files ".br();
	echo "Writing $piece_name".br();
	while (!feof($handle) and $splitnum < 999) {
		if($current < $piecesize) {
			if($content = fread($handle, $buffer)) {
				if(fwrite($fw, $content)) {
					$current += $buffer;
					} else {
						exit("filesender.php is unable to write to target folder. Target folder may not have write permission! Try chmod +w target_folder".br());
					}
				}
			} else {
				fclose($fw);
 
				// Swift mail
				$senddate = date("l j F Y");
				$mailsubject = "Database Backup - $senddate - " . date("G:i:s") . " - Part " . FormatFileNumber($splitnum) .  " / " . ceil(filesize($zipdbname)/$piecesize) ; // Subject in the email to be sent
				$mailbody = "Your database backup is attached to this email"; // Brief Message
				$transport = Swift_SmtpTransport::newInstance()
					->setHost($mailserver)
					->setPort($mailport)
					->setEncryption($mailencryption)
					->setUsername($smtpuser)
					->setPassword($smtpass);
				$mailer = Swift_Mailer::newInstance($transport);
				$message = Swift_Message::newInstance($mailsubject)
					->setFrom($from)
					->setTo($to)
					->setBody($mailbody);
				$attachment = Swift_Attachment::fromPath($piece_name, 'application/zip');
				$message->attach($attachment);
				$result = $mailer->send($message);
 
				echo "Just Sent: $piece_name".br();
				exec("rm -r -f $piece_name");
				sleep(7);
 
				$current = 0;
				$splitnum++;
				$piece_name = $zippath.'/'.$base_filename.'.'.str_pad($splitnum, 3, "0", STR_PAD_LEFT);
				echo "Writing $piece_name".br();
				$fw = fopen($piece_name,"w");
					}
			}
	fclose($fw);
 
	// Swift mail
	$senddate = date("l j F Y");
	$mailsubject = "Database Backup - $senddate - " . date("G:i:s") . " - Part " . FormatFileNumber($splitnum) .  " / " . ceil(filesize($zipdbname)/$piecesize) ; // Subject in the email to be sent
	$mailbody = "Your database backup is attached to this email"; // Brief Message
	$transport = Swift_SmtpTransport::newInstance()
		->setHost($mailserver)
		->setPort($mailport)
		->setEncryption($mailencryption)
		->setUsername($smtpuser)
		->setPassword($smtpass);
	$mailer = Swift_Mailer::newInstance($transport);
	$message = Swift_Message::newInstance($mailsubject)
		->setFrom($from)
		->setTo($to)
		->setBody($mailbody);
	$attachment = Swift_Attachment::fromPath($piece_name, 'application/zip');
	$message->attach($attachment);
	$result = $mailer->send($message);
 
	echo "Just Sent: $piece_name".br();
	exec("rm -r -f $piece_name");
	sleep(7);
 
	fclose($handle);
 
	if ($deletezipdb == "yes") {
		exec("rm -r -f $zipdbname");
		echo " ".br();
		echo "$zipdbname deleted".br();
	}
	echo " ".br();
	echo "Finished to send all emails of the database.".br();	
	exit;
}
 
 
// Functions	
function FormatFileNumber($num)
{	global $EstimateSplittedFiles;
	return sprintf("%0" . strlen($EstimateSplittedFiles) . "d", $num);
}
 
function br()
{	return (!empty($_SERVER['SERVER_SOFTWARE']))?'<br>':"\n";
}
 
 
?> | 
Partager