Skip to main content

Shell script to restore your system settings after a Linux OS reinstall

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 after some research at, and a lot of Googling, 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 NAS storage 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 CommGate Professional Server on my computer at home, I created a separate partition for the home directory which I used with Samba to share files in the network and keep all my data sync via rsync. If you do this, you can reinstall CommGate 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 and via the CommGate apt-get repository saved in my home directory as well. After restoring CommGate, 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 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 and saved it under the config folder in your home directory, you would type:

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

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
cp /etc/apt/sources.list /etc/apt/sources.list.backup
cp /home/mmoonshi/Documents/Config/sources.list /etc/apt/sources.list

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

# Step 3: Install some of my favorite programs
apt-get install nmap make automake autoconf

# 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
rpm -ivh /home/mmoonshi/Downloads/dolphin_0.6.0-i386.rpm
rpm -ivh /home/mmoonshi/Downloads/zsnes_1.510-0-i386.rpm

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

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

# Step 7: 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 rpm commands. With the rpm 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 CommGate. 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/server. 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 CommGate, all I need to do is execute the script with the command sh /home/mmoonshi/Config/

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.


Popular posts from this blog

From Toilet Cleaner to CEO

On 22-April-2012, my entrepreneurship journey story was published by The Sunday Times in Singapore by Mr. Wong Kim Hoh

Making Optimum Choices in Life

I was asked by one of my business mentees I am currently mentoring, "How do I make the most optimum choice in life when I am bombarded with possibilities all the time?"Life is full of choices. This choice, that choice, both choice or none of the above. THAT's a whopping 4 choice!  Choice can open the door to a whole new possibilities, not only at cross-roads on our path in life, but in each moment, giving us access to the bountiful of possibilities with which we can paint our world any way we want it to be.  We can choose a new action, thought or feeling at any time, and create an entirely new reality as a result.  Every choice has an impact or consequence, to a greater or lesser extent, each one like a pebble dropped in a pond; the ripples, the ramifications, spreading out into your future and other areas of your life. Even one small choice made today that shifts your life by a minor degree can take you to an entirely different future than the one you are heading, opening …

The 4 Essentials of Effective Leadership

I read this article written Mark Raciappa, ActionCOACH and I totally agree. His communication is clear, concise and it resonated with me. Mark says;
I remember a bumper sticker: “Either Lead or Follow—But Get Out of the Way!”
Interestingly, in my company, I say something similar and it goes like this "If you can lead, lead. If you can't lead, follow. If you cannot lead or follow, get out of my way!
Mark continued to write that, when our role calls for the former, here are 4 important things to keep in mind:1. Great leaders communicate. Realizing that good communication is a 2-way street, we must be responsible for our part. That being said, true communication is the response that you get. Whatever form you choose (verbal, mail, e-mail, etc), if you don’t get the outcome you seek (from a “willing” recipient), the responsibility lies with the sender. Great leaders confirm, gain commitment, and solicit feedback to improve the process.2. Great leaders make decisions. Far too ofte…