Automating Database Backups

edited December 2011 in Feature Requests
All,

I wanted to get everyone's feedback on automating database backups. I am in the process of developing the procedure for how to backup my growing number of Fuel sites and it would help if a few things that are cloudy for me right now could be explained:

1) First, which option is better? Use ci_cron.php or use CI's CLI? (options stated here: http://getfuelcms.com/user_guide/modules/cronjobs)

2) When running a backup manually in the Fuel admin via the Backup module, it outputs a zip file to the data_backup folder with a date of when the backup is run in the filename. Is there a way using Cron to have a single file be overwritten each time a new backup is made rather than accumulate many many files over a certain amount of time? Or, is there any way that Fuel can aid in automating the removal of x amount of previous backup files that are not needed, say like 5 previous ones?

Thanks,
Erik

Comments

  • edited 10:34AM
    I am also a little confused as to the way the documentation writes out sample cron commands.

    On this page: http://getfuelcms.com/user_guide/modules/cronjobs under "Database Backup" the examples shown there in the code view look unfamiliar to me as a cron novice. Where would I insert this code to be run? In the Fuel Cronjob module or somewhere in the fuel/crons folder?

    What's tripping me up is the prefacing of each line with "php". Isn't it supposed to start with the numbers representing the hour, day, and days of week that the job is supposed to run?
  • edited 10:34AM
    Additional question: I just tried doing a sample cron backup job and I get the following PHP warning:

    PHP Warning: set_time_limit(): Cannot set time limit in safe mode in /var/www/vhosts/example.com/httpdocs/fuel/crons/ci_cron.php on line 72

    Noticed that PHP deprecated safe mode, so I'm not sure what to do here. Should I comment out this line?

    Erik
  • edited 10:34AM
    The examples provided show the command to be run (the "command" field in the admin interface). I would try running that via the command line (e.g. the Terminal on mac or SSH to your server and run the command) just to make sure the command will work correctly on your server environment. The "php" is part of the command. For you to properly set the cron in the interface the user that runs php (e.g. apache), will need to have the permissions to create a crontab.

    More on cron jobs:
    http://www.webmasters-central.com/article-blog/tutorials/cron-tutorial-managing-cron-tab-or-cron-job-is-easy/

    There is also a good shell script that we sometimes use if the database is big (due to memory issues using CI's database backup utility) and can sometimes be a simpler approach because you don't have to worry about the PHP user being able to run cron jobs:
    http://sourceforge.net/projects/automysqlbackup/

    You can comment out that line in the ci_cron.php file. However, I'm not sure if the admin interface for creating the cron will work in safe_mode.

    Also, we will be addressing the issue of specifying the date with the file name as well as doing cleanup of older files in an upcoming release.
  • edited 10:34AM
    Great, thanks for clearing that up. I've heard of automysqlbackup, I may look into that if these efforts fail. I will let you know how this goes.
  • edited 10:34AM
    Here's an update. I commented out line 74, which made that particular error go away, but the email still shows the following HTML which is delivered in this raw format to my email address:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Error</title> <style> html,body { margin: 20px 0 10px 0; padding: 0; } body { font: 11px Arial, Helvetica, sans-serif; text-align: center; } h1 { margin: 0; padding:0; font-family: Arial,Helvetica,sans-serif; font-size: 22px; color: #999; font-weight: normal; padding: 10px 4px 5px 0; } p { font-size: 12px; margin: 0 0 10px 0; line-height: 15px; } a { color: #690; text-decoration: none; } a:hover { color: #333; text-decoration: underline; } #error_general { width: 500px; margin: auto; border: 1px solid #ddd; padding: 0 20px 20px 20px;} </style> </head> <body> <div id="error_general"> <h1>An Error Was Encountered</h1> <p>The URI you submitted has disallowed characters.</p> </div> </body> </html>

    Looks like the error has to do with disallowed characters in the URI. Any idea where that is happening so I can fix that? Does this have to do with my cron command? Here is the command I am using just for reference:

    php /var/www/vhosts/example.com/httpdocs/fuel/crons/ci_cron.php --run=/fuel/cron/db_backup
  • edited 10:34AM
    I opened up that HTML in a browser and based on its physical appearance, it appears to be a CI error. If that helps any...
  • edited 10:34AM
    Oh, and also, I disabled safe mode and that line 74 error went away. I am still experiencing this URI error though.
  • edited 10:34AM
    I got it to work by changing my command syntax to this:

    php /var/www/vhosts/example.com/httpdocs/fuel/crons/ci_cron.php backup/cron
  • edited February 2013
    Can anyone help?

    When i try in my terminal the cron command:

    ~/htdocs/project/fuel/crons$ php ci_cron.php /fuel/my_module/my_controller/test

    i get an error

    Notice: Undefined offset: -1 in /Applications/MAMP/htdocs/toptennis/fuel/modules/fuel/config/fuel_constants.php on line 17

    and a Database error

    Unable to connect to your database server using the provided settings.

    Filename: third_party/fuel/Loader.php
    Line Number: 134


    what am I doing wrong?
  • edited 10:34AM
    I'd try adding the following to your index.php bootstrap file to set $_SERVER variables that may not be set when running it via CLI. This is a snippet from the 1.0 beta:
    if (defined('STDIN')) { /* if your FUEL installation exists in a subfolder, then you may want to change SCRIPT_NAME to /subfolder/index.php (Needed for using Tester module if running via CLI) */ $_SERVER['SCRIPT_NAME'] = 'index.php'; $_SERVER['SERVER_NAME'] = 'localhost'; $_SERVER['SERVER_PORT'] = 80; $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; $_SERVER['HTTP_HOST'] = 'localhost'; }

    Also, CI 2.0 provides CLI access via the index.php:
    cd /path/to/webfolder/ php index.php /fuel/my_module/my_controller/test
  • edited February 2013
    That didn't seem to do much unfortunately... when running the ci_cron the errors are the same. I also tried using the index.php method and I get this


    Notice: Undefined offset: -1 in /Applications/MAMP/htdocs/project/fuel/modules/fuel/config/fuel_constants.php on line 17

    Severity: Warning
    Message: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/project/fuel/modules/fuel/config/fuel_constants.php:17)
    Filename: core/Common.php
    Line Number: 418


    and again the database connection error. Any clue?
  • edited 10:34AM
    That error appears to be because $_SERVER['SCRIPT_NAME'] is not properly set. Try changing that line in the fuel_constants.php to this (which is what it is in 1.0):
    define('WEB_FOLDER', (count($_FUEL_SEGS) > 1) ? $_FUEL_SEGS[count($_FUEL_SEGS)-2] : '/');

    For the database connection issue, are you using MAMP by chance or do you have multiple versions of PHP? If so, try changing "php" command to point to another version or using the full path to the other PHP binary (e.g. /Applications/MAMP/bin/php/php5.2.17/bin/php). In my case, I have database issues with the PHP version that comes with OSX but the MAMP version does not so I use that instead.
    http://stackoverflow.com/questions/8480663/changing-the-default-php-command-path
    http://serverfault.com/questions/315022/how-to-change-php-binary-location-in-osx-command-line
  • edited 10:34AM
    That was exactly the problem!
    Solved both issues, thank you very much!!!
Sign In or Register to comment.