June 20, 2007

Simple script restores your system settings after OS reinstall

Author: Jeremy LaCroix

Reinstalling your operating system is never a fun or welcome task, but sometimes it's unavoidable. Restoring settings and downloaded applications after installing an operating system can take quite a bit of time, so I've come up with a shell script to make things a bit easier.

This technique does not take the place of a good external backup. I recommend backing up your important files to a place other than your computer, such as a CD or DVD disc, because with a powerful enough shell script, you could accidentally wipe out hours of work in just a few seconds.

When I installed Ubuntu on my computer, I created a separate partition for the home directory, which is where personal files are typically stored. If you do this, you can reinstall Ubuntu without losing your files, as long as you don't overwrite the home partition. Since I have the configuration files that reside on my root partition backed up inside my home directory, the files should still be there after I reinstall, so all I have to do is copy them to the correct place.

In addition, I have several programs downloaded from the Internet saved in my home directory as well. After restoring Ubuntu, all I have to do is execute a command to install the programs again. My script can take care of that for me as well.

Creating a shell script

A shell script is a text file that contains Linux commands. If you're familiar with MS-DOS batch files, the idea is virtually the same.

To create a shell script, open a text editor, enter some commands, and save the file with whatever name you wish, although make sure the filename ends with the extension ".sh" instead of ".txt."

To make the script executable, open a terminal window (available in Ubuntu's Applications menu under Accessories) and execute the chmod +x command with the file path. To run the script, execute the sh command including the file path. For example, if you named the file restore.sh and saved it under the config folder in your home directory, you would type:

chmod +x /home/yourusername/config/restore.sh
sh /home/yourusername/config/restore.sh

Here is an example of such a script. I shortened it a bit from the one I use for the sake of simplicity. Under the script listing I'll describe each section.

# System Restore Script
# Purpose: To restore my most used applications and settings easily

# Step 1: Restore my sources.list file
sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup
sudo cp /home/jeremy/Documents/Config/sources.list /etc/apt/sources.list

# Step 2: Make sure I have all the security and software updates
sudo apt-get update
sudo apt-get dist-upgrade

# Step 3: Install some of my favorite programs
sudo apt-get install frozen-bubble neverball chromium supertux kdf xmms xmms-skins totem-xine nvidia-glx xine-ui

# Step 4: Install programs that I have downloaded on my local hard disk
# Note: If I move these packages to a different location, I'll need to update this script
sudo dpkg -i /home/jeremy/Documents/Downloads/dolphin_0.6.0-0ubuntu1_i386.deb
sudo dpkg -i /home/jeremy/Documents/Downloads/zsnes_1.510-0ubuntu1_i386.deb

# Step 5: Restore screen resolution settings for my monitor
# Note: Do not do this unless restoring to a single pc
sudo cp /etc/X11/xorg.conf /etc/X11/xorg.conf.backup
sudo cp /home/jeremy/Documents/Config/xorg.conf /etc/X11/xorg.conf

# Step 6: Install the Adobe Flash plugin for Mozilla Firefox
# Note: This works with 32-bit Ubuntu only
sudo apt-get install flashplugin-nonfree

# Step 7: Reboot
sudo reboot

Understanding the script

Each section of the script begins with at least one comment -- indicated by a line that begins with a pound symbol (#) -- so that if you look at the script later, you'll remember what each section does.

In Step 1 I use the cp command to copy a file from one location to another without erasing the original file. The first cp command copies the sources.list file, which contains a list of software repositories I'm subscribed to, to a backup version, so if I make a mistake I can reclaim the original. I chose to name the backup copy sources.list.backup, but you can name it whatever you wish. The second cp command copies a locally stored sources.list file to its native location to be used by Ubuntu.

The purpose of the prefix command sudo is to run the associated command as the root user. Most of the commands in this script require this; when you execute the script, you will need to enter the root password for the first instance of sudo.

In Step 2, the command apt-get update synchronizes my list of packages with that of the online repositories, and the command apt-get dist-upgrade downloads any and all packages that are newer than the ones I currently have installed. This command will also install security updates, if any have been released.

In Step 3 I download and install applications I use that aren't installed by default using the subscriptions I have stored in my sources.list file. You can list as many packages as you'd like with one instance of this command. The trick here is to add an application to this script every time you install a new one.

I have certain other software packages downloaded to my hard disk. Storing packages locally rather than installing them from a repository may be useful if you compile your own or you prefer a certain version over another. Section 4 will install them for me using dpkg commands. With the dpkg command, the name of the application to install is combined with its path on the hard disk for the sake of accuracy.

My monitor configuration isn't automatically recognized by Ubuntu. I like to have a resolution of 1280x1024 and a refresh rate of 85Hz. All of the settings required for my monitor are in my xorg.conf file, so by restoring that file in Step 5, my monitor is set up the way I like it after the PC is rebooted. I also need to do this because this script installs the nvidia-glx package, which is necessary to enable hardware acceleration for my video card.

Restoring your xorg.conf file this way is a good idea only if you use this script on a single PC. No two computers are compatible with the same xorg file unless both have the exact same hardware, so if you're writing a script to set up multiple PCs, be sure to skip this section, or you may not have a working graphical user interface when you reboot.

We're almost home. In Step 6, I install the Adobe Flash plugin for Mozilla Firefox, which lets me view videos on YouTube. Finally, I reboot. After performing this much maintenance on your PC at once, it's a good idea to reboot your machine.


Since I have my home directory as a separate partition, and I have my restore script stored there, after installing Ubuntu, all I need to do is execute the script with the command sh /home/jeremy/Config/restore.sh. When it finishes, my computer automatically reboots, and it's set up just the way I like it. The resolution of my monitor is configured, my favorite programs are installed, my installed applications are the newest available, and I'm able to view Web sites that utilize Adobe's Flash plugin. This technique saves me what would have otherwise been a few hours of work.

Got a clever script that you'd like to share with our readers? Tell us about it!

Jeremy LaCroix is an IT technician and technologist.


  • System Administration
Click Here!