This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new Linux.com!

Linux.com

Feature: System Administration

Simple script restores your system settings after OS reinstall

By Jeremy LaCroix on June 20, 2007 (5:00:00 PM)

Share    Print    Comments   

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.

Results

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.

Jeremy LaCroix is an IT technician who writes in his free time.

Share    Print    Comments   

Comments

on Simple script restores your system settings after OS reinstall

Note: Comments are owned by the poster. We are not responsible for their content.

Use the dpkg magic

Posted by: spartas on June 21, 2007 04:21 AM

One of the nice things about debian-based distributions is apt. Another nice thing is dpkg.
Instead of choosing just a few packages and specifying them on the command line to <code>apt-get install</code>, with dpkg it is possible to restore all the packages that were previously on the system. To use this, before upgrading, save the list of currently installed packages to the home folder with the following command:


<code>
dpkg --get-selections > ~username/packages.list
</code>


After upgrading, to restore all of the installed packages, all that is necessary is the following commands:



<code>
apt-get update

sudo dpkg --set-selections < ~username/packages.list

sudo apt-get -u dselect-upgrade

</code>


Note, dselect is not spelled deselect (i.e. the first e is missing).



This second part can be automated (e.g. put into a script).
<!-- start: modules/comments/xartemplates/user-modifiedby.xd -->
[Modified by: spartas on June 21, 2007 04:22 AM]<!-- end: modules/comments/xartemplates/user-modifiedby.xd -->

#

Re: Use the dpkg magic

Posted by: Jeremy LaCroix on June 21, 2007 02:14 PM
Thanks, Spartas! I didn't know about that one.

That seems like a great idea for a restore script. :)

#

Re: Use the dpkg magic

Posted by: Daniel J. Givens on June 22, 2007 04:36 AM
I include dpkg --get-selections in my nightly backup scripts. The idea behind my backup scheme is that I only have to backup stuff that I've brought to the system outside of what is available in a package. I can do a standard install, suck in my package list, restore my /etc, /home, /opt, and select /var dirs and be back in action fairly quickly. All the while, I can put more system backups on a single volume and still be able to get right back to where I was just before disaster struck my system down.

#

scripts

Posted by: Anonymous [ip: 10.64.10.96] on December 17, 2007 06:34 PM
Linux Guru:

Is there a way I can capture all users in the /home directory to a file in Red hat linux?

thanks.
a

#

Updating dotfiles

Posted by: Noah Spurrier on June 21, 2007 07:12 PM
I have a Makefile that packages all of my beloved dotfiles into a tarball that I can copy to any new system.
I actually have a hook in my .bashrc that will automatically download and open the tarball once a week.
So whenever I login to a system I have not used in a while it gets updated with the latest dotfiles.
Of course, this can lead to surprises if you are not paying attention. Some people don't like things getting
updated behind their backs, but so far this has been working for me without a major meltdown.
<a href="http://www.noah.org/wiki/index.php/Dotfiles">http://www.noah.org/wiki/index.php/Dotfiles</a>



Yours,
Noah
[Modified by: Noah Spurrier on June 21, 2007 07:31 PM]

#

apt config file

Posted by: Anonymous [ip: 24.149.205.222] on July 21, 2007 04:01 AM
If you copy the apt config file from different versions of the os, then you will have to change the file (eg: replace feisty with gusty)

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya