1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Networks HOW-TO: Backup automatically via a network

Discussion in 'Hardware' started by iam, 14 Jan 2003.

  1. iam

    iam What's a Dremel?

    Joined:
    24 Mar 2002
    Posts:
    12
    Likes Received:
    0
    HOW-TO: Backup important files across a network automatically

    Aim: To automatically backup a folder onto a server (or any other PC on the network) on a certain day and at a certain time.

    What you need:
    > Two PC's (although you can adapt this guide to use just one)
    > Notepad
    > Windows 9x/Me/2000/XP (again, I imagine Linux people could adapt this article for their needs)

    Background Info:
    The reason I'm doing this article? Well, there's a number of reasons. First of all it's because I love you all so much. Second is because I am currently in the middle of setting this up myself. Thirdly, some people argue it's because I'm such a damn nice guy. :p

    What I'm using:
    My server is a PIII-600 with Windows .NET Server 2003 installed, this rig is my fantastic (or as some of you may say, /quoting hand gestures "l33t") new laptop, and my main rig is an AXP 1600 running on an EpoX 8K7A.

    Method
    Sharing - Share and share alike
    Right, first things first. You need some files to backup, and they have to be in a shared folder on your network. I'll give you a quick rundown of how-to share a folder.
    > First, find the folder with your assorted top-secret and very important files in, then right click on said folder. Zoom down to Properties, and click it. You'll be presented with some various 'mumbo-jumbo' about the folder, that's not what we want. If you have networking installed, you should see a little tab at the top of the window, titled 'Sharing', click it. Then down near the bottom of this tab their should be an area entitled 'Share this folder on my network'. Note: If you have selected to make all your documents private, then your going to have to move this folder to the 'Shared Documents' folder, then follow the same steps.

    [​IMG]

    The script - Becoming a l33t h4><0r

    Okay, now they're shared, it's time to enter the exciting world of DOS Batch File Scripting :D. Now after a few minutes playing around I made the following script:

    Code:
    @echo off
    echo Welcome to the Automated Backup Script
    net use z: \\Hp\backups 
    copy z:\*.* c:\backups\ /V
    echo Your files have now been backed up. :)
    pause
    
    /Here's one I made earlier moment folks

    Right, that script will first assign the network folder \\Hp\backups to the drive letter z:, and then copy all the files in that folder over to the server, and put them in the 'backups' folder on the C: drive. The text behind the echo parts are just to make it look nice, and the pause command is just there to tell you that's it run succesfully. Ignore the error 'This folder has already been assigned to a drive letter', the rest of the process will still continue without any problems. That '/V' verifies that all the files have been copied.

    So, you can simply cut and paste that script into Notepad, and then save it as backups.bat (be sure to change the file extensions drop down menu to 'All files'). Of course, you need to replace my files with yours. So let's use another example. The machine with the files on is called 'main', and the folder on it is called 'music'. Here's the script you'd use:

    Code:
    @echo off
    echo Welcome to the Automated Backup Script
    net use z: \\main\music 
    copy z:\*.* c:\backups\ /V
    echo Your files have now been backed up. :)
    pause
    
    However, there are problems with this script.
    > It won't copy any sub-folders. This may be a problem if you want to organise your backups into different folders.
    > If there is a large amount of files, the process will take a while because it copies every file, regardless of the fact that it hasn't changed since the last time.

    Unfortunately, I have no real solution to the latter, but the former is quite easily solved.

    Say if your backup folder consisted of two sub-folders, 'music' and 'documents', aswell as some miscellaneous stuff in the root folder. Also, say there is another subfolder in the 'documents' sub-folder, called 'work'. Here's the script that would back it all up:

    Code:
    @echo off
    echo Welcome to the Automated Backup Script
    net use z: \\Hp\backups 
    copy z:\*.* c:\backups\ /V
    copy z:\music\*.* c:\backups\music\ /V
    copy z:\documents\*.* c:\backups\documents\ /V
    copy z:\documents\work\*.* c:\backups\documents\work\ /V
    echo Your files have now been backed up. :)
    pause
    
    As you can see, I've simply just added an extra three lines that backup all those extra sub-folders. Note: The machine your copying too needs to have those subfolders in the 'c:\backups\' folder. Otherwise the copy command will fail.

    Scheduling - Time to get your diaries out

    Now, here come's the complicated part folks :p

    First things first, find your way to the Control Panel, and then to 'Scheduled Tasks'. Then click 'Add new task'. Here's where you need to decide how important your data is. I feel safe with backing up my data every two days. So on your server, when it asks you to select which application you wish to run, scroll down the list until you find 'Command Prompt'. The reason why we're doing this is because for some strange reason Scheduler doesn't want you to be able to run bat files. Never mind, it's easily overcome. So, you've selected 'Command Prompt', then click next. Now select when you want your backup script to run, as I said earlier, for me, every two days is sufficient.

    [​IMG]

    Follow the rest of the wizard, possibly filling in your password, until you reach the final screen. When you do, select the checkbox to 'Open advanced properties for this task...'. Then click finish.

    On the next screen, you need to change the command to something like below:

    [​IMG]

    Obviously, if your 'backup.bat' is located in 'C:\scripts\' then you need to change the command box to suit your requirements.

    Hey presto! We're all done!

    Once that's all done, and the task starts, you should be left with:

    [​IMG]

    That's all folks, I've been iam, and you've been a great audience. :D

    Any problems, please feel free to e-mail me: luke@mceonline.co.uk
     
  2. iam

    iam What's a Dremel?

    Joined:
    24 Mar 2002
    Posts:
    12
    Likes Received:
    0
    Quick update: I realise the xcopy command works much better than the copy command, especially when the /D parameter is used (which only copies files that have been changed since the last backup)....

    So once all the testing is completed this end, I'll duly edit the HOW-TO :)

    Cheers,
     
  3. RTT

    RTT #parp

    Joined:
    12 Mar 2001
    Posts:
    14,120
    Likes Received:
    74
    *Stickied!*

    Great work :cooldude:
     
  4. iam

    iam What's a Dremel?

    Joined:
    24 Mar 2002
    Posts:
    12
    Likes Received:
    0
    Ta very much :)

    /hopes it doesn't go the way of the majority of all stickies..... unread :eeek:

    :p
     
  5. Haddy

    Haddy World Domination

    Joined:
    22 Jan 2002
    Posts:
    2,821
    Likes Received:
    0
    Very very nice.....Knew that old server would come in handy some day...
     
  6. RTT

    RTT #parp

    Joined:
    12 Mar 2001
    Posts:
    14,120
    Likes Received:
    74
    I actually used it last night too, but that 5gb drive on my PII is getting rather full :wallbash:
     
  7. vi-kata

    vi-kata What's a Dremel?

    Joined:
    27 Jan 2002
    Posts:
    195
    Likes Received:
    0
    Nicely written :)

    One minor bug - the format for COPY is:

    COPY [source] [destination] - so your batch files are actually copying from the Z drive to the C drive, which is probably the wrong way around :blush: . You can also copy straight to the \\server\share in the COPY command, you don't actually need to map the drive first.

    You're right about xcopy being better, especially when backing up large sets of files or multiple directories. I have a similar script that runs to backup my "My Documents" folder and all it's subfolders. Using xcopy also gets rid of the need to have an already-existing folder structure at the destination. The xcopy line goes:

    xcopy "c:\My Documents" \\server\share\ /D /E /V /I /H /R /Y

    Type "xcopy /?" at the commandline for a list of what the /D etc mean.

    It's worth noting that you can also run gpedit.msc in Windows 2000 or XP and click: Computer Settings - Windows Settings - Scripts - Shutdown and then add the script/batch file in there. This way the script runs whenever the PC is shutdown, rather than at a scheduled time. My script backs up around 400Mb, so I just shut the machine down and it copies the folders for about 5 minutes before switching off.

    Kata
     
    Last edited: 16 Jan 2003
  8. iam

    iam What's a Dremel?

    Joined:
    24 Mar 2002
    Posts:
    12
    Likes Received:
    0
    Cheers for the info :)

    Alas! Rev 1.1 of the guide is coming very soon! :D

    This will include:
    > xcopy guide
    > NT Backup guide
    > local backup guide
    > Linux backup guide

    And maybe a few more bits n bobs :D
     
  9. iam

    iam What's a Dremel?

    Joined:
    24 Mar 2002
    Posts:
    12
    Likes Received:
    0
    Hehe, hate it when that happens, nothing worse than thinking somethings backed your stuff up to find out that it hasn't :D

    Especially when it's been running 24/7 for three months, without me touching it once, then when my PC dies, and I come to retrieve backups, there isn't anything there :blah: :rolleyes:

    But I'm a clever boy and made backups to my other server ;) :cooldude: :D
     
  10. felix the cat

    felix the cat Spaceman Spiff

    Joined:
    11 Jan 2002
    Posts:
    4,914
    Likes Received:
    11
    id just like to say cheers...i had this idea of automatic backup ages ago, but never knew how to do it, and i used your idea for the basis of writing my own script, so ta very much...
     
  11. iam

    iam What's a Dremel?

    Joined:
    24 Mar 2002
    Posts:
    12
    Likes Received:
    0
    No problems m8y, glad it helped you out :) :D
     
  12. dakar

    dakar What's a Dremel?

    Joined:
    6 Mar 2002
    Posts:
    460
    Likes Received:
    0
    Another option you may consider is robocopy. I know it ships with the 2K and NT Resource Kits, but i think its publically available as well... Its the M$ equiv. of rsync.. and when used with the /MIR (mirror) switch it atucally does a file comparison and makes for quicker backups.

    You can reduce your batch files to a single scheduled task... ie robocopy.exe \\source \\dest /MIR and vice versa.

    It can actually do a lot more but that is beyond the scope of this thread.

    I use it for incremental backups of live data several times throughout the day for warm spare file servers, saves wear and tear on the tape drives.
     
  13. :: Phat ::

    :: Phat :: Oooh shakalaka!

    Joined:
    7 Jun 2002
    Posts:
    4,886
    Likes Received:
    3
    Ok - Download a program called XXCOPY.EXE, its only small 192kb then create a batchfile.
    Right, line 1 del c:\logfile.txt this deletes the log file that was originally created. You could include a couple of lines that renames one log file to fatherlog.txt and renames the original fatherlog to gradnfatherlog so you can have 3 or more log files saved etc.

    the second sets your shared folder up as a mapped drive, not necessary but makes life easier.

    now you need to call the xxcopy program, change the path to wherever you save it, the first path is the copy from location, the second is the copy to (the mapped drive)

    Then come the paramaters.

    /e = Check Every File
    /da = Copy New/Changed files
    /h = Include Hidden Files
    /yy = Auto Yes @ Prompts
    >> = save data to log file (enter path)

    then the last line dissconnects the mapped drive, then all you need to do is create a sheduled task as shown by "Iam" in the first post.

    If you want to go a step further and have an SMTP server on your network then you can add another couple of lines using a program called "BLAT" download and copy the file and follow the instructions for creating the .reg file. Now make another .bat file with the following.
    All this does differnently is uses the DOS prompt to send you an e-mail when the backup is complete. Very handy....

    there you go.. my addition to this stickie :brrr:
     
  14. iam

    iam What's a Dremel?

    Joined:
    24 Mar 2002
    Posts:
    12
    Likes Received:
    0
    Revision 1.1 Part 1
    xcopy

    Okay folks, after a couple of months of being a bum, I've decided to do Rev 1.1 Part 1, which will substitute the earlier "copy" command with the the much more agile xcopy command.

    So, for the above script:

    Code:
    @echo off
    echo Welcome to the Automated Backup Script
    net use z: \\Hp\backups 
    copy z:\*.* c:\backups\ /V
    copy z:\music\*.* c:\backups\music\ /V
    copy z:\documents\*.* c:\backups\documents\ /V
    copy z:\documents\work\*.* c:\backups\documents\work\ /V
    echo Your files have now been backed up. =)
    pause
    
    My new revised version would look like this:

    Code:
    @echo off
    echo Welcome %USERNAME% to %USERDOMAIN% to the Automated Backup Script
    net use * /delete /y
    net use z: \\Hp\backups 
    xcopy z:\*.* c:\backups\ /D /E /V /C /L /H /Y
    echo Your files have now been backed up. =)
    pause
    
    That does exactly the same job as above, but a better one. Let me explain why, it's basically down to the parameters I've used:

    /D : Doesn't copy any files that are older than that at the destination

    /E : Copies all directories and sub directories, even if they're emtied

    /V : Verifies each new file has been succesfully copied

    /C : Continues copying even if errors occur

    /L : Displays files that will be copied

    /H : Copies hidden and system files

    /Y : Suppresses any prompts to confirm your choice to overwrite files

    Now, you can add/swap/change those parameters as you wish to give you your perfect solution.

    For anybody who's interested, here's the full listing of possible parameters, achieved by typing "xcopy /?" at the command prompt:

    And thats that folks, all that you have to do then is set up scheduling like I showed above.

    Enjoy! :)
     
  15. Flax

    Flax What's a Dremel?

    Joined:
    24 Jun 2002
    Posts:
    300
    Likes Received:
    1
    When I tried Revision 1.1 it listed all of the files, but copied non of them, removing /L made it work fine. I'm using windows XP Home.

    Thanks for making that :) it's been quite useful
     
  16. ST8

    ST8 What's a Dremel?

    Joined:
    14 Feb 2003
    Posts:
    596
    Likes Received:
    0
    this article got me interested, however im running a linux server and hence needed a linux backup script, so here we go.

    My bash skills arn't what they should be hence this is in perl. Just edit as nessacery and place in crontab :)

    It uses samba file system to mount the remote shares

    Code:
    #!/usr/bin/perl
    
    # Simple *nix backup script using smbfs
    # by ST8
    # Place this script in crontab and your away...
    
    # Requirments:
    # * You need a working samba setup!
    # * You need smbfs installed (modconf -> kernel/fs/smbfs)
    
    use strict;
    use File::Find;
    use File::Copy;
    
    # Configuration
    
    # Directory to put backups in (*this dir must exist*)
    my $bkdir   = '/usr/backup';
    
    # Mount point for remote files (*this dir must exist*)
    my $mntpt   = '/usr/backup/mntpt';
    
    # Share Username
    my $susr = 'guest';
    
    # Share Password
    my $spwd = 'guest';
    
    # Directories to backup (*Local Dir will be created on run*)
    #               'Remote Dir'   => 'Local Dir',
    my $bkdirs  = { '//ST8/test' => 'ST8',
                  };
    
    
    ########### Dont Edit below here unless u know what you're doing! ###########
    
    foreach my $dir (keys %{ $bkdirs }) {
        print "Trying: $dir\n";
        chdir($bkdir);
        mkdir("$bkdir/$bkdirs->{$dir}", 0700) unless -e "$bkdir/$bkdirs->{$dir}";
    
        # Mount our remote drive
        my $rem = `mount -t smbfs -o username=$susr,password=$spwd "$dir" "$mntpt" 2>/dev/null`;
        
        # Couldnt moun't ... onto the next share
        if ($rem =~ /failed/) {
            print "Couldn't mount: $dir\n";
            my $um = `umount $mntpt`;
            next;
        }
    
        print "Mounted: $dir\n";
    
        # Recursivly index the dir
        find({ wanted => sub { &wanted($_, $bkdirs->{$dir}) }, follow => 1 }, $mntpt); 
    
        # Unmount ... we're done!
        my $um = `umount $mntpt`;
    }
    
    sub wanted {
        my ($file, $dir) = @_;
        my $rem = $File::Find::name;
        (my $dirl = $File::Find::dir) =~ s/$mntpt/$bkdir\/$dir/;
        my $loc = "$dirl/$file";
    
        if (-f $rem) {
            mkdir($dirl, 0700) unless (-d $dirl);
            if ((stat($rem))[9] > (stat($loc))[9]) {
                copy($rem, $loc);
                print "Updated: $rem -> $loc\n";
            }
        }
    }
    
     
    Last edited: 25 Jun 2003
  17. Shad0r

    Shad0r What's a Dremel?

    Joined:
    9 Mar 2002
    Posts:
    33
    Likes Received:
    0
    MS Backup has been included in almost every Windows version ever and it's a great tool for doing all of this.
     
  18. iam

    iam What's a Dremel?

    Joined:
    24 Mar 2002
    Posts:
    12
    Likes Received:
    0
    ST8 - Fantastic work on the script, saved me a lot of trouble :D

    Shad0r - MS Backup isn't automated enough - although you can schedule jobs in it, its a lot more work to set up than the simple scripts shown. However - it may be an ideal solution for those who have trouble with the above scripts :)

    Thanks to everybody who's contributed :)
     
  19. mashie

    mashie The one and only

    Joined:
    15 Mar 2002
    Posts:
    1,055
    Likes Received:
    0
    Thanks for the tip, exactly what I have been looking for.

    Oh, you can get robocopy.exe here.

    Edit: finally the w2k version...
     
    Last edited: 11 Sep 2003
  20. cpu121

    cpu121 What's a Dremel?

    Joined:
    25 Nov 2003
    Posts:
    327
    Likes Received:
    0
    I've tried to do an auto backup on each of my systems using the schedule jobs facilty in Window's own back-up program. However I've just checked to see if the first try on Sunday Morning happened although it doesn't seem to have appeared in the selected Partition. It might have something to do with the computer being "off" although I swear I set it to "wake-up" to do the job, unless that only works when the system is in standby or hibernation? If so then :duh: for not making sure the system was in standby.

    All I need now is a script which would run when each of the networked machines turns on and send their back-up file straight to my main rig when it is turned on.
     
Tags:

Share This Page