Painless Thumbdrive Backups
backup-thumb.sh is the engine that backs up your thumbdrive. Our rule calls it, giving the name of the device (the SYMLINK) as its only argument. Everything else is configured in the CONFIG section. The backup script is shown in Listing 1.
Listing 1. Backup Script
#!/bin/bash
# Thumbdrive backup script from <citetitle>Linux Journal</citetitle>
# ##############################################
# CONFIG section
# where you want the backups to be kept
BACKUP_DIR=/backups/thumb
# how many old backups to keep
GENERATIONS=7
# backup only once a day
# set to 0 if you want a backup run every time
# you insert your thumbdrive
BACKUP_ONCE_DAY=1
# completion sound to play when backup is done
SOUND=/usr/share/sounds/KDE_Beep_ClockChime.wav
# END CONFIG
# ##############################################
# main program
# wait for device to settle
sleep 10
# make sure no one will be able to copy our backups
umask 077
# check the directory
DEVICE=$1
if [ ! -d ${BACKUP_DIR} ] ; then
mkdir -p ${BACKUP_DIR}
fi
# only backup once per day
if [ ${BACKUP_ONCE_DAY} -gt 0 ] ; then
DIDTODAY=${BACKUP_DIR}/${DEVICE}.did_today
find ${BACKUP_DIR} -name ${DEVICE}.did_today -a -mtime +1 -delete
if [ -f ${DIDTODAY} ] ; then
exit
else
touch ${DIDTODAY}
fi
fi
# rotate backups
cd ${BACKUP_DIR}
let GENERATIONS=${GENERATIONS}-1
while [ ${GENERATIONS} -ge 0 ] ; do
let NEWFILE=${GENERATIONS}+1
if [ -f ${DEVICE}.backup.${GENERATIONS} ] ; then
mv -f ${DEVICE}.backup.${GENERATIONS}
${DEVICE}.backup.${NEWFILE}
fi
let GENERATIONS=${GENERATIONS}-1
done
# do the backup
dd if=/dev/${DEVICE} of=${BACKUP_DIR}/${DEVICE}.backup.0 > /dev/null 2>&1
# notify that we're done
aplay ${SOUND} > /dev/null 2>&1Put this script in /usr/local/bin/backup-thumb.sh, and remember to chmod +x it. Next, edit the CONFIG section—the parameters are as follows:
BACKUP_DIR: where you want the backups to go.
GENERATIONS: how many days of backups to keep. Backups will be numbered 0 (most recent) to the limit you enter (oldest). Keep in mind that you need to have enough storage space for this many backups. If you are backing up a 1GB fob and set GENERATIONS to 7, backups will consume 7GB of space.
BACKUP_ONCE_DAY: if you plug and unplug your fob multiple times a day, you probably won't want to back it up each time. backup-thumb.sh uses a tag file so that it backs up only once per day. If you want to change this so it runs a backup every time you plug in a thumbdrive, set BACKUP_ONCE_DAY to 0.
SOUND: in this example, I've chosen a sound from the KDE distribution, but any WAV file will work. You easily can modify the script to use madplay instead of aplay and use an MP3 file as your completion sound.
backup-thumb.sh sleeps for ten seconds on startup, because it must wait for the kernel to finish scanning the thumbdrive. If you plug in a thumbdrive and type dmesg, you'll see a “waiting for device to settle” message while this happens. Ten seconds for the kernel scan should be sufficient even for older machines.
Next, backup-thumb.sh sets permissions tightly so that only root can read the backups. Otherwise, some nefarious person could copy your backup to a different machine and mount it there.
The script executes a simple dd (bit-for-bit copy) of your thumbdrive to a backup file. This works whether the device is encrypted or not. When it's finished, it plays a noise you will hear on your computer's speakers. On a USB 2.0 port, backing up a 1GB thumbdrive takes about one minute.
If you lose your thumbdrive and want to restore your backup to its replacement, simply dd the backup image to the new thumbdrive, like so:
dd if=corsair_drive.backup.0 of=/dev/corsair_drive
Or, if you want to grab only some files from the backup, do the following:
mkdir /mnt/thumb mount -o loop corsair_drive.backup.0 /mnt/thumb
You now can copy the files from /mnt/thumb.
If you're using TrueCrypt to encrypt your thumbdrive, you can mount the backup image in much the same way:
truecrypt corsair_drive.backup.0 /mnt/thumb/
That's about as painless as we can make thumbdrive backups. If you're too lazy to plug your drive in and come back when it beeps...well, stay away from laundromats!
Andrew Fabbro is a senior technologist living in the Portland, Oregon, area. He's used Linux since Slackware came on floppies and presently works for Con-way, a Fortune 500 transportation company.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- RSS Feeds
- What's the tweeting protocol?
- New Products
- Trying to Tame the Tablet
- Validate an E-Mail Address with PHP, the Right Way
- IT industry leaders
2 hours 17 min ago - Reply to comment | Linux Journal
19 hours 5 min ago - Reply to comment | Linux Journal
21 hours 38 min ago - Reply to comment | Linux Journal
22 hours 55 min ago - great post
23 hours 30 min ago - Google Docs
23 hours 52 min ago - Reply to comment | Linux Journal
1 day 4 hours ago - Reply to comment | Linux Journal
1 day 5 hours ago - Web Hosting IQ
1 day 7 hours ago - Thanks for taking the time to
1 day 8 hours ago
Enter to Win an Adafruit Prototyping Pi Plate Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Prototyping Pi Plate Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.




