Web Hosting Forum | Lunarpages

Author Topic: Site and MySQL backups via cron  (Read 120542 times)

Danielle

  • Guest
Site and MySQL backups via cron
« on: December 02, 2004, 09:41:40 AM »
The following scripts were provided from scanman20.  You can contact him either via PM on this board or via email at admin@notonebit.com

Finally, don't forget to visit his site www.notonebit.com if you like these scripts.  He has loads of other cool stuff there as well  :thumb:

Site Backup via Cron

Code: [Select]
<?
$datestamp = date("Y-m-d_H-i-s");      // Current date to append to filename of backup file in format of YYYY-MM-DD

/* CONFIGURE THE FOLLOWING VARIABLES TO MATCH YOUR SETUP */
$filename= "Full_Account_Backup-$datestamp.tar";   // The name (and optionally path) of the dump file
$ftp_server = "123.123.123.123";      // Name or IP. Shouldn't have any trailing slashes and shouldn't be prefixed with ftp://
$ftp_port = "21";   // FTP port - blank defaults to port 21
$ftp_username = "anonymous";      // FTP account username
$ftp_password = "";      // FTP account password - blank for anonymous
$filename = "/home/YOURACCOUNT/" . $filename . ".gz";

$command = "tar cvf ~/$filename ~/*";
$result = exec($command);

$command = "gzip -9 -S .gz ~/$filename";
$result = exec($command);

// set up basic connection
$ftp_conn = ftp_connect($ftp_server);

// Turn PASV mode on or off

ftp_pasv($ftp_conn, false);

// login with username and password
$login_result = ftp_login($ftp_conn, $ftp_username, $ftp_password);

// check connection
if ((!$ftp_conn) || (!$login_result))
{
   echo "FTP connection has failed.";
   echo "Attempted to connect to $ftp_server for user $ftp_username";
   exit;
}
else
{
   echo "Connected to $ftp_server, for user $ftp_username";
}

// upload the file
$upload = ftp_put($ftp_conn, "foo.tar.gz", $filename, FTP_BINARY);

// check upload status
if (!$upload)
{
   echo "FTP upload has failed.";
}
else
{
   echo "Uploaded $filename to $ftp_server.";
}

// close the FTP stream
ftp_close($ftp_conn);

unlink($filename);   //delete the backup file from the server
?>


-------------------------------
MySQL backup via cron - Emailed to You

Code: [Select]
<?
$datestamp = date("Y-m-d");      // Current date to append to filename of backup file in format of YYYY-MM-DD

/* CONFIGURE THE FOLLOWING SEVEN VARIABLES TO MATCH YOUR SETUP */
$dbuser = "";            // Database username
$dbpwd = "";            // Database password
$dbname = "";            // Database name. Use --all-databases if you have more than one
$filename= "backup-$datestamp.sql.gz";   // The name (and optionally path) of the dump file
$to = "you@remotesite.com";      // Email address to send dump file to
$from = "you@yourhost.com";      // Email address message will show as coming from.
$subject = "MySQL backup file";      // Subject of email

$command = "mysqldump -u $dbuser --password=$dbpwd $dbname | gzip > $filename";
$result = passthru($command);

$attachmentname = array_pop(explode("/", $filename));   // If a path was included, strip it out for the attachment name

$message = "Compressed database backup file $attachmentname attached.";
$mime_boundary = "<<<:" . md5(time());
$data = chunk_split(base64_encode(implode("", file($filename))));

$headers = "From: $from\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: multipart/mixed;\r\n";
$headers .= " boundary=\"".$mime_boundary."\"\r\n";

$content = "This is a multi-part message in MIME format.\r\n\r\n";
$content.= "--".$mime_boundary."\r\n";
$content.= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$content.= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$content.= $message."\r\n";
$content.= "--".$mime_boundary."\r\n";
$content.= "Content-Disposition: attachment;\r\n";
$content.= "Content-Type: Application/Octet-Stream; name=\"$attachmentname\"\r\n";
$content.= "Content-Transfer-Encoding: base64\r\n\r\n";
$content.= $data."\r\n";
$content.= "--" . $mime_boundary . "\r\n";

mail($to, $subject, $content, $headers);

unlink($filename);   //delete the backup file from the server
?>


