A Simple Linux Backup Method
You hear it all the time from writers and your geek friends: “You should back up your computers.” Well, here's one more geek to bug you about it again, but also to give some help.
This article describes the method that I
use to back up
my home Linux systems. It’s an easy method for a
non-technical
Linux user to back up her important data. We’ll discuss the
decisions you have
to
make in order to do a thorough backup.
This backup method can
be performed manually, but is better automated. I’ll give you
a
couple of small programs that I’ve written to help automate
this
backup method, along with step-by-step instructions to use the
software. We’ll also briefly look at a couple of other good
programs
to help you back up.
What does it mean to back up something? It simply means making a copy of that something for safekeeping, in case the original becomes unusable. The copy should be stored on different hardware than the original -- for example, you might copy a document in your Documents folder (which is on your hard drive) to a USB key, a CD, or even an on-line service.
Why and What to Back Up
Why should you bother making backups? Because computers fail: hard drives fail, making it impossible to get your data from them; systems and software crash, sometimes corrupting or deleting your important documents. And people fail too: have you ever accidentally erased a file that you needed? I’ve done it too many times. With a backup, you’re mostly protected: only the very latest changes, if any, are lost when you restore a backed-up file.
What should you back up? Your priority should be to back up those files that are most important and hardest to recreate. Here’s my take on the importance of backing up certain data from the perspective of a general desktop user, from most important to least:
-
Your files: This includes documents, spreadsheets, email, calendar data, financial data, downloaded music -- anything that you’ve created, recorded or received that has meaning and importance to you. These are clearly the most important and hardest to recreate, because you or others created them from imagination and hard work or because you paid for them.
-
Your settings: This includes changes you’ve made to personal settings: desktop configuration (e.g., colors, backgrounds, screen resolution, mouse settings, locale) and program options, such as settings for OpenOffice, Gimp, your music player, and your email program. These are easier to recreate than your documents, but you’d hate to lose them -- it takes time to recreate them.
-
System settings: Many people never touch their system settings -- the settings are created during Linux installation and stay that way. For those people, backing up system settings is less crucial than backing up their personal settings, since a re-installation would fix things. For people who customize their systems -- e.g., changing system configuration files in
/etc-- backing up these settings can be at least as important as backing up personal settings. -
Installed software (and everything else): This category includes installed system software (primarily Linux) and application software (such as OpenOffice, Firefox, and the Apache Web server). Such software can usually be restored by reinstalling, but not always.
Overview: A Simple Backup Method
In this section, I’ll outline a simple backup method that I use every day. One of its main strengths is that it’s simple -- easy to set up and use and it can be completely automated. Since I use this method to back up my home systems, the method as well as the main backup programs presented here have been tested in my environment for a couple of years.
What It’s Best Suited For (And What It’s Not)
The Data
The method presented here is best suited for backing up the high-priority items -- your files, your settings, and possibly system settings. And the amount of data you can back up is limited to the size of whatever you’re backing up to; we’ll discuss this further below.
The Backup Schedule
Backups are done once every day in a weekly cycle. On Mondays, the backup program performs a full backup -- it backs up everything you’ve asked it to. On other days, it does an incremental backup -- it only backs up files that have changed since the last backup. The program keeps all the backups for that week -- that is, it keeps the Monday full backup along with the Tuesday through Sunday incremental backups. That way, you can always restore any file that’s been backed up on any day of the week. When Monday comes around again, last week’s Monday backup is erased to make room for the new Monday full backup.
Automatic or Manual
The backups can be started automatically by your system at certain times of the day. We’ll discuss how to use the Linux scheduler tool, known as cron, to do this. Or you can manually start a backup, preferably at the end of the day after you’re done using the computer.
Back Up to What?
You have a lot of options when choosing what kind of device will receive the backup. Consider factors such as:
-
Portability - Do you need to use the device to back up several machines? Will the backup media (such as diskettes, an external hard drive or Iomega Zip disks) be carried off-site?
-
Capacity - How much data will be backed up? Remember that the media must hold up to a week’s worth of backup data with this backup method. Do you want to use multiple removable media, such as a set of Zip disks? If so, the backups can be spread among those media, so each one doesn’t have to hold the full week’s data – but you’ll need more disks, which raises the cost.
-
Speed - Some types of devices are much faster than others and can back up much more data very quickly. For example, copying data to a USB key or an external hard drive can be very fast, much faster than a Zip disk or diskette.
-
Cost - Larger or more flexible media usually cost more.
One very important consideration when choosing the backup medium is capacity -- how much you need to back up. With this simple backup method, the backup size for any single backup is limited to the size of your backup medium. For example, you can back up to diskette or Zip disk -- but only one diskette or Zip disk. You won’t be prompted for more when the first fills up. Of course, you can use another diskette or Zip disk for the next night’s backup.
Better, you can back up to a USB memory key -- again, only one. Best: you can back up to another hard drive, such as an internal drive, an externally-attached USB drive or one elsewhere on your network. This would allow for a complete system backup -- a backup of every file on your system -- if you so choose.
This simple backup method only works for read/write drives.
That
is,
it will back up to any drive that you can copy a file to -- a hard
drive, network drive, single diskette and single Zip disk. It will
not back up to a CD or DVD; you can’t do a cp
(copy) command to copy a file to those. Maybe I’ll add that
capability
in Release 2 (currently scheduled for around Feb. 2019, so
don’t hold
your breath :-). Of course, you can manually burn the resulting backup
archive
files to a CD or DVD.
Setting Up the Backup
Setting up your system for backup involves the following steps, which we’ll go through in detail:
-
Decide the backup medium -- that is, to what device are you backing up?
-
Decide if you want the backup to run automatically, and if so, add a scheduled backup task.
While you have to make these decisions yourself, the provided software can help implement those decisions. Look at the Simple Backup Software page to get the software prepared.
Now let’s look at each step in turn.
Decide What to Back Up
|
Software |
Deciding what to back up and then finding where it’s all stored is probably the hardest step in setting up a backup. Files and settings are stored in many directories, along with other data that you probably don’t want backed up.
We defined four categories of data above that can be backed up. Let’s look at each in more detail.
-
Your files. These are usually stored under users’ home directories, such as
/home/steve. They could be in a variety of subdirectories, such asDocumentsorMy_Documents, under the home directory. If you’re backing up your entire system as the root user, don’t forget to back up these items in everyone’s home directory, including/root. In fact, if your users’ home directories aren’t too large or your backup medium has sufficient space, consider backing up all of/home.If you can’t back up all of
/home, examine Table 1 below, which lists some subdirectories (of a user’s home directory) to check for important files. When looking at this table, please remember two things: first, remember that in Linux, “~/” represents your home directory, e.g. “/home/steve”; and second, remember that any file or directory name that starts with a period (“.”) is by default hidden. To view these in both KDE’s Konqueror file manager and Gnome’s Nautilus, select View from the main menu, then select “Show Hidden Files.” From a command line, typels -afrom within the appropriate directory to see all files.Table 1: Directories to check for important data to back up
Application or Document Type
Directories
What It Contains
Documents
~/Documents, ~/My Documents, ~/Desktop, ~/, othersUsually contains documents you actively created, such as word processing and spreadsheet files. If you store documents on your desktop or in folders on your desktop, look in the
~/Desktopdirectory. Also, check your home directory for any important documents.Scripts and programs
~/binIf you’ve written any scripts or small programs, you might have put them in this directory. Otherwise this directory might not exist.
KMail (email)
~/.kde/share/apps/kmailThese directories contain your stored emails and their attachments. For Evolution, the directory stores address, calendar and to-do data as well. The Thunderbird directory also contains address data.
Evolution (email, contacts, addresses, calendar, to-do)
~/.evolutionThunderbird (email)
~/.thunderbirdGnucash (financial)
Any
When Gnucash and KMyMoney first create data files, they ask where to save the files.
KMyMoney (financial)
(calendar/to-do)
~/.kde/share/apps/korganizerThese directories contain calendar files with your appointments, meetings and holidays, and to-do list items.
Sunbird (calendar/to-do)
~/.mozilla/sunbirdKAddressBook (contacts)
~/.kde/share/apps/kabcContains address book and distribution list files.
KNotes (sticky notes)
~/.kde/share/apps/knotesContains notes and configuration data.
-
Your settings. Your desktop environment and most applications save their settings in one or more text files. Most of these are in hidden files and directories in your home directory. For example, KDE stores its settings in the
~/.kdedirectory. Many KDE applications store their settings under this directory as well. We will treat bookmarks (for both browsers and file managers) as part of your settings rather than your data, so they’ll get consideration here as well.You should examine these hidden files and directories to see which you need to back up. Consider backing up the files and directories listed in Table 2 below, all of which would be in your home directory if they exist. Note that this list is undoubtedly far from complete; if you use different applications than I use, your list could be very different. Note that the asterisks (“
*”) in the list below indicate that anything can follow; for example, “.gimp*” would include “.gimp” as well as “.gimp-2.2”.Table 2: Files and directories to check for personal settings
.bashrc, .bash_profile (if you’ve customized them).eclipse.emacs.fonts.conf.gaim.gconf.gconfd.gimp*.gnome.gnome2.gnucash.gnupg.gtk-bookmarks.kde.local.lpoptions.mozilla (has settings for Mozilla, Firefox, and Sunbird).mplayer.ooo*, .OpenOffice*, .openoffice*, or OpenOffice*.profile (if you’ve customized it).realplayerrc.spamassassin.ssh.viminfo.Xauthority.xemacs.xine.xinitrc.XmodmapAs mentioned above, don’t forget to back up these items in every user’s home directory under
/home, and inroot’s home directory in/root.You may not want to back up everything in each of these directories. For example, several applications improve their performance by caching data in special directories. My Firefox cache directory at
~/.mozilla/firefox/xxxxxxxx.default/Cachehas about 30 megabytes of files that I don’t want backed up. You’ll see later how you can add such files and directories to an exclude list to ensure they are not backed up. -
System settings. Almost all system settings are stored in files in the
/etcdirectory. I recommend backing up the entire/etcdirectory; it’s generally not exceedingly large (on my systems, it’s less than 60 megabytes), and it can be hard to distinguish between what’s important and what’s not. Note that you’ll be able to back up everything in/etconly if you run the backup as therootuser. Otherwise, certain files and directories may not be accessible during the backup.The
/vardirectory may also contain some important data, but don’t back up all of it. For example, on my SUSE Linux 10.0 system,/varcontains several large cache directories that I do not back up, but also contains/var/spool/mail(user mail files) and/var/spool/cron(settings for cron), which I do back up.You should consider backing up the
/bootdirectory as well. This contains the system boot configuration, including the compressed Linux kernels, configuration data, and data for the GRUB boot loader (which lets you select an operating system when booting). Personally, I don’t back up this data because it’s not hard to restore. You may want to consider backing it up if you dislike reloading and reconfiguring software.If you’ve made changes elsewhere in the system, consider backing up those files as well. For example, I often add scripts or links to
/usr/binor/usr/local/bin, and I add just those files to the backup. Perhaps you’ve added fonts under/usr/X11R6or scripts in/bin-- you can add those files or directories to the backup as well. -
Installed software and everything else. For most desktop Linux users, installed software and software configuration files can be restored by reinstalling the system or the missing software, and thus there is little chance of permanently losing something. However, if you sometimes install software that you’ve purchased and downloaded from the Internet, you may want to consider backing up those applications separately; if your hard drive fails, you may not be able to download the software again. Also, reinstalling software or system drivers is not a whole lot of fun, even for geeks like me, so it may be worth your while to back up such software.
After this detailed examination of what
to back up,
there is
one
more consideration that could make the whole discussion moot. If you
have a sufficiently large and fast backup medium and enough time, why
not just back up the entire system? In other words, just specify the
root directory “/” as the
one directory to
back up. (If you do this, you probably want to exclude
network-mounted file systems -- it’s usually faster and safer
to back
them up directly rather than indirectly across a network.)
The Backup Medium -- Back up to What?
|
Software |
|
On
my SUSE systems, plug-in and
removable media are mounted in the |
As described above, the backup medium for this method can be any device that can have files copied to it. It works very well with USB memory or hard drive devices, network-attached drives, floppy disks, and Iomega Zip disks. For removable storage like USB memory and hard drives, all you have to know is where to find the device’s files when it’s plugged in to your system -- that is, where it is mounted.
Most modern Linux systems mount devices automatically when they are plugged in (USB, FireWire devices) or inserted (floppy or Zip disks). Most also put an icon on your desktop (SUSE Linux 10.0 puts an icon in a “My Computer” folder on your desktop). You can use these icons to find out where the device is mounted -- see the sidebar “Where Is It Mounted?” to the right.
Who Runs the Backup?
|
Software |
Should
the backup
be run as the root user or as a regular user?
That
depends on what data you’re backing up.
If you’re only backing
up files below your own, regular-user home directory, then running
the backup under your user id should work fine. But if you’re
backing
up system files or other users’ files, this may not work --
your
regular user id may not have sufficient privileges to access those
files.
As an example, my backups include the entire /etc
directory. However, my regular steve user id
cannot
access a few directories below /etc, such as
/etc/ssl/private and /etc/cups/certs,
and
also has no access to the home directories of my wife’s and
kids’
accounts. As a result, I run my backups as the root
user.
There are other, more complex solutions to this access
issue, but they’re beyond the scope of this article. If you
have
access problems, run the backup as root.
Automating the Backup
|
Software |
If
you leave your
computer on most or all of the time, you can have the backups run
automatically. That way you don’t have to remember to do it
at the
end of the day (or night, for we programmers).
We’ll use the
standard Linux scheduling tool called cron, a
service
that starts programs at scheduled times. The
“schedule” it
uses is called a crontab. You can change the
crontab with the
crontab -e command, or using a graphical tool
such as
KCron (part of KDE),
gnome-schedule,
or vcron.
There are only two things you have to do:
-
Make sure the
cronservice is set up. Your Linux distribution probably has a graphical tool to let you do this; use it if it does. For example, in SUSE Linux 10.0, you would use YaST Control Center, selecting the System category, then the System Services subcategory. Mandriva Linux is similar: from Mandriva Control Center, select System, then Services. Fedora Core has thesystem-config-servicescommand. Or you can use the Linux commandchkconfig:steve-lap:~ # chkconfig
Makefile off
SuSEfirewall2_init on
SuSEfirewall2_setup on
acpid on
alsasound on
atd off
autofs off
autoyast off
...
boot.scsidev off
chargen-udp off
cron on
cups onThis command shows which services are on and which are off. The list of services can be very long, so you may want to run the command as
chkconfig | grep cron. If thecronservice is off, run the following two commands (in bold and underlined below), which turn it on and then double-check that’s it on:steve-lap:~ # chkconfig --set cron on
steve-lap:~ # chkconfig | grep cron
cron on
steve-lap:~ # -
Schedule your backup. Now, run either KCron, gnome-schedule, or vcron to schedule your backup. For KCron, type
kcronfrom a command line to start KDE’s scheduler tool (or the Simple Backup Configuration Program can start it for you). If you want to run thebkupscript as a regular (non-root) user, kcron is simple: just right-click on the Tasks item and select New to open the Edit Task dialog (see Figure 1). In the Comment field, type a comment such as “Everyday system backup.” In the Program field, enter the full path to thebkupscript supplied with the software, followed by a space, followed by the full path of the directory to the configuration files (by default,~/.simplebackup). Next, click the “Run every day” checkbox. Select the time – as an example, select the “23” button under Hours and the “0” button under Minutes for 11:00pm. Finally, click the Ok button to save the new task.Next, right-click on “Variables” and select New. In the Edit Variable dialog, select “SHELL” for the Variable field, and for Value enter “
/bin/bash”. This is needed because thebkupscript needs to run in Bash.If you want to run
bkupasroot, you must also run kcron asroot. The screen looks more complicated (see Figure 2) because you can schedule tasks for any user; just click the
sign next to
“System Crontab” or next to
“root” (either will work fine), and then
proceed as described above to add the task and the SHELL variable.
And that’s it! Assuming you remember to leave your computer on, it should now automatically perform the backup at the time you specified.
Outputs of the Backup
Checking That the Backup Worked
After a backup has run, it’s a good idea to check that everything worked as expected. In the Advanced Backup Options, you specified a directory for log files. These log files contain the outputs from the backup script’s commands. Have a look to see if there are any error messages.
The Backup Files
The bkup script stores all backed-up
files in a
compressed archive file. Specifically, the archive is a
gzip-compressed
tar archive. You
can find these files, one for each day of the week, in the directory
you specified in the Where
to Back Up page of the Simple Backup Configuration Program.
Restoring Files From the Backup
For now, restoring backed-up files is a manual process – you can’t do it using the Simple Backup Configuration Program. You must look in each archive file, from newest to oldest, to see if it contains the file that you want to restore. The process is described in the steps below; I hope to automate this in the future.
-
Plug in or insert the backup device to mount it, if necessary.
-
Find the directory containing the backup files. You specified this directory in the Where to Back Up page of the Simple Backup Configuration Program.
-
Determine which is the newest archive file in the directory, using the file’s date and time. In Konqueror and Nautilus, you can view the files’ dates and times and sort the directory by date/time. From a command line, issue the command
ls -ltr. -
Look in the archive file to see if it contains what you want to restore:
-
In Konqueror or Nautilus, open the archive file by double-clicking on it. Konqueror will simply display the archive’s contents; Nautilus by default will open Gnome’s Archive Manager (File Roller). (You can also right-click on the archive in Konqueror and select the Open with Ark option to open KDE’s archive manager.)
-
If you prefer the command line, you can issue a command to list all files in the archive and then
grepon the name of the file you want to restore:steve-lap:/media/usbdisk # tar tzf Backup.Wed.tar.gz | grep bookmarks.html
home/steve/.mozilla/firefox/xxxxxxxx.default/bookmarks.html
steve-lap:/media/usbdisk #
-
-
Restore the file by using either your file manager or the command line:
-
In Konqueror or File Roller, navigate to the file that you want to restore. Open another Konqueror or Nautilus window and navigate to the directory where you want to put the restored file. Then just drag the file from the archive window (Konqueror or File Roller) and drop it in that directory.
-
To restore a file from the command line, change to the root directory (“
/”) and issue atarcommand with the appropriate filenames:steve-lap:~ # cd /
steve-lap:/ # tar xvzf /media/usbdisk/Backup.Wed.tar.gz
home/steve/.mozilla/firefox/xxxxxxxx.default/bookmarks.html
steve-lap:/ #Replace the two filenames listed in blue and red above with your archive filename and the name of the file that you want to restore. You can restore more than one file at a time if you wish. Note that the
tarcommand above, including the blue and red filenames, should be entered as a single command; the arrow (
) above
indicates that this is a single command that just didn’t fit
on one
line in the browser.
-
Other Backup Software
There are many, many different ways to back up your system and your important data. You can certainly do it yourself -- just copy your important files and directories onto a suitable backup medium. Plenty of Linux software has been written to help with backups – as of this writing, Freshmeat.net lists 151 projects for Linux under the “Backup” category. While I haven’t tried most of these, a couple are worth mentioning.
-
KDar (KDE Disk Archiver): A very nice KDE backup program. Has a good interface that’s fairly easy to use. One advantage is that it can create split backups, such as a backup that spans multiple Zip disks. Depends on the user having the “dar” package installed.
-
Amanda (Advanced Maryland Automatic Network Disk Archiver): Geared towards system administrators, but it comes with most Linux systems. Good choice if you have some Linux configuration and command-line expertise, as it doesn’t come with a graphical user interface.
-
Distribution-specific: Some distributions provide their own simple backup software. For example, SUSE Linux 10.0’s YaST system configuration tool contains a decent-looking tool, although I’ve never tried it. Mandriva Linux also supplies backup software with its configuration tools.
There are many others out there on Freshmeat; check them out at your leisure if you don’t find this method sufficient for your needs.