Comments
Please see my question above
Please see my question above - no one ever responded with any help. I thought it was because no one read this - but there have been posts since.
Automatically backup any USB storage device
Hi
I have a server that only serves as backup server.
I've been trying to make a udev rule that would automatically backup any USB storage device connected.
This way, I can just plug in my USB devices to my server, and it is automatically backed up.
However, what I have made so far does not work. I hope you can help me.
Here is my udev rule:
BUS=="scsi" KERNEL=="sd?1" SUBSYSTEM=="block" ATTRS{removable}=="1" ACTION=="add" RUN+="/usr/local/bin/copy-drive.sh %k"I hope you can help.
Cannot get this script working
I read you magazine regularly and was glad when I found this article. However I cannot get it working at all --
I am running Ubuntu 8.04
For starters, when I run the udev check I do not get any values that speficy SYSFS, they are all ATTRS (although there is a line for serial and model) - also there is no BUS line at all.
I tried writing the rule using ATTRS and nothing, I also tried writing the rule using SYSFS (even though that parameter did not appear) and nothing -
They symlink is not even created.
HELP please
So I never heard back on this???
Well, I asked for help and I never heard back on this - the output of udevinfo -a -p $(udevinfo -q path -n /dev/sdf) does not result in anything similar to what you are suggesting. There is no sysctl line or bus line - i see similar values in here but they are labelled attrs and the udev script just won't work
This is what I ended up making
root@lighthouse:/etc/udev/rules.d# cat 96-backuphome.rules
BUS=="usb", SYSFS{serial}=="0010101640150EE9W", SYMLINK=="tosh_ext", RUN+="/home/benito/scripts/homebackuponplugin.sh tosh_ext"
root@lighthouse:/etc/udev/rules.d#
Although usb is listed as
SUBSYSTEMS=="usb"
DRIVERS=="usb"
NOT the BUS (although I know that it is) ... and serial looks like ATTRS{serial}== not SYSFS{serial}== as suggested in this article. I figured the reason I hadn't heard back is that no one read this. I read your magazine monthly - maybe I shouldn't bother if I can't get a reply?
Thanks,
Benjamin
How to recover
At the end of your article, you state that you can recover files from the image usong mount like so:
mkdir /mnt/thumb
mount -o loop corsair_drive.backup.0 /mnt/thumb
My own thumb drive (and most such drives, I think) has the data partition on a partition within the drive. You have to use a different mount command:
mount -o loop, offset=xxxxx corsair_drive.backup.0 /mnt/thumb
Determining the value of the offset is a pain. I found a script at http://www.number.ch/wiki/index.php/PartitionRecovery that makes it much easier:
#/bin/sh
offset=$1; shift
limit=$1; shift
while [ $offset -le $limit ]
do if mount -o ro,loop,offset=$offset $* 2> /dev/null
then echo " Successfully mounted starting from offset $offset."
exit 0
fi
offset=$(($offset+1))
[ $(($offset % 1000)) == 0 ] && echo -n . # Progress indicator
done
echo "No filesystem found up to $offset."
exit 1
I found the article very useful. Thanks.
Derek Tattersall
usb key partitions
I found it instructive to write the run rule like this:
RUN+="/usr/local/bin/usbkey.sh myserialnum %k"
Then in the script I added echo $0 $@ >>/tmp/log.txt
I found that the script was called several times, with different device names. Then I removed the echo and added an if [ "$2" = "sdb2" ]
so I could mount only the partition I wanted.
I added a mount line in /etc/fstab and used the uid= and gid= to set myself as the owner. I have the script mount the drive, and luckily enough, when I remove the key, the mount goes away.
BTW, I don't use the key for backup, but I've found that the "unison" utility is very useful for syncing the data on the key with the data on either of two systems.
What I'd really like to do is to pop up a window, like gnome-terminal or xterm or something, and then execute an optionally interactive script. Anybody know how to do that? I tried sudo -u jpipkins gnome-terminal, but that didn't work.
Correction/Diff that worked for me
Great article. This is something I've had to manually do and now I'm free of that task. Yahooo!
I struggled a little at first because it just didn't work straight away. I'm running Ubuntu Edgy. Then I followed the link to Daniel Drake's "Writing udev Rules" and noticed that his examples all used "==" instead of "=". Each declaration in the udev rule seems to need 2 math symbols. They should be "==" or "+=". Here is my rule and it worked great. Oh the joy!
BUS=="usb", SYSFS{serial}=="00176F962D19E", SYMLINK=="cruzer", RUN+="/usr/local/bin/backup-thumb.sh cruzer "
Now I just need new laptop with USB2.0 as a gig thumbdrive takes 30 mins to backup.
rsync
Why not use rsync to backup only the modified files? It isn't difficult if the flash isn't encryped.
Regards,
Luis Sismeiro
generations
Rsync is a great solution for keeping files and/or directories in sync, and is much faster than copying the whole thing each time.
But, rsync is not so great if you sync, then realize that you need a specific version of a file from 2 days ago.
--
Bill Arlofski
Reverse Polarity
rsync snapshots
You can get the best of both worlds though (speed of rsync + multiple versions), with the added bonus of consuming less space than multiple full copies.
http://www.mikerubel.org/computers/rsync_snapshots/
The solution described there gives the illusion of multiple full copies, while only requiring the space of one copy plus the sum of the deltas.