syrinx75 | Bonjour,
Voici un script php pour exécuter en php la sauvegarde zip de votre site et d'une base de données (séparément). Les sauvegardes sont envoyées par email.
Pensez à éditer les premières lignes du script! C'est basé sur la version 4.0.6 de Swift mailer. A mettre dans un sous répertoire /lib
Entre autres,
Vous devez indiquer la taille des morceaux de sauvegarde qui sont envoyés dans les emails. Sachant que Googlemail a une limite à 9 Mo pour les pièces attachées.
Vous pouvez utiliser le SSL pour l'envoi de l'email si nécessaire.
Vous avez la possibilité de conserver l'archive .zip sur le site ou de l'effacer.
Vous pouvez le lancer en automatique avec cron.
J'espère que ça vous sera aussi utile qu'à moi.
Code :
- #!/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";
- }
-
-
- ?>
|
Message édité par syrinx75 le 06-03-2011 à 09:11:36
|