Site Sponsors:
Incrementally Backing Up Changes to a USB Drive in Linux 

Saving Changes Anywhere?

Whenever we want to back up a Linux file system to a FAT / FAT32 file system, strange things can happen!

The need was to help us incrementally, and recursively, back-up file changes made during the past few days to a huge USB drive. -Since FAT partitions do not have all of that POSIX info, we were getting tired of waiting while absolutely everything was being copied over - even things that had not changed.

Unless you want to re-format the FAT partition to use NTFS (or EXT`n`), then there are two techniques here. The first technique also creates an on-line archive. The second works a lot like Mr. Bill's xcopy.

Technique 1: Coding It

Because the following script uses tar rather than cp, your file dates and time stamps will be preserved. Even ctime, mtime, or atime ... all the time!


file_name="`basename $0`"


echo 'Started at' `date` ", looking for deltas within $days days"

echo 'Source = '$source', Destination = '$dest
echo 'TAR file is ' $file_tar
echo 'FIND file is ' $file_tar_list

echo ' '
# echo 'Cleanup started at ' `date`
# $(find $source -name *.class -print -exec rm '{}' \; )
# $(find $dest -name *.class -print -exec rm '{}' \; )

echo ' '
echo 'Tar-up started at ' `date` ' from ' $source
pushd . > /dev/null
cd $source
find . -type f -mtime -$days -print > $file_tar_list
tar cvf $file_tar -T $file_tar_list
popd > /dev/null

echo ' '
echo 'Un-tar started at ' `date` ' to ' $dest
tar xvf $file_tar -C $dest

echo ' '
echo 'Completed at ' `date`

Since I have been writing a lot of Java this decade, I included a way to remove all of those pesky .class files. Just remove my # comments before those first find statements if you have a similar need.

Calling It

Once you copy the above into a file name (such as in the below,) all you need to do next is to call it. The script expects a pair of folders on its command-line:



file_name="`basename $0`.rpt"

./ $source $dest | tee $file_name

Designed to be reflective, you can even swap the source and destination file around with impunity:



file_name="`basename $0`.rpt"

./ $source $dest | tee $file_name

Of course, it is of no importance if we use the above on CentOS, Ubuntu, Slackware, or DSL. -This backup routine will work the same wherever a console / terminal / command-line / secure shell / or other script-running shell interface is available.


Perfect for pen-drives, hard disks, or other mountable media, this script will leave a copy of the .tar file on your hard-drive in your home directory ($HOME) after all is said and done. A copy of the report file will also be placed right next to the script for your perusal & retroactive peace-of-mind, as well.

Technique 2: Preservations

If you do not want to have an on-line archive laying around, then you can just use:

for arg in "/my_folder1" "/my_folder2"
cp -r -u -v --preserve $arg "./"

Simply copy & paste the above into a file, mark that file as executable, then modify and copy the script to where you would like the backup to store YOUR folders - like on a USB drive.

Best of all, note that running the operation repeatedly will only copy the 'deltas' (changes,) not everything else.


Clearly both techniques are superset operations: The only potential down-side is that previously deleted files will still be left hanging-around your backups. So if you want verbatim archives, then either delete the backup folders from time to time, else copy the tar file, as described earlier, to your backup device.

Finally, note that the above operations also work the same on NTFS, as well as on all EXT-n, partitions.


R.A. Nagy

NOTE: If your are backing up to a POSIX file system and want to keep a log, then you might want to also consider this article.

Before any backup strategy however, don't forget to watch for, as well as reclaim any Zombies!


Add Comment
Comments are not available for this entry.