I wrote the following script, but it seems like the tar file won't attach.

<?php
// Directory to compress and email
$directory = "/home/test/public_html/2.0.0/";

// Email from
$from = "backups@test.org";

// Email to
$to = "webmaster@test.org";

// File Name
$name = "Backup";

// Email Subject
$subject = "Site Backup";

// Email Message
$message = "main directory backup";

/* *************************
DO NOT EDIT BELOW THIS LINE
****************************/

// Add more details to the file name
$name .= " - ".date("n/d/Y @ g:i:s a");

// Create the command
$cmd = "tar -czvf ".$file.".tgz ".$directory;

// Create the tar
$exec = exec($cmd);

// Create the headers
$pathinfo = pathinfo("/");
$fileatt = $pathinfo['dirname'].$name;           
$fileatt_type = "application/octet-stream"; // File Type $headers = "From: ".$from; // Read the file $file = fopen($fileatt,'rb'); $data = fread($file,filesize($fileatt)); fclose($file); // Write out the headers $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; $message .= "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type:text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; $data = chunk_split(base64_encode($data)); $message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"{$name}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; // Email the tar mail($to,$subject,$message,$header) or die("Cannot email backup!"); ?>

What did I do wrong?

    1. Don't write your own MIME encoder, use an existing tested one
    2. Don't send backups by email, it isn't secure
    3. Don't send backups by email, they will become too large for it
    4. Even if you did want to do this, ignoring my previous advice, don't do it via PHP (at least, not via the web)

    Mark

      MarkR wrote:

      1. Don't write your own MIME encoder, use an existing tested one
      2. Don't send backups by email, it isn't secure
      3. Don't send backups by email, they will become too large for it
      4. Even if you did want to do this, ignoring my previous advice, don't do it via PHP (at least, not via the web)

      Mark

      What language should I do it in? How would I go about doing it? I have a gmail account so I can handle attachements of up to 30 mb...so they can split archived.

      Any advice?

        Run it in a shell script on cron; use a secure file transfer method (e.g. scp, sftp) to download the files to your backup box.

        PHP should not generally be used for this sort of thing, particularly on the web. The script may time out or otherwise misbehave, particularly with the level of error checking in evidence above (i.e. none)

        cron should also be configured to mail you the output so you can tell if something goes wrong.

        Mark

          How would I write the shell script? What if I don't have shell access?

            If you don't have shell access then you can't make backups easily. Get a better provider.

            Not having shell access makes hosting unusable.

            Mark

              Why would you need to back up your entire live site on a regular basis anyway? Accumulated data, sure; but wouldn't the site itself be stored/maintained somewhere else and the live site copied from that?

                Why would you need to back up your entire live site on a regular basis anyway? Accumulated data, sure; but wouldn't the site itself be stored/maintained somewhere else and the live site copied from that?

                And doesn't the host provide backup facilities anyway? If they don't then that's another reason to get a different host.

                  Write a Reply...