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

In early April 2006, I sign-up for the Excellerated Business School for Entrepreneurs (BSE) after a preview session held in Singapore by Executive-Directions. The seminar was held from 15 April to 23 April 2006 in Subang Jaya Hotel in Kuala Lumpur, Malaysia. The BSE in KL was organized by GlobeSL Sdn Bhd. I left Singapore on 14 April via Transtar , the only 1st class coach service which has 16 seats to create a lot of room for your leg. They also provide hot meal during the journey with hot drinks on demand. The seats are equipped with a massage chair, a personal in-flight entertainment system on LCD screen, PC Games and the F&B Attendant will provide anything that you ask for. From newspaper, magazines, blanket, hot/cold drinks, a new set of earphones for the entertainment system... they have it all. They are most courteous and polite at all times. I truly enjoy the Transtar service. For SG$50 from Singapore to KL, it's worth it. I arrived in Subang Jaya Hotel at about 10p

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

Competition: How Eliminating This Word From Your Vocabulary Will Skyrocket Your Success

Competition: How Eliminating This Word From Your Vocabulary Will Skyrocket Your Success this essay is extracted from Rok SIVANTE's Facebook Notes . Click here for Rok SIVANTE Facebook Profile page. ---------------------------------------------------------------------------------------------------------------------------- Competition. How much of business & economic theory has been based around the concept? What’s happened to this economy of ours, based on the scarcity model of which competition is a core principle? (For anyone who hasn’t picked up a newspaper in the last year - it hasn’t exactly been doing as hot as most would like) What are the consequences of continuing to operate in the world from a belief system dictating there must be a winner and a loser in all outcomes? Are your results really dependent on how others around you conduct their lives? Your success in business has NOTHING to do with the “competition.” What any other company or person is doing has NOTHING to