-------------------------------
MySQL backup via cron - FTPed to You

Code: [Select]
<?
$datestamp = date("Y-m-d");      // Current date to append to filename of backup file in format of YYYY-MM-DD

/* CONFIGURE THE FOLLOWING THREE VARIABLES TO MATCH YOUR SETUP */
$dbuser = "";      // Database username
$dbpwd = "";         // Database password
$dbname = "";      // Database name. Use --all-databases if you have more than one
$filename= "backup-$datestamp.sql.gz";   // The name (and optionally path) of the dump file

$command = "mysqldump -u $dbuser --password=$dbpwd $dbname | gzip > $filename";
$result = passthru($command);

/* CONFIGURE THE FOLLOWING FOUR VARIABLES TO MATCH YOUR FTP SETUP */
$ftp_server = "";   // Shouldn't have any trailing slashes and shouldn't be prefixed with ftp://
$ftp_port = "21";            // FTP port - blank defaults to port 21
$ftp_username = "anonymous";         // FTP account username
$ftp_password = "";         // FTP account password - blank for anonymous

// set up basic connection
$ftp_conn = ftp_connect($ftp_server);

// Turn PASV mode on or off
ftp_pasv($ftp_conn, false);

// login with username and password
$login_result = ftp_login($ftp_conn, $ftp_username, $ftp_password);

// check connection
if ((!$ftp_conn) || (!$login_result))
{
   echo "FTP connection has failed.";
   echo "Attempted to connect to $ftp_server for user $ftp_username";
   exit;
}
else
{
   echo "Connected to $ftp_server, for user $ftp_username";
}

// upload the file
$upload = ftp_put($ftp_conn, $filename, $filename, FTP_BINARY);

// check upload status
if (!$upload)
{
   echo "FTP upload has failed.";
}
else
{
   echo "Uploaded $filename to $ftp_server.";
}

// close the FTP stream
ftp_close($ftp_conn);

unlink($filename);   //delete the backup file from the server
?>


Please note that all scripts should be with a .php extension, the file should have 755 permissions, and you would also need to not only change the first few variables to those of your site setup but also add a cron job in Cpanel with a path to the script such as "php /home/username/path-to-the-php-script" (without the "" and replacing path-to-the-php-script with your actual path to it).

Anything else, just post here or contact scanman (not me  :poke:) about it

Thanks  :)

Offline in_sussex

  • Newbie
  • *
  • Posts: 4
Site and MySQL backups via cron
« Reply #1 on: December 03, 2004, 03:16:11 AM »
I have been trying to set up this type of housekeeping php script but find I keeping the message

Status: 404
Content-type: text/html
X-Powered-By: PHP/4.3.9

No input file specified.

even though I have provided a full valid path of the kind afer the php

ie in the form

   /home/user/public_html/.. etc

and have set permission to 755 as advised

Am I going wrong in how I specify the path?

Thanks for any advice

Offline fretnmore

  • Grandma Looney
  • Über Jedi
  • *****
  • Posts: 2842
    • Tri Wolf Studios
Site and MySQL backups via cron
« Reply #2 on: December 03, 2004, 03:28:00 AM »
The cron job command should be

Code: [Select]
php -q /home/username/public_html/folder/name.php

where username is your username for LP
and name.php is whatever you have named the php program listed above
If you have that php program in a folder then you will also need the folder name
Life is not measured by the number of breaths we take, but by the moments that take our breath away.
----------------------------------------------------------
Tri-Wolf Studios
Lunarpages Web Hosting
Lunarpages Forums
Lunarpages Affiliate Program

Offline in_sussex

  • Newbie
  • *
  • Posts: 4
Site and MySQL backups via cron
« Reply #3 on: December 03, 2004, 04:06:43 AM »
Thankyou

What is the meaning of the -q parameter?

Offline in_sussex

  • Newbie
  • *
  • Posts: 4
Site and MySQL backups via cron
« Reply #4 on: December 03, 2004, 04:09:09 AM »
Sorry

Also do you know if there is a way to pass the equivalent of a query string on a php command line eg what is the command line equivalent of


     myprog.php?type=1000

creating the $type variable in the php script

Offline fretnmore

  • Grandma Looney
  • Über Jedi
  • *****
  • Posts: 2842
    • Tri Wolf Studios
Site and MySQL backups via cron
« Reply #5 on: December 03, 2004, 04:31:40 AM »
The -q parameter prevents PHP from outputting HTML headers by default; without it, there will be superfluous console output which cron will email you.

Now I don't know as much as that may sound like. I got the explanation from this thread which you may want to read yourself. Perhaps the answer to your second question is in there.
Life is not measured by the number of breaths we take, but by the moments that take our breath away.
----------------------------------------------------------
Tri-Wolf Studios
Lunarpages Web Hosting
Lunarpages Forums
Lunarpages Affiliate Program

Offline in_sussex

  • Newbie
  • *
  • Posts: 4
Site and MySQL backups via cron
« Reply #6 on: December 03, 2004, 06:41:26 AM »
thank you fretnmore for your kind help

Offline JamesG

  • Web Designer UK
  • Berserker Poster
  • *****
  • Posts: 11732
  • Web Designer
    • Affordable Web Design
Site and MySQL backups via cron
« Reply #7 on: December 03, 2004, 07:26:35 AM »
i cant believe this isnt a sticky :roll:

Danielle

  • Guest
Site and MySQL backups via cron
« Reply #8 on: December 03, 2004, 09:32:17 AM »
Garvey, it is in the how-tos and there isn't a point to making it a sticky.  The reason is this, if each person posting a how-to here makes their how-to a sticky then eventually there will be a bunch of stickies above this anyway which means that making it a sticky would have been a waste of time.

If you want to make it a sticky somewhere, feel free to do so.

Thanks.

Offline JamesG

  • Web Designer UK
  • Berserker Poster
  • *****
  • Posts: 11732
  • Web Designer
    • Affordable Web Design
Site and MySQL backups via cron
« Reply #9 on: December 03, 2004, 10:56:25 AM »
:oops: i didn't notice it was in a how-to section, then again, i didn't even realise LP had a how-to section.

**puts trunks on**

**dives in**

Offline skylerj

  • Trekkie
  • **
  • Posts: 15
Site and MySQL backups via cron
« Reply #10 on: January 02, 2005, 04:42:38 PM »
Hi, I'm on the galileo server, I'm trying to run "the site backup and send to ftp" script. I'm getting the exec(disabled) error. What are my options in order to set up automated backup creation and sending to an ftp server.  Thanks!

Offline BlueTicona

  • Spaceship Navigator
  • *****
  • Posts: 77
    • http://www.bluetopmedia.net
Site and MySQL backups via cron
« Reply #11 on: January 02, 2005, 05:43:09 PM »
Hey Danielle, many kudos and thanks for that.

I have cron jobs running on my computer here and in all honesty never really thought about doing one for the site.  :D  Thanks! You fixed a problem before it even happened lol.  :thumb:
http://www.DreamTune.comThe Melody of Magic & New Age!

We are a support community for pagans and their Magical & New Age lifestyles. This community is meant to be a gathering place for everyone to get together and exchange information on various Paths, Teachings & Healings.

Danielle

  • Guest
Site and MySQL backups via cron
« Reply #12 on: January 02, 2005, 08:15:25 PM »
Blueticona, you're welcome, although I just posted it in the how-to section; the main kudos should go to scanman who created the scripts  :yey:

Offline skylerj

  • Trekkie
  • **
  • Posts: 15
Site and MySQL backups via cron
« Reply #13 on: January 02, 2005, 10:01:03 PM »
May I have an answer to my question please? Despite all the security measures I've implemented on my site, I can't really rest easy until I know that backups for it are automated at least once a week. I know that this exec(disabled) is beyond my control, and the exec command is probably necessary in any automated backup script. I would really like like to know my options on the gallieo server. Is there a cron command that I could input in cpanel to get the same result?

Danielle

  • Guest
Site and MySQL backups via cron
« Reply #14 on: January 02, 2005, 10:08:14 PM »
Hi skylerj,

I am not certain why exec() would be required to do a backup at all.  Please provide the exact error message you are receiving and a copy of the script you yourself are using with all settings other than any password (asterick it out).

I did not see your question, and there was no intention on anyone's part to overlook it.

We appreciate the detailed information being provided to help assist you in this matter.

Thanks.

 

Share |