Tips for Simple Backup

Note: This tip has been expanded into a full-fledged article with complete software.

Did you know that your Linux system already has all the software you need to easily back up your data? This tip shows you how to do it.

Caveats: This method is best for backing up limited amounts of data, because it can only back up to a read/write file system. For example, it can back up to another hard drive on your computer (internal or external USB), a network-attached drive, a drive on another computer in your network, a single diskette, or a single Zip disk. It cannot back up directly to a CD or DVD, since these are not read/write file systems; however, you can manually burn the resulting file to a CD or DVD if it will fit. Also, this method will not back up data to multiple diskettes or Zip disks.

Here's what you need to do:

  1. Decide which user will run backup: If you're backing up a lot of system settings (such as in /etc), you should run the backup as the root user. If you're mostly backing up personal files and settings, run it as your regular non-root user.

  2. Decide which files to back up: This method works best with a limited amount of data; back up only the important stuff, like your documents, financial data, and certain desktop and system settings.

    Now, create a text file with a list of full path names to the directories and/or files you want to back up. Put the file in a new directory, e.g. ~/backup in the root or regular user's home directory. See the example below, adapted from my system; I call the file BackupFiles.txt.

    /etc
    /home/user1
    /home/user2/Documents
    /home/user3
    /home/steve
    /root
    /mnt/win_d/Network_Backups/Pogo/bkup_pogo
    /mnt/win_d/Network_Backups/cdrw_backup
    /mnt/win_d/Network_Backups/Pogo/BackupFiles.txt
    /mnt/win_d/Network_Backups/Pogo/ExcludeFiles.txt

    In a separate text file, put a list of files and directories to exclude from backup  -- if any -- call it something like ExcludeFiles.txt and put it in the new ~/backup directory.


    /home/steve/Download_Files
    /home/steve/My_Documents/Video
    /home/steve/My_Documents/Pictures
    /home/steve/My_Documents/Work-Related
    /home/steve/tmp
    /home/steve/.kdevelop-doc
    /home/steve/.kde/share/apps/kdevelop/KDE-Documentation
    /home/steve/.kde/share/cache
    /home/steve/.java/deployment/cache
    /home/steve/.thumbnails
    /home/steve/evolution/cache
    /home/steve/.mozilla/Steve/rjgj3hy8.slt/Cache
    /home/steve/.mozilla/firefox/default.3c7/Cache
    /home/steve/.galeon/mozilla/galeon/Cache
    /home/steve/.bzf/cache
    /home/steve/Software/eclipse
    /home/steve/SpaceTripperDemo
    /home/steve/mnt
    /home/steve/win
    /home/steve/win.tar.gz
    /root/tmp

  3. Decide to where you'll back up: As stated earlier, you must back up to a read/write file system. If you're using a hotplug device such as a USB hard drive, it's most likely mounted read/write for you when you plug it in. Find out where that mount point is by clicking on the device's icon and looking at Konqueror's Location bar; for example, my USB hard drive is mounted to /media/LACIE.

  4. Run the backup (manually): You'll need to repeat this step every time you back up -- every night. After mounting the output drive, change to the root directory and run the tar program from the command line to copy the files:

    mount /media/LACIE
    cd /
    tar -czvf /mnt/LACIE/backupfile.tar.gz -T BackupFiles.txt -X ExcludeFiles.txt

    This backs up all files that are specified in BackupFiles.txt (but not in ExcludeFiles.txt) to a backup archive called backupfile.tar.gz on the backup disk (/mnt/LACIE). The -z option causes the tar command to compress the backup archive using gzip, which allows you to fit more data onto your backup drive.

  5. Add sophistication with full and incremental backups: A full backup backs up all the files you specified; an incremental backup only backs up files that have changed since the last backup. For both types of backup, use the -g option on the tar command, specifying the name of a snapshot file. The snapshot file simply records information about the files that were backed up, so that tar can recognize which files have changed when doing an incremental backup. Also, we'll add the day (Tuesday in the example below) to the filename to help distinguish the backups:

    mount /media/LACIE
    cd /
    tar -czvf /mnt/LACIE/backupfile.Tue.tar.gz -T BackupFiles.txt -X ExcludeFiles.txt -g snapshot.txt
    How do you perform a full versus an incremental backup? Simple:
    • To do a full backup, just erase the snapshot file (rm snapshot.txt) before the tar command. tar will create a new snapshot file during the backup.
    • To do an incremental backup, don't erase snapshot.txt. Pretty simple, huh?

  6. Automate: The easiest form of backup is one that you can forget about. Next, we'll put all the necessary commands into a simple script, and use the built-in Linux scheduler tool called cron to run it for us every night. Here's a very simple script called simple-bkup.sh as an example; you can download it here:

     1 #!/bin/bash
    2 #
    3 # Simple script to back up data to a mounted read/write file system.
    4 #
    5 finalLoc="/media/LACIE"
    6 bkloc="$HOME/backup"
    7 snapfile="$bkloc/snapshot.txt"
    8 bkfiles="$bkloc/BackupFiles.txt"
    9 exfiles="$bkloc/ExcludeFiles.txt"
    10
    11 # If it's Monday, erase the snapshot file, which forces a full backup
    12 dayofweek=`date +%a` # Gets the day of the week in 3-letter format
    13 if [ $dayofweek = Mon ]
    14 then
    15 # It's Monday; erase the snapshot file so we do a full backup
    16 rm -f $snapfile
    17 fi
    18
    19 # Try to mount the backup device if not already mounted.
    20 mount | grep "$finalLoc"
    21 if [ $? != 0 ]
    22 then # grep didn't find $finalLoc, so drive isn't already mounted
    23 # Try to mount the backup drive ($finalLoc); if unsuccessful, print an
    24 # error message, compress the log, and exit the script.
    25 mount "$finalLoc" -o rw
    26 rc=$?
    27 if [ $rc != 0 ]; then
    28 echo "Error $rc mounting $finalLoc"
    29 exit 1
    30 fi
    31 fi
    32
    33 cd /
    34 # Back up files listed in $bkfiles (but not in $exfiles) to $finalLoc. Create a log file to
    35 # record the tar command's results; the log file is placed in the $bkloc directory.
    36 tar -czvf $finalLoc/backupfile.$dayofweek.tar.gz -T $bkfiles -X $exfiles -g $snapfile >$bkloc/bkup.$dayofweek.log 2>&1

    Change the blue, highlighted text to values appropriate for your system. For finalLoc, change /media/LACIE to your backup device's mount point; for bkloc, change $HOME/backup to a directory where you want to keep backup information. Save this script in, say, your ~/bin directory. Don't forget to make it executable; in Konqueror, right-click on the file, select Properties, select the Permissions tab, and click on the appropriate check boxes to make it executable. Or you could instead type chmod +x simple-bkup.sh on a command line.

  7. Schedule: Now, schedule the backup to run every night at, say, 11:00pm.  From a command line, type kcron to start KDE's scheduler tool. If you want to run simple-bkup.sh as a regular (non-root) user, kcron is simple; just right-click on the Tasks item and select New (

    KCron's new task dialog
    Figure 1. KCron's Edit Task dialog.
    Figure 1). Type a comment such as "Everyday backup," enter the full path to the simple-bkup.sh script that we created above, and click the "Run every day" checkbox. Select the time -- for 11:00pm, select the "23" button under Hours and the "0" button under Minutes. Finally, click the Ok button to save the new task.

    Image of KCron's main window
    Figure 2. KCron's main window for a root user, with System Crontab expanded.
    If you want to run simple-bkup.sh as root, you must also run kcron as root. The screen looks more complicated (Figure 2) because you can schedule tasks for any user; just click the Plus '+' sign next to entries in KCron sign next to "System Crontab" or next to "root" (either will work fine), right-click on Tasks, select New, and then proceed as described above.
That's it; you're done. Now sit back and relax, knowing that your system will automatically back up your important files every night. Well, if you remember to leave it on.