SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict khuramyz's Avatar
    Join Date
    Oct 2005
    Location
    Manchester, UK
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    cron job mysql dump

    Hi friends

    I have created a file to make the DB Backups. I have run it through the browser and its running fine. But when I run it through the cron then it starts giving error.
    The script is basically,

    PHP Code:
    $today date('Y-m-d');

    mkdir('./db_backups/'.$today0777);

    $sql "select hostname, dbname, username, pass from xxxxxx";
    $rs $DBConn->query($sql);
    while(
    $objPartner $DBConn->fetchNextObject($rs))
    {
        
    set_time_limit(500);
        
    // Enter Database access details
        
    $host$objPartner->hostname;
        
    $user$objPartner->username;
        
    $pass$objPartner->pass;
        
    $db=   $objPartner->dbname;

      
    // Instructing the system to zip and store the database
      
    system(sprintf(
        
    'mysqldump --opt -h%s -u%s -p%s %s | gzip > ./db_backups/%s/%s.sql.gz',
        
    $host,
        
    $user,
        
    $pass,
        
    $db,
        
    $today,
        
    $db
      
    ));
      echo 
    '+DONE';


    The error i recieve in the cron email repeatedly is
    Code:
    sh: /db_backups/2008-11-19/xxxxx.sql.gz: No such file or directory
    mysqldump: Got errno 32 on write
    +DONE
    I have tried to look up the errno 32 and its about Broken pipe.
    Which is kind of crazy as I have put all the permissions right. I have put the full rwx writes on the cron script as well db_backups has a 777 right.
    Please help me out

    Regards
    Khuram Javaid
    PHP Developer and Entrepreneur
    http://www.phprad.com/

  2. #2
    SitePoint Zealot smadeira's Avatar
    Join Date
    Oct 2003
    Location
    Pennsylvania
    Posts
    192
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are potential issues with ownership of the files, etc. that could be causing you a problem. Try touch() on the filename first to create the file and then the mysqldump to populate the file.
    Scott

  3. #3
    SitePoint Addict khuramyz's Avatar
    Join Date
    Oct 2005
    Location
    Manchester, UK
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok put the touch before the system command.
    lets see what happens as the next cron runs at every 15 minute.
    Khuram Javaid
    PHP Developer and Entrepreneur
    http://www.phprad.com/

  4. #4
    SitePoint Addict khuramyz's Avatar
    Join Date
    Oct 2005
    Location
    Manchester, UK
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Touched the file but still no effect. any other suggestions.
    Works in the browser but not in cron.
    Khuram Javaid
    PHP Developer and Entrepreneur
    http://www.phprad.com/

  5. #5
    SitePoint Addict khuramyz's Avatar
    Join Date
    Oct 2005
    Location
    Manchester, UK
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Seems like the error is in line mkdir()
    its giving out the error Permission Denied.
    Even when I try to run this script through the browser, it creates the directory.
    Any help ?
    Khuram Javaid
    PHP Developer and Entrepreneur
    http://www.phprad.com/

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When running a script through cron, you're usually using the command line interface to php. There are some differences. Notable ones are it may use a different php.ini file at a different location and different settings, and even be a different version of php. It's simply a different php install.

    Second, the CLI doesnt set php's current working dir to that of the script being executed. This means relative file paths aren't relative to the directory you think they are. If you want to mimic the working directory behavior of running php through the webserver, you can do this in the script which gets initally called by the command.
    PHP Code:
    // top of script
    chdir(dirname(__FILE__)); 
    Read
    http://www.php.net/manual/en/features.commandline.php

    And yes, the script likely runs as a different user compared to running it through the webserver. You can check by using getmyuid() getmygid() to help you work out proper permissions.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •