Backing Up and Restoring User Data
This is a short guide on creating a full backup of your personal user data on Linux. It describes what gets backed up and why, illustrates best practices, and provides the exact steps needed to both back and up and restore your data safely.
An Introduction to Home Directories and Their Contents
What's a Home Directory?
Linux adheres to the Filesystem Hierarchy Standard which stipulates that all standard user data be kept under the /home
filesystem. Each user on the system is given a subdirectory under /home
that corresponds to his or her username, so Joe Student's home directory would be /home/jstudent
, assuming that his username is jstudent
.
This is very similar in concept to a user profile in Windows. However, unlike Windows, a user's home directory is the only location that any given user has permission to write to, unless other privileges have been granted by an administrative user.
Standard Contents
A typical Ubuntu home directory has several empty directories and one regular file created in it by default, as shown by ls
:
Desktop examples.desktop Pictures Templates
Documents Music Public Videos
Anything in these directories, as well as any other files placed in your home directory, are natural candidates for being backed up.
Dotfiles
However, there are other files and directories in your home directory which may not be readily apparent at first. These are generally known as dotfiles because they begin with a dot ('.'). Much like hidden files in Windows, they do not show up in most file listings unless explicitly enabled. Running ls
on the same home directory above with the -a
option to show dotfiles reveals:
. .dmrc .gnupg Public
.. Desktop .gstreamer-0.10 .pulse
.bash_history .esd_auth .gtk-bookmarks .pulse-cookie
.bash_logout examples.desktop .gvfs Templates
.bashrc .fontconfig .ICEauthority .update-notifier
.cache .gconf Music Videos
.config .gconfd .nautilus .Xauthority
.dbus .gnome2 Pictures .xsession-errors
Desktop .gnome2_private .profile
These files are created and used by any number of programs to store things like program settings and preferences, temporary files, and so on. Some very commonly used programs make extensive use of dotfiles:
Program |
Dotfile(s) |
Example Contents |
Firefox |
.mozilla |
Bookmarks, addons, preferences |
Thunderbird |
.mozilla-thunderbird |
Local Folder storage, addons, mail account settings, preferences |
OpenOffice |
.openoffice.org |
Preferences, auto-correct settings, custom styles |
Emacs |
.emacs |
Custom configuration |
Vim |
.vim, .vimrc, .viminfo |
Custom configuration, command history |
Bash |
.bashrc, .bash_profile |
Shell customizations |
Matlab |
.matlab |
Command history, preferences, shortcuts |
Pidgin |
.purple |
IM profiles, logs, passwords, and misc. configuration |
Most dotfiles are named after the program that creates them, but as Firefox and Thunderbird illustrate, there are occasional exceptions.
Deciding What to Backup and Restore
Backing Up
Given all of the various files on your home directory, it may seem a little daunting to know what to back up and what to ignore. It is always better to back up too much than too little, so when creating your backup archive, always include the full contents of your home directory, including all of your dotfiles, whether you know if you need the files or not. With a full backup, you'll always be guaranteed that whatever files you need are available to you. That's a far safer proposition than doing a partial backup and attempting to recover your data later, only to discover that you missed something important.
Restoring
Unlike creating your backup, restoring the entire contents of your backup archive over top of your new environment in one fell swoop is not generally considered to be a good idea. While you more than likely won't run into problems with your regular (i.e., non-dotfile) data, dotfiles are another matter entirely simply because they often contain program configuration information that may not necessarily be compatible with your new environment.
Some programs' dotfile data is more portable than others – that is, assuming the same or reasonably similar program versions, their dotfile data can be moved from user to user, or even from one operating system to another, without much of a problem. Firefox and Thunderbird are perfect examples of this; their .mozilla
and .mozilla-thunderbird
directories are easily movable between systems, and Firefox will generally be able to convert an older user profile to whatever is required for the latest version.
Other dotfile data is not so easily portable, and perhaps the most readily apparent example may be the GNOME Desktop. Its many applications store an extensive amount of user configuration data in numerous directories including (but not limited to!) .config
, .gconf
, .gconfd
, .gnome2
, gnome2_private
, .gstreamer-*
, .gvfs
, and .nautilus
. The problem stems from the fact that GNOME is constantly undergoing rapid development, and program configuration is not always completely compatible between older and newer software versions. The ultimate result for the end-user, when attempting to restore these files between dissimilar versions or Linux distributions, is often a broken desktop that lacks newer functionality and features.
With the thousands of programs available for Linux, it's impossible to say exactly which files you'll want to restore and which ones you're better off ignoring. However, there are some general questions to consider when deciding whether or not to restore a dotfile:
-
Do you know what the particular dotfile is and what information it contains?
-
Have you modified the dotfile yourself in an editor?
-
Does the dotfile's parent program store some form of user data above and beyond basic program options (e.g., bookmarks or addons)?
-
Would it take a significant investment of time to re-customize the dotfile's parent program to your liking?
If the answer to any of those questions is “yes”, then chances are good that it's something you'll want to consider restoring. But erring on the side of caution, if the dotfile already exists in your new environment, you'll want to move the existing file or directory out of the way before doing the restore. That way, if something unpredictable happens as a result of the restore, you can always put things back to the way they were beforehand and resolve any problems manually.
Backup
These backup steps will create a full, compressed archive of your entire home directory.
Creating an Archive
-
Reboot your system to ensure you have no network drives or your local Windows partition mounted.
-
Open a Terminal window.
- Ensure you are in your home directory:
cd $HOME
- Check the size of your home directory:
du -s .
The size reported will be in 1k blocks.
- Check the free space on
/tmp
, where the archive will be created:
df -k /tmp
The size in the “Available
” column is also in 1k blocks. You will need to have 2x the size of your home directory available.
- Create the archive:
tar cjvf /tmp/backup.tbz2 .
Note: make sure that you use a dot ('.') and not an asterisk ('*') or dot-star ('.*'), as the latter will not archive any of your dotfiles.
Copying the Archive to Your User Share on fsvs01 (H:\)
Once your archive is created, copy it to a location other than your Linux partition on your system. The safest location is your user share on fsvs01, as this will ensure that your data is available in the event of any hard drive problems.
-
Connect to the Windows share //fsvs01/users
; the following step assumes that you have mounted it to ~/fsvs01/users
.
- Copy the archive to your user share:
cp -i /tmp/backup.tbz2 ~/fsvs01/users/students/<class_year>/<username>/
where <class_year>
and <username>
are your graduating class year and Windows user name respectively.
Restore
Copying the Archive from Your User Share on fsvs01 (H:\)
Copy your backup archive to your system from the location you saved it to.
-
Connect to the Windows share //fsvs01/users
; the following step assumes that you have mounted it to ~/fsvs01/users
.
- Copy the archive to your home directory:
cp ~/fsvs01/users/students/<class_year>/<username>/backup.tbz2 ~
where <class_year>
and <username>
are your graduating class year and Windows user name respectively.
Unpack the Archive
Unpack your directory to a temporary location to avoid overwriting any existing files.
- Create a directory to uncompress your backup to:
mkdir ~/restore
- Change to this directory:
cd ~/restore
- Unpack the backup file:
tar xvfj ~/backup.tbz2
Restore Your Data
A full copy of your backed up home directory is now available under ~/restore
. Move the files you want to keep by using mv
:
mv matlab_data ~
You can optionally move all of your regular (i.e., non-dotfile) files and directories by running:
mv * ~
If you are restoring dotfiles, rename any existing files or directories of the same name first so that you have a way to recover from any potential problems. For example:
mv ~/.mozilla ~/.mozilla.orig