SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist vhogarth's Avatar
    Join Date
    Nov 2003
    Location
    Taxachussets
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    creating cron job script

    I want to implement a cron job to dump my database everynight at like 2am and have the filename include the time/date. How do i go about doing so. I have no experience with cron jobs. Can someone hook a brotha up? If you've implemented one, can you share? If not, can you point me to where i can get more info? Thanks.

  2. #2
    Mal Reynolds Mandibal's Avatar
    Join Date
    Aug 2003
    Location
    Columbus
    Posts
    718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If no one responds with help by tomorrow morning (U.S. eastern time) I have exactly what your looking for at work on my development computer. I'll post what I have then. I'm not a shell scripting guru and can't recall the specifics otherwise I'd help ya out right now.
    Erh

  3. #3
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    Regina, SK
    Posts
    318
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Start by writing the script (it can be bash, php, perl, whatever your familiar with). The simply create a cron entry that calls the script.


    To run every day at 2am you would want a cron entry like:

    02 * * * * * /path/to/yourscript

    Have a look at mysqldump, its really easy to run a shell command like:

    mysqldump -u username -ppassword database > somefile.txt

    Which will echo the results of your command to STDOUT.

    So, you could do something like:

    PHP Code:
    #!/usr/bin/php -q  // Assuming this is where you php executable is
    $filename =  '/full/path/tofile/' date('d-m-Y') . '.sql';
    $cmd = `mysqldump -u username -ppassword database > $filename`; 
    Make that script executable (chmod +x yourscript) and that should do it.
    Reduce or eliminate spam on your sites without CAPTHCAs
    - maybe it is possible: http://formantispam.thekerrs.ca/

  4. #4
    SitePoint Wizard HarryR's Avatar
    Join Date
    Dec 2004
    Location
    London, UK
    Posts
    1,376
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To run every day at 2am you would want a cron entry like:

    02 * * * * * /path/to/yourscript
    That would run every hour at 2 minutes past, to run at 02:01am every day you'd use:
    Code:
    01 02 * * * /command/whatever

  5. #5
    SitePoint Evangelist vhogarth's Avatar
    Join Date
    Nov 2003
    Location
    Taxachussets
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by nos
    Start by writing the script (it can be bash, php, perl, whatever your familiar with). The simply create a cron entry that calls the script.


    To run every day at 2am you would want a cron entry like:

    02 * * * * * /path/to/yourscript

    Have a look at mysqldump, its really easy to run a shell command like:

    mysqldump -u username -ppassword database > somefile.txt

    Which will echo the results of your command to STDOUT.

    So, you could do something like:

    PHP Code:
    #!/usr/bin/php -q  // Assuming this is where you php executable is
    $filename =  '/full/path/tofile/' date('d-m-Y') . '.sql';
    $cmd = `mysqldump -u username -ppassword database > $filename`; 
    Make that script executable (chmod +x yourscript) and that should do it.

    I'll have to give that a try tomorrow when I have my wits about me, im a lil messed up right now (liquor) so i dont even want to attempt something i havent done before. Thanks for the feedback.

  6. #6
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    Regina, SK
    Posts
    318
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whoops, nice catch HarryR. Glad you caught that, would have hated to have passed on bad information to someone.
    Reduce or eliminate spam on your sites without CAPTHCAs
    - maybe it is possible: http://formantispam.thekerrs.ca/


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
  •