Dec.af Sites:    Contacts Lenses Comparison | Funny Status | The Trailermash  | Craawler SEO & Website Spellchecker Tool | Image to Base 64 Tool

Backup USB Drive on Insertion

June 12th, 2006 at 9:02 pm By admin (Features, Gadgets, Tech & Science)

I use my USB drive for everything. It runs Portable Firefox, Portable Thunderbird and also holds most of my files. While it’s great having these programs run no matter where I am, frequently backing up the data is a pain. I wanted an automatic solution. Unable to find a good solution on the net, I created one for myself.

Step 1: Create the folder you want to backup to (Mines c:\removablebackup)
Step 2: Create the backup batch file for your USB key. You can do this by inserting the following in a textfile and saving it as backup.bat. An example is provided below:

@echo off
echo -------------------
echo Removable Device Automatic Backup
echo -------------------
echo No Rights Reserved, do whatever you want with this.
xcopy "*" "c:\removablebackup\" /Y /E /R
echo Backup Complete!
Pause.

Make sure you change the section in bold to reflect your individual backup location.

Step 3: Place the following in a text document, and save it directly to your removable drive as autorun.inf:

[autorun]
action=Backup
open=backup.bat
label=Backup
includeRuntimeComponents=True

Step 4: Your done! Now whenever you insert your USB key you should see this nifty backup option:

backup.png

36 Comments

  1. dan said,

    June 12, 2006 at 10:31 pm

    yah, i did that once, then i kinda formatted my usb key.. lol

  2. mike said,

    June 22, 2006 at 7:22 pm

    Wow, you have no idea what you just saved me from buying. Thank you.

    I was trying to do the reverse: use usb keys instead of tapes for backups. So I wanted to copy from my hard drive to the USB stick. In case anyone else is interested, here’s what I did:

    1. Clear out the USB key.
    2. Make a folder (I called it “storage”) on the usb key.
    3. Put the autorun.inf file on the usb key. The contents are the same as above.
    4. Put the backup.bat file on the usb key. The contents are as follows:

    @echo off
    echo ——————————-
    echo Removable Device Automatic Backup
    echo ——————————-
    echo
    xcopy “c:\FolderIWantToBackUp” “\storage” /Y /E /R
    echo Backup Complete!
    Pause.

    you can customize the xcopy options – just google for “xcopy”
    the “\storage” path is, of course, relative to the USB key.

    Again, thanks to Johnsee for the inspiration.

    Quick question, though: my “@echo off” command will still output “ECHO is off” to the console. The @ is supposed to suppress this, right? Does anyone know why it wouldn’t be doing this?

  3. mike said,

    June 22, 2006 at 8:04 pm

    I just realized, it may be wise to put a command to clear out the destination before writing to it. In case the source has had a file deleted, it won’t be present in the backup. I added the following line right before the xcopy command

    “rd /s /q “\backup”

    Also: I solved the “ECHO is off” problem. Typing a period immediately following a blank echo command will output a blank line (like I wanted), instead of an “ECHO is off” message. So, all my echo statements now are:

    echo.

    with a period right after it.

  4. Girish said,

    July 7, 2006 at 8:00 pm

    Hi,

    i am not able to take backup, when i inserted my USB (U3)

    i have followd same steps as given above, so help me out.

  5. How to Automatically Backup your USB Drive on Insertion at Shivaranjan’s Blog said,

    July 8, 2006 at 2:43 am

    [...] Many of us are using USB drives these days as the prices have fallen down. If you use your jump drives regularly then you might want to backup the data in it automatically. I came across this article by Sandstorming.com on how to automatically backup your pen drive when you insert it. [...]

  6. Jestre said,

    July 26, 2006 at 1:29 am

    Since 7 zip is portable try the following for smaller sequential backups:
    NOTE: it creates a new backup file everyday but try setting system date format to YYYY-MM-DD format so it will sort.
    It then creates a file name of Firefox.dat.2006-07-25.7z etc.

    Del “\Data\Firefox.dat\XUL.mfl”
    Del “\Data\T-Bird.dat\XUL.mfl”
    \Apps\7-Zip\7z a -r “C:\Backups\Firefox.dat.%date%.7z” “\Data\Firefox.dat”
    \Apps\7-Zip\7z a -r “C:\Backups\T-Bird.dat.%date%.7z” “\Data\T-Bird.dat”
    \Apps\7-Zip\7z a -r “C:\Backups\Personal.%date%.7z” “\Personal”

  7. zuper said,

    August 26, 2006 at 6:14 pm

    i’ve added a /D to the xcopy command,

    so it backs up only the files modified ;)

  8. zuper said,

    August 26, 2006 at 6:30 pm

    i’ve also created a log file, so you always know what happened..

    and so you can clear the pause command…

    date /t>>backup.txt
    time /t>>backup.txt
    xcopy “*” “c:\removablebackup\” /Y /E /R /D>>backup.txt
    echo ———————————————–>>backup.txt

  9. Dan W. said,

    September 10, 2006 at 11:10 pm

    Hey Johnsee, you thu MAN…………………………………works great and a complete solution to my problem which was the same as your problem. I did add the RD command to my .bat file to clean up my backup folder but thanks for taking the time to post.

  10. Liam said,

    September 20, 2006 at 9:55 am

    I have added a few commands from the original

    @echo off
    echo ———————————
    echo Removable Device Automatic Backup
    echo ———————————
    echo Backing Up Data
    date /t>>backup.txt
    time /t>>backup.txt
    xcopy “*” “c:\removablebackup\” /e /y /r /h /d>>backup.txt
    echo Backup Complete!
    Pause.

    howevery when i try to backup my USB drive i keep getting a share violation issue and its always on the same file. Is there a way i can make it skip and share violations or is there a way i can fix it? Can anyone help?

  11. Jaap said,

    October 4, 2006 at 11:25 pm

    This little thing is just what I need! But I have a little problem.

    Whenever I try the option ‘backup using the program provided on the device’ I get the error that ‘F:\ is not a valid win32 application’. For every other task the stick is functioning perfectly. When I run the xcopy command from the command prompt the files are copied as expected.

    Can someone help me out?

  12. Jaap said,

    October 5, 2006 at 2:11 am

    Thanks if you’re thinking about my head-breaking problem, but the solution was very simple but stupid also. I named my file ‘backup.dat’ in stead of ‘backup.bat’.

    Thanks again for this powerful tool!

  13. Jason said,

    October 28, 2006 at 1:10 am

    Hey thanks guys,
    I am using USB backup mode #2 (backing up from C: drive to USB key), which works great.
    I am actually trying something a little more advanced now, backing up from a network drive to USB key), which I am having some difficulties with, as when the XCOPY command is used, it seems to forget where the key is (drive letter-wise) and is actually copying the network drive to my C: drive instead.

    (I should mention to, that rather than having the key “Auto-update by itself” I have the USB key open a webpage that I built to help navigate the key folders. Then on the main page, I have a link to “Update key”)

    Has anyone else tried this?
    Thanks again for this cool app.

    J

  14. Andrew said,

    November 8, 2006 at 12:12 am

    Hello,
    iI found very good what Jestre said.
    Could you tell me how to set system date format to YYYY-MM-DD
    using batch command?

  15. belief said,

    August 16, 2007 at 6:05 pm

    I suggest programmers to write simple file to sync flash drive with a microsoft briefcase in hdd. the point is briefcase is use for synchronisation, it does better than pure backup which doesnt address delete.

  16. Vikrant Rampuria said,

    August 23, 2007 at 9:02 pm

    Hello
    I want my data to be backed up with to my USB drive with regular interval of time. For instance i save all my data to one folder and i want somethng which can auto backup data with a interval of 5 or 10 mins.. Is it possible or not … Can anyone help me ???

  17. steve said,

    September 10, 2007 at 12:25 pm

    I really dont understand about programing. Could anyone make an .exe program of it. I really appreciated. Thank You

  18. Ed M said,

    November 9, 2007 at 7:31 pm

    Great! really works! Thank you.

  19. homer said,

    November 12, 2007 at 1:56 pm

    Ingenious, now all that is left is removing files that have been deleted from the source folders

  20. divad said,

    November 13, 2007 at 8:38 pm

    hi …

    I want to know how I can make a automaticaly backup when the usb flash disk plug to the computer without asking me if i want or not….

  21. meridia online said,

    December 15, 2007 at 4:13 pm

    meridia

    encrusting discussed

  22. Michael said,

    March 17, 2008 at 4:10 am

    how do you make the program delete things in the backup folder that have been deleted on the flash drive?

  23. Anon said,

    April 11, 2008 at 4:08 am

    “how do you make the program delete things in the backup folder that have been deleted on the flash drive?”

    Use robocopy and the /mir option. Not only is it more reliable than xcopy but it has a 22 page manual full of options you can use. It can be obtained with the resource kit tools from microsoft. You’ll have to keep the exe on your usb drive but it’s 78 KB. If you have vista you already have it just type robocopy /? in a command shell.

  24. Brad said,

    May 21, 2008 at 6:24 pm

    I had it working (2nd method….backing up from C:\ to USB drive), and after I was playing around with making some of the files hidden (autorun.inf, backup.bat, icon file, robocopy.exe) I’m getting the error message: F:\ Access is denied

    Any ideas? I appreciate any help.

  25. Ken said,

    May 24, 2008 at 1:29 pm

    I had this working. Its great but instead I used winrar to archive it onto my usb. I used this batch command:

    @echo off
    echo ——————-
    echo Removable Device Automatic Backup
    echo ——————-
    “C:\Program Files\WinRAR\RAR.exe” a -r -t “BACKUP_FILE.rar” “FOLDER BEING BACKED UP” -ag[mm-dd-yyyy]
    pause

    Then I used this website’s .inf template

    resulting file will named:
    BACKUP_FILE[MM-DD-YYYY].RAR

    Hope this helps someone.

  26. Kjetil said,

    September 18, 2008 at 7:00 pm

    I have a small suggestion for a change to the script. There is always the danger of selecting backup when we are using someone elses computer. Or somewhere where you don’t want any backup. At least when you have a habit of clicking it.

    My suggestion is to create a batch file on the C: drive(s) of your computer(s) where you want the backup to run. In this batch file, you put the xcopy command.

    Then, on your USB drive, you remove the xcopy command, and instead call the batch file on C:.

    Thus, if the batch file exists on C:, your backup is made. If not, the backup will fail (which is what you want!).

  27. Hasperhoven . NET » Blog Archive » Backup van USB sticks en memory cards said,

    October 27, 2008 at 2:50 am

    [...] vond daarvoor een trucje met een autorun.inf op de kaart zelf op deze pagina. Maar voor mijĀ  bleek dat niet te werken. Uiteindelijk ben ik zelf maar wat gaan prutsin in [...]

  28. Jim said,

    February 25, 2009 at 2:00 am

    my autorun.inf doesn’t work and it’s driving me bananas!

    The dialog box appears… so it’s not an XP error. But all of my autorun options don’t appear in the dialog box – ie. name of the drive, name of my .bat file, icon… nothing!

    I’m at the end of my rope…. help??

  29. Justin Bussell said,

    March 31, 2009 at 2:53 pm

    Howdi, for some reason my script only backups a folder called “other” and not any other folder. Script is
    @echo off
    echo ——————-
    echo Removable Device Automatic Backup
    echo ——————-
    echo No Rights Reserved, do whatever you want with this.
    xcopy “*” “h:\removablebackup\” /Y /E /R
    echo Backup Complete!
    Pause.

    Autorun script is
    [autorun]
    action=Backup
    open=backup.bat
    label=JBussell
    includeRuntimeComponents=True

  30. Justin Bussell said,

    March 31, 2009 at 2:53 pm

    ^anyway to make it backup more than one folder?

  31. Rexy said,

    April 19, 2009 at 4:50 am

    I found this very useful, thanks!

    A tip especially to Kjetil; Just add /W to the xcopy line along with the other xcopy settings. That makes you have to press W in order to make it start copying. So that you do not accidentally backup when you do not want to.

  32. Rexy said,

    April 19, 2009 at 4:51 am

    A correction to the last post. Adding /W makes you have to press a key, any key. Not W in particular.

  33. tim from Radio Clash said,

    April 30, 2009 at 1:59 am

    Anyway I can make this fire when I insert the flash drive rather than on click?

    Otherwise the xcopy example seems to be working for me, thanks!

  34. amatbrewer said,

    May 7, 2009 at 1:51 pm

    I found a variation to what Kjetil suggested. I moved the backup.bat file to the target folder and changed the autorun.inf to run it rather than the one on the memory stick.
    Now it only backs up to computers with the backup.bat in the correct folder.

    [autorun]
    action=Backup
    open=C:\backupFolder\backup.bat
    label=Backup
    includeRuntimeComponents=True

    {this goes in the backup.bat file}
    rem backup all files & folders in the local directory
    xcopy “*” C:\backupFolder /E/Y/I/M
    Echo Backup Complete

  35. Amar Jain said,

    November 10, 2009 at 12:12 pm

    Guies I am in urgent need of this solution.
    What I want is that as soon as my one document gets modified, it should get coppied in my USB drive from pc. So, detailed command for inserting something like this?
    Let me make it more clear. Basically from 11th of Nov., I have to use computer in my exam for writing my exams. So what I will do is that I will save one file in my computer, and another in pen drive after every few minutes to insure that if my data gets lost from one place, I have it on other place. So if an automated script can do this job of coppying the file as soon as it gets modified and saved again, then things will become easier for me. But please, detailed reply as I understand the dos commands but not good at it.

  36. chucklehead said,

    January 11, 2010 at 5:55 am

    I tried to get this running on Windows 7 but no luck. Microsoft decided to amputate the autoplay USB options for Windows 7 in an effort to protect people from scary malware.

    If anyone has a solution that works on Windows 7 I’m sure lots of people would appreciate it.

Trackback URL for this post: http://www.sandstorming.com/2006/06/backup-usb-drive-on-insertion/trackback/