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 linux.com, howtoforge.com 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 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
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
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.

Results

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/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.

Comments

Popular posts from this blog

Strategic Plan vs. Operational Plan

Strategic Plan Vs. Operational Plan: Do You Know the  5 Main Differences ? What's the difference between a Strategic Plan vs. an Operational Plan? Both are plans but are they the same? If not, what’s the difference? Do you need both? Why do we need both?  After mentoring Start-Ups from pre-revenue to achieving at least S$1M in revenues since 2012 and supporting growing Small-Medium Enterprises within the S$4M to S$10M range revenues, whose leadership teams are stuck in their day to day operational matters and fire-fighting, I hear these questions frequently.  My response: A  strategic plan  outlines your Vision, Mission, Core Values to build the Culture with high-level goals for the next 3 to 5 years. It also takes into account how you’ll measure those goals, and the major projects you’ll take on to meet them. An  operational plan  (also known as a  work plan ) is an outline of what each of your business d...
Why Data Backups Are So Important Our computers, smartphones, personal digital assistants, MP3 players, operating systems, and software are tools that we use to create and manipulate the content that is the most important aspect of computing - our data. Without your personalized data, the computing experience will be mundane and very generic. We need to be able to create in order to really feel good about what we’re doing. Many times people don’t realize how important their data is until it’s too late. I have see customers whom says, why do I need to spend $3,000 on a tape backup system just to archive, copy another instance of my data and then keep it? I’ve seen way too many people treat their hard drive’s like an all-you-can-eat buffet, and they’ll just pile anything and everything that they can find into an endless collection of files and folders that will be very difficult to make sense of in the future. The whole point of technology should be to simplify our lives instead of comp...

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