• home
  • forum
  • my
  • kt
  • download
  • Cronjobs: A Full Introduction. (Weekly database backup script included.)

    Author: 2007-09-06 14:46:00 From:

    Cron jobs are a way of automating repetitive tasks using the Cron tab in CPanel. The beauty of it is that it lets you run whatever script (php, asp etc.) and whatever time you want - automatically. Not only does this save time, but also a great deal hassle. I'm sure you'll agree how annoying it is backing all your databases every sunday afternoon.

    Unlike other cron tutorials, I will give you the basics and just give you list a cron commands, which you don't even need! With CPanels GUI, setting up and using cron jobs is like stealing cnady from a baby.

    Examples of use of cron jobs

    -- Database backups
    -- Sending weekly updates (via email)
    -- Notifying members when they have unread messages on a forum
    -- Checking affiliates
    -- Basically anything which is repeated

    There are lot of examples, but that's all can think of at the moment.

    Setting up your first cron job

    Firstly, all scripts MUST be placed above public_html - otherwise they may be tampered by users!! Therefore, their path should something like this:
    /home/username/backup.php
    and NOT
    /home/username/public_html/backup.php


    Secondly, you need your script. For this example, I'm using this script (www.somecoders.com) I found for backing up databases:
    Save this to backup.php.
    <?
    #!/usr/local/bin/php -q
    function get_database_file($database_name, $domain, $username, $password)
    {
    //Construct the URL out of all our little pieces
    $url = 'http://' . $username . ':' . $password .'@'. $domain .'/getsqlbackup/'. $database_name .'.gz';
    //Return the file's contents
    return(file_get_contents($url));
    }

    function send_database_file($to, $from, $subject, $message)
    {
    global $database_name, $domain, $username, $password;
    //Thanks to http://www.theukwebdesigncompany.com/articles/php-file-attachments.php for making this easy
    $unique_sep = md5(uniqid(time()));
    //Tell our E-mail client that we're sending both a message, and an attachment
    $headers .= "From: $from\n".
    "MIME-Version: 1.0\nContent-Type: multipart/mixed;boundary=\"$unique_sep\";\n".
    "charset=\"iso-8859-1\"\nContent-Transfer-Encoding: 7bit\n\n" .
    "--$unique_sep\n".
    "Content-Type: text/plain; charset=\"iso-8859-1\"\n".
    "Content-Transfer-Encoding: 7bit\n\n".
    $message."\n\n".
    "--$unique_sep\n".
    "Content-Type: gz; name=\"wp.gz\"\n".
    "Content-Transfer-Encoding: base64\n".
    "Content-Disposition: attachment\n\n";
    //Get the file from our function
    $file = get_database_file($database_name, $domain, $username, $password);
    //This is where the file is actually attached
    $headers .= chunk_split(base64_encode($file)) . "--$unique_sep--\n";
    //Finally we can mail
    if(mail($to, $subject, $message, $headers))
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    //If your database is username_cms, ONLY PUT cms!
    $database_name = "cms";
    //Include :2082, it's cPanel's port
    //No www or http
    $domain = "yoursite.com:2082";
    //cPanel username and password
    $username = "username";
    $password = "*******";
    //Who to send the backup to
    $email = "email@address.com";
    //Appears in the from field
    $from = "email@address.com";

    //Send it off with the data in the E-mail message.
    send_database_file($email, $from, $database_name ." Database Backup", "The backup of ". $database_name ." was performed at ". date("g:i A F j, Y"));
    ?>


    Now I'm not going to explain the whole script because it's to do with complicated mail header functions in php. But it's pretty simple apart from that: it gets the database file and sends it to your email address. All you have to do is change the details near the bottom. Having used this script personally, I've found it very very useful and time saving.

    Next, we need to use the cron tab in CPanel to set up a cron job. Follow this step by step guide:

    -- Open CPanel
    -- Click on on 'Cron jobs' icon
    -- A cron tab interface will appear
    -- Enter "php -q /home/my_username/backup.php" in the field "Command to run"
    -- Set your time settings using the boxes below
    -- Hit "Save crontab" and you should be done

    Hint: Set a time close to now, so that you can test and see if it actually works.




    Note: there should be a "Save crontab" button, but since I ahve many cron jobs, my one is further down.

    The above cron tab would run the backup.php script every Sunday, every month, at 5:00pm (as time of hour is 0).

    Conclusion

    As you can see, using cron jobs is VERY simple. All you need is a repetitive task and a script for it. If you need any help, please don't hesitate by commenting.

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Administration (7)
      Editing Files (2)
      Getting Started (8)
      Installation (8)
      Linux and other OSs (10)
      Miscellaneous (10)
      Networking (8)
      Security (9)
      Shells and Utilities (14)
      System Monitoring (5)
      Troubleshooting (1)
      X Windows (6)

    New

    Hot