-1

My purpose is to automatically backup MySQL databases and the whole public_html folder into a zip file and them email it as an attachment to the webmaster(me) in a downloadable form. Something I can run as a cron.

Some research gave me scripts to backup the files but found nothing on how to mail it as attachment or even how to trigger a PHP file through a SHELL(.sh) file.

I am working on Linux server through a shared host but have allowance to crons.

Any ideas on how to do that? Some code would help quite a lot! Thanks in advance.

1 Answers1

0

If you're using Cpanel, you could proably connect using a simple PHP script, which you can trigger using cron jobs.

I'll include an example.

$auth = base64_encode("username:password"); //replace with your credentials. Make sure you don't publish them anywhere
$domain = "cpanel.mydomain.com"; //replace with your domain
$theme = "paper_lantern"; //replace with your theme
$secure = false; //if no valid ssl certificate
$ftp = true; //false if you don't want to backup to an FTP server
$ftpserver = "ftp.mydomain.com"; //replace with your FTP server for backing up to FTP
$ftpusername = "ftp username";
$ftppassword = "ftp password";
$ftpport = "21";
$ftpdirectory = "/";

if ($secure) {
    $url = "ssl://" . $domain;
    $port = 2083;
} else {
    $url = $domain;
    $port = 80;
}

$socket = fsockopen($url, $port);
if (!$socket) {
    exit("Failed to open socket connection.");
}

if ($ftp) {
    $params = "dest=ftp&server=$ftpserver&user=$ftpusername&pass=$ftppassword&port=$ftpport&rdir=$ftpdirectory&submit=Generate Backup";
} else {
    $params = "submit=Generate Backup";
}

fputs($socket, "POST /frontend/" . $theme . "/backup/dofullbackup.html?" . $params . " HTTP/1.0\r\n");
fputs($socket, "Host: $domain\r\n");
fputs($socket, "Authorization: Basic $auth\r\n");
fputs($socket, "Connection: Close\r\n");
fputs($socket, "\r\n");


while (!feof($socket)) {
    $response = fgets($socket, 4096);
}

fclose($socket);

sleep(120);

//laatste file
$path = "/home/myusername/backups"; //backup store location

$latest_ctime = 0;
$latest_filename = '';    

$d = dir($path);
while (false !== ($entry = $d->read())) {
  $filepath = "{$path}/{$entry}";
  if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
    $latest_ctime = filectime($filepath);
    $latest_filename = $entry;
  }
}
$file = "backups/" . $latest_filename;


$email_to = "Somebody <me@example.com>";
$email_from = "My Webserver <webserver@example.com>";
$email_subject = "Created a backup";
$email_txt = "<html><body>\n" . "Backup created on " . date('d/m/Y') . " at " . date("h:i:sa") . " (" . date_default_timezone_get() . "). Backup saved as " . $latest_filename . " <br>Backup is created every day." . "<br><hr><br> This mail was automatically generated. Please don't answer." . "</body></html>"; 
$fileatt = $file;
$fileatt_type = "package/x-generic";
$fileatt_name = $latest_filename;
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers="From: " . $email_from;
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_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" . $email_txt;
$email_message .= "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";

mail($email_to,$email_subject,$email_message,$headers);

In cronjobs you will create something with an action like php -f ~/backup.php

rrr
  • 412
  • 4
  • 10