Automatically Backing up a USB Drive with RSYNC (KDE)

USB Drives of all kinds need to be backed up and the best backup is an automatic one (it’s the only way to make sure that it gets done!).

So here is one recipe for doing just that using RSYNC and some BASH scripting magic.

I’ve split this into two files. You don’t have to do this of course and one may well be better for you. I used two because I can run the second one manually as well. Put everything in if you want to backup each drive individually, however, note that KDE produces an annoying extra dialog (a security warning) asking if you really want to run the autorun.

    This resides in the root of the USB drive and is executed automatically by KDE when the drive is detected (though not if the drive is attached when booting)
    This is a bit of a nasty hack, I have manually configured a list of drives that might be attached so that I can back them all up together. Not elegant but it works for me.


# KDE will automatically run an executable file called: .autorun, autorun or (in that order)
# Alternatively, a non-executable file called .autoopen or autoopen can contain a file name
# of a non-executable file on the media which will be opened with the default app for that file.
# See:

# Also see:
# for some interesting ideas

# Where are we running from? e.g. /media/usbpen1
mediaDir=$(echo $0|sed 's/autorun//')

kdialog --title "USB Drive Backup" --yesno "I'd like to backup the USB drives, can I?"
if [ $? = 0 ]; then
echo " OK Selected, I'm going"
echo "Autobackup run: `date`" >usb-linux-auto-backup.log
exec ~/bin/
echo " Cancel selected, so do nothing - bye."
#! /bin/bash


echo "Starting USB Backup: `date`"
echo "Starting USB Backup: `date`" >~/Backups/usb-backup-manual.log

# From
# To

dcopRef=`kdialog --progressbar "Starting backup - press cancel to stop further processing (no next step)" 4`
dcop $dcopRef showCancelButton true

#until test "true" == `dcop $dcopRef wasCancelled`; do
for f in "CF2G1" "SD1G1" "USBPEN1" "USBPEN2"
dcop $dcopRef setLabel "Backing up $MNT/$f  ==>  $TO"
echo "--------------------------------------"
echo "$f  ==>  $TO"
inc=$((`dcop $dcopRef progress` + 1))
sleep 2
if [ -e $MNT/$f ]; then
  dcop $dcopRef setProgress $inc
  RSCMD="rsync --recursive --times --delete-during --stats --human-readable -h $MNT/$f $TO"
  echo $RSCMD
  echo $RSCMD  >>~/Backups/usb-backup-manual.log
  dcop $dcopRef setLabel "RSYNC for $f finished"
  dcop $dcopRef setProgress $inc
  dcop $dcopRef setLabel "$MNT/$f not mounted"
  echo "$MNT/$f not mounted"
  echo "$MNT/$f not mounted"  >>~/Backups/usb-backup-manual.log
echo "======================================="
sleep 2

dcop $dcopRef close

echo "End: `date`"
echo "End: `date`" >>~/Backups/usb-backup-manual.log

Note the use of KDialog to provide a minimal GUI. In the second file, KDialog produces a progress bar.

Also note the RSYNC parameters. These are always painful to get to grips with so it is nice to have an example to work from. In this case I am backing up so I am making sure that the backup is an exact copy of the original (as opposed to synchronising which would allow changes to happen on either side).

  • inktvis75

    Thanks for your script, i used it as the basis for my own script:

    [Desktop Entry]

    [Desktop Action syncdir]
    Name=Synchronize Folder
    Exec=/usr/local/bin/ %u

    doeldir=`kdialog –getexistingdirectory /media`
    qdbusRef=`kdialog –progressbar "Synchronisatie voortgang"`
    qdbus $qdbusRef showCancelButton true

    for i in `find $folder -name '*' -type d`
    qdbus $qdbusRef setLabelText "Synchroniseer $folder/$i naar $doeldir"
    echo "————————————–"
    echo "$i ==> $doeldir"
    inc=$((`qdbus $qdbusRef org.freedesktop.DBus.Properties.Set org.kde.kdialog.ProgressDialog value` + 1))

    sleep 2
    if [ -e $i ]; then
    qdbus $qdbusRef $inc
    RSCMD="rsync –recursive –times –delete-during –stats –human-readable -h $i $doeldir"
    echo $RSCMD
    qdbus $qdbusRef setLabelText "Synchronisatie $i gereed "
    qdbus $qdbusRef $inc
    echo "ERROR"
    echo "======================================="
    sleep 2

    qdbus $qdbusRef close

    Frederik Vos ([email protected])