E-Mail:
Get our new Windows 7 eBook (PDF) for $7 with 70+ Tips. Download Now!

Messing Around with Backing Up a USB Stick (Linux)

I use my USB sticks for a variety of things, most notably a temporary backup of important files from various PCs.  One day I had a spectacular USB stick failure and lost my backups.  Ever since then I thought it was a good idea to back up the backups.

There are a number of ways to do this.  In Linux, it’s always interesting because no matter how simple or well-explained, it never works for me.  This isn’t necessarily a reflection on Linux; moreso a reflection of what happens when I get near a computer.

This comes as a distinct disadvantage when one works in MIS, as I do.

My first step on the discovery path is, of course, Google.  When one searches on `backing up USB drive’, one gets all sorts of interesting results, generally about backing up to a USB stick (and p*rn).

One suggestion dealt with discovering the specific ID of the USB stick.  I tried and it failed, as usual.  Again, part of it reflects on me but some part of it also involves Linux.  Sometimes the documentation is, shall we say, lacking.  Or it assumes you know what you’re doing (which I don’t).  Of course I’m not speaking of all Linux documentation or programs.

I remember using rsync a month or so ago to sync two directories on two machines.  It was fast and accurate, as advertised.  I thought I had documented it in my personal help file but apparently not.  Or I documented it on a different computer but I lost my memory in a childhood accident and can’t remember which.

The next listing was http://forums.whirlpool.net.au/forum-replies-archive.cfm/1088468.html

BINGO.

This detailed the author’s process of backing up and discovering how the whole thing worked.  It would be silly to reproduce it here - I recommend visiting the page if you’re at all curious.

I messed with this person’s script and came up with the following.  Hopefully it will be of some use to you.  It appears to be pretty basic rsync.  I cut out the sections dealing with UDEV and RULES.  Copy these two lines to an empty document in a text editor (I use mousepad - you can use whatever floats your boat.  It’s better to avoid real word processors unless you know it saves in text format).  I saved this as usb_backup.

rsync -ar –log-file=/home/leftystrat/Desktop/$(date +%Y%m%d)_rsync.log /media/KINGSTON /home/leftystrat/Documents/usb_backup

/usr/bin/notify-send -t 0 “Backup Message”  “BACKUP’S DONE, Stupid!”

For your benefit (and mine), let’s dissect this.  The red text indicates something you need to modify to make this work for you:

rsync -ar : start backing up, archive+verbose options

–log-file=/home/leftystrat/Desktop/$(date +%Y%m%d)_rsync.log :  creates a logfile with the date in the title and puts it on your desktop.  I left it at Desktop so I could verify that it was working.  I’ll edit this to send to the destination directory from now on.

/media/KINGSTON /home/leftystrat/Documents/usb_backup :  your source and destination directories - (these must be on the same line as the command).  For source I used the (auto)mounted name of the stick and for destination, a directory I created just for this purpose.  Don’t forget to change the relevant lines or else you’ll wind up with some interesting results (if it works at all).

/usr/bin/notify-send -t 0 “Backup Message”  “BACKUP’S DONE, Stupid!” :  this sends a popup to your desktop with the text in quotes.  This will obviously work for you, but you might want to change the text.  -t 0 makes the text expire immediately.

After you do this, there’s one more step:

chmod +x  usb_backup (or whatever name you gave it).  This makes the script executable.  It will work from the directory it’s in like this: [prompt]./usb_backup     If you want, you can sudo mv usb_backup /usr/bin/ (copies the script to a directory that will make it executable from anywhere).

That’s it.  It works!  (I’m always excited when something works because it usually doesn’t).  It’s even more exciting because I found the original script and modified it.  You too can do this.  Congrats - you’re a programmer now :)

[DISCLAIMER:  I am not a programmer.  I mess with stuff and occasionally it works.  I just wanted to pass on what worked for me in hopes of helping and educating others - sometimes in the wrong way of doing things.]

3 Comments

I would recommend you at least add a “shebang” line as the first line of your script:

#!/bin/sh -

That makes it very clear just what is supposed to happen. I’m mildly surprised that your script works without that, though I guess some default or other is kicking in.

Even with that, there are some fun gotchas, as almost all Linux’s (sym)link /bin/sh to bash (in POSIX mode) and you might actually get real Bourne shell on other systems like Solaris. Then, there’s Ubuntu and maybe soon Debian. Since Ubuntu 6.06 /bin/sh is actually dash (the Debian Almquest shell), because it’s a lot smaller than bash and thus faster to start when running a gazillion shell scripts in a row, like, say, oh, when booting your machine. If you wanna know more about this, check my PLUG Preso: http://princessleia.com/plug/2008-JP_bash_vs_dash.pdf.

If you like intro books _Learning the bash Shell_ is great. If you prefer learning by example, my _bash Cookbook_ will help, though it is *not* an intro book itself. If I ever know we’re going to be in the same place at the same time I’ll bring you a copy.

Meanwhile, there are a lot of great bash references out there, starting with:
* http://tldp.org/LDP/Bash-Beginners-Guide/html/index.html
* http://tldp.org/LDP/abs/html/

Some of the other fun things you can do almost trivially are using replaceable parameters so that you can use the same script to back up different things to different places, but still default to the locations you currently have hard-coded. More detail on request. :-)

One final note, my Ubuntu 8.04 helpfully tells me:
The program ‘notify-send’ is currently not installed. You can install it by typing:
sudo apt-get install libnotify-bin

No big deal, and quite a helpful message, but FYI that isn’t always there. (There are ways to test for what’s there and try other things too, of course. :)

Somehow I knew that some kind soul would come along, shortly after I posted that, and tell people the right way to do it. The fact that it is you, JP, surprises me not at all.

I have seen those shebang thingies for a while and never knew what they were for. I remember someone telling me once that they were not commented out, even with the hash mark, so that confused the hell out of me.

Apparently notify-send is included out of the box with 8.10, as I certainly didn’t install it.

Thanks for the tips and links - I’ll be downloading, visiting, and learning. Eventually I should know scripting.. after all, I’ve been using linux for a few years now… it mirrors my general computer knowledge: incredibly precise in little spots. Lots of technical, some basic, and huge holes in between.

You should see me with a soldering iron and high voltage…

What Do You Think?

 
55 queries / 0.334 seconds.