Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > Programming Articles
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools
Site and mySQL backups via cron
Jase2
Join Date: Dec 2007
Posts: 1,575

 

USA
Show Printable Version Email this Page Subscription
Jase2 Jase2 is offline 02-02-2008, 10:00 PM

Site Backup via Cron:

Code:
<?
$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:
<?
$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:
<?
$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
?>

All these scripts should have the .php extension, and the file should have 755 permissions.

Don't forget to change the first few variables to the beginning of the scripts to match your site details. Also note, that you will need to set-up a cron job via your 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).

That's it, enjoy!

Regards Jason
Reply With Quote
  #2  
Old 07-04-2008, 03:17 PM
tazzarkin tazzarkin is offline
 
Join Date: Nov 2007
Posts: 137
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thank you for this. I don't understand why no one ever posted a thank you. Well, anyways, this will help a lot.
Reply With Quote
  #3  
Old 07-15-2008, 07:15 PM
kp^ kp^ is offline
 
Join Date: Mar 2008
Posts: 11
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Yes, thank you!
Reply With Quote
  #4  
Old 07-30-2008, 04:20 PM
Markowitch Markowitch is offline
 
Join Date: Nov 2005
Posts: 17
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

How do you close your board during the backup?
Reply With Quote
  #5  
Old 01-14-2009, 04:02 AM
jkcerda jkcerda is offline
 
Join Date: Mar 2008
Posts: 425
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

coooooool
Reply With Quote
  #6  
Old 03-08-2009, 02:13 PM
Jasem's Avatar
Jasem Jasem is offline
 
Join Date: Feb 2006
Location: www.menokia.com
Posts: 594
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Excellent, thank you!
Reply With Quote
  #7  
Old 05-16-2009, 09:28 PM
sheppardzwc sheppardzwc is offline
 
Join Date: Dec 2008
Location: South Carolina
Posts: 104
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Just to bump, THANKS! This is exactly what I was looking for.

Just a question -- how can I fix this?:

will@rodan:/tth/cron$ php backup.php

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 474195519 bytes) in /tth/cron/backup.php on line 20
Reply With Quote
  #8  
Old 05-18-2009, 07:10 AM
dbembibre's Avatar
dbembibre dbembibre is offline
 
Join Date: Sep 2004
Location: Madrid (Spain)
Posts: 93
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by sheppardzwc View Post
Just to bump, THANKS! This is exactly what I was looking for.

Just a question -- how can I fix this?:

will@rodan:/tth/cron$ php backup.php

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 474195519 bytes) in /tth/cron/backup.php on line 20
Increase memory_limit in php.ini
Reply With Quote
  #9  
Old 05-21-2009, 02:29 AM
ubcforums ubcforums is offline
 
Join Date: Apr 2009
Location: clouds
Posts: 83
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

thanks..this saves lot of time
Reply With Quote
  #10  
Old 05-22-2009, 01:07 PM
squishi squishi is offline
 
Join Date: May 2006
Location: Frankfurt
Posts: 282
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks for the code.
Though, a mysql backup by email? I rather not receive 1GB of email attachments. :-)
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 01:08 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.06666 seconds
  • Memory Usage 2,294KB
  • Queries Executed 23 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (3)bbcode_code
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (9)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete