If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 7 of 7
  1. #1
    Senior Member
    Join Date
    Dec 2003
    Member #
    PHP backup script
    by Adam LeVasseur [xarst]

    This tutorial explains how to make a backup script that can be run with SSH or a cron job. Most *nix hosting companies let users create cron jobs for his directory. This script can't be run with a browser due to security reasons.

    Be sure to have a lot of available space on the server, because a backup can be a lot of megs, even when this script bunzip2 them.
    This script has limitations, like that he doesn't remove old backup files.
    I'm planning to improve it to have it keep a max of "x" files in the folder where they are located.

    You need to specify absolute paths in this script. If you don't the absoulute path, just paste this piece of code in a new file and call it "abspath.php". Upload it to your server. Then access it with a browser, and it will output the absolute path to this file.
    PHP Code:
    echo getcwd();
    Simple, right?

    Now we go with the backup script. I don't think I have to explain each part because it is very well commented. (All the lines that begin with two slashes [//] are ignored by the PHP parser and are only for human readibility.

    PHP Code:
    //Backup date variable. Replace "Ymd" with
             //"Ymd_H-i" to include the time.

    $backupdir "/the/absolute/path/to/the/folder/";
    //Where are the files located?
    $files "*";
    //What file to backup? Use a * to backup all the files
             //inside the folder entered above.
    $backupto "/the/absolute/path/to/the/folder/";
    //Where to store the tarball?

    $fileprefix "bak";
    //This is the prefix that will be added before the date:
             //The underscore _ is added automatically

    $tararg "-cf";
    //Here goes the tar arguments. I recommend -cf.
             //c is for compressing. f is for outputting
             //a file.

    $bz2arg "-z9";
    //Here goes the bunzip2 arguments. I recommend -z9.
             //z is for creating a archive
             //and 9 is for max compression. z is always needed

    //Call the function
    backupsus() {
    $backupsuscmd "cd $backupdir;
    $tararg {$fileprefix}_{$backupdate}.tar $files;
    $bz2arg {$fileprefix}_{$backupdate}.tar;
    {$fileprefix}_{$backupdate}.tar.bz2 $backupto";
    passthru ("$backupsuscmd");
    Paste this code in a file with a 'php' extension and upload it to your server.
    If someone asks of how to create a cron job, I will extend the tutorial.
    To run this script via SSH, go to the folder where it's located and type in:
    php4 -q filename.php
    Of course, replace 'filename' with the correct name.

    If you need help with something related to this just post


  3. #2
    Senior Member
    Join Date
    Dec 2003
    Member #
    Part 2

    I've decided to post here a continuation explaining how to edit the crontab and to make it compartible with the vast major hosting companies.

    First we are going to create the shell script. This isn't needed by some companies, but some do need it. I had to create it on my 1and1 hosting.
    Create a file called, and open it with notepad or similar.

    Paste this code to it:
    export PATH
    cd /path/to/the/folder/where/the/script/is
    php4 -q filename.php
    The first line sets the path to the needed programs for the backup script to work: tar,mv and bunzip2.
    The second line exports the path to be available to the backup script.
    The third line goes to the folder where the backup script created on the post above is. Change it to the correct path/
    The fourth line simply runs the backup script with the PHP parser. Change 'filename.php' to the one you created in the post above.
    Now upload this file to your server.

    Now we are going to set up the crontab file. You need to have SSH access to do this.
    When in the SSH prompt, type in:
    crontab -e
    Now type in this:
    * * * * * sh /pathto/the/shell/script/created/above/ > /dev/null
    See the five stars?[*]
    The first one stand for minute(0-59)
    The second for hour(0-23)(23 = 11pm)
    The third for day of the month(1-31)
    The fourth for month of the year(1-12)
    And the last for day of the week(0-6)(0 = sunday)
    This is the time it will be run. As example, this is what you have to put where the stars are:
    0 16 * * 1
    This will run the backup script at 4pm every monday.
    30 0 * * *
    This will run the script every day at 0:30 of midnight)

    Now we save the crontab, like this:
    Hit escape to exit the "insert" mode, and type:
    This saves and exit.
    Now just sit and relax, as at the hour you have chosen to run the script.

    Test if it works by going to the folder where the is, and typing in:
    If it just takes a while and doesn't say anything, it has worked!

  4. #3
    Junior Member
    Join Date
    Sep 2004
    Member #

    What i need to achieve withour script is to backup my htdocs folder. I want to copy it and reaname it into my root directory (above htdocs) Here is the break up:

    WWW -> Backup, Htdocs, temp, etc,

    Then i have frm htdocs the folder i want to diplicate and rename to the date and be inserted into my backup directory, in the folder called htdocsbak that is above my htocs and name the folder the date it was backuped on. I want this to happen every 3 days.

    Can you help me out?


  5. #4
    Junior Member
    Join Date
    Jan 2005
    Member #
    Trying your script

    But when I attempt to untar the output after ftping to my PC I get this error using WinRAR.

    ! C:\Documents and Settings\xxxxx\Desktop\bak_20050101.tar.bz2: Read error in the file C:\Documents and Settings\xxxx\Desktop\bak_20050101.tar.bz2

    Here is the PHP file I made:
    $backupdate = date("Ymd");
    //Backup date variable. Replace "Ymd" with
    //"Ymd_H-i" to include the time.

    $backupdir = "/home/www/";
    //Where are the files located?

    $files = "*";
    //What file to backup? Use a * to backup all the files
    //inside the folder entered above.

    $backupto = "/home/www/";
    //Where to store the tarball?

    $fileprefix = "bak";
    //This is the prefix that will be added before the date:
    //The underscore _ is added automatically

    $tararg = "-cf";
    //Here goes the tar arguments. I recommend -cf.
    //c is for compressing. f is for outputting
    //a file.

    $bz2arg = "-z9";
    //Here goes the bunzip2 arguments. I recommend -z9.
    //z is for creating a archive
    //and 9 is for max compression. z is always needed

    //Call the function

    function backupsus() {
    global $backupdate,$backupdir,$backupto,
    $backupsuscmd = "cd $backupdir;
    tar $tararg {$fileprefix}_{$backupdate}.tar $files;
    bunzip2 $bz2arg {$fileprefix}_{$backupdate}.tar;
    mv {$fileprefix}_{$backupdate}.tar.bz2 $backupto";
    passthru ("$backupsuscmd");

    Hoping you can tell me what may be wrong.

    Thanks in advance,

    Bob G.

  6. #5
    Junior Member
    Join Date
    Oct 2005
    Member #
    Just try to rename the file prior to unzip it.

    Enjoy !

  7. #6
    Junior Member
    Join Date
    May 2007
    Member #
    Bringing back an old thread. How can I use this without having it make it a zip file. Like I want it to copy a file each day and just name it to a date and have it copied to a different folder. Kinda like an archive.

  8. #7
    Senior Member leprechaun13's Avatar
    Join Date
    May 2005
    Member #
    having a problem is there a way to get this to work without passthru?
    Regards Phil,

Remove Ads

Posting Permissions

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

Search tags for this page

backup folder php script


php script to backup directory


script php backup file


scripts php backup -mysql

Click on a term to search for related topics.
All times are GMT -6. The time now is 01:25 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: