Article | Automate Backup for IE Favorites

Before diving deep into the world of shell scripting, I had ventured with batch scripting on Windows XP machine. Well, recently I had to format Windows 7 running on Dell Studio laptop.

In doing so, I forgot to keep backup of the browser bookmarks/favorites, even though Chrome/Firefox provide options to export them. However, I had been using IE lately and kept rather few, yet important links bookmarked.So, I rarely cared about backing them up until recently.

Why not Google?

Well, I did few days back and found the following link useful;

http://support.microsoft.com/kb/211089

As most of the Windows users would be knowing the OS keeps your favorites/bookmarks under following directory structure;

Windows XP – C:\Documents and Settings\<username>\Favorites

Windows 7 – C:\Users\<username>\Favorites

Generally, Windows OS, be it XP or 7, would be installed in C drive, part of partitioned hard disk storage. So, I was wondering why not make use of the other partitions which are least impacted by crashing/formatting of the Operating System.

Also, keeping backup of the folder manually regularly would not be that easy, even though it is.

Why not automate the backup process?

This is what I think should be done (technically), by creating the following batch script;

:: Assuming the system has another partition as E drive

cd /d "E:"
rmdir /s /q Favorites
cd /d "C:\<YOUR-DIRECTORY-STRUCTURE>"
xcopy /s /f Favorites "E:\Favorites"

Now, I just have to save it as batch program, keep it on any drive other than the OS drive.

I tested it on Windows 7 and worked well.

NBM | Snap Active Window

Few years back, I had created a NetBeans Module (community-based plugin) along with Jay Mahadeokar. However, we couldn’t give time to it and somehow lost the source code as well, after successfully publishing it into the NetBeans 6.0 Update Center.

Brief History

Snap It is a plugin that enables you to take screenshots from within the IDE. Basically, it tries to emulate the “Take Screenshot” Utility, bundled with almost every Ubuntu Distro. This module enables you to take a screenshot of what’s visible in your system’s resolution. The snapshots taken are not depended on the resolution of NetBeans IDE.

Whats New

Recently, I have started devoting time back on learning Java, NetBeans Platform and thus, I thought of making the plugin from scratch and add features, which were earlier thought of.

Have a look at the following snapshots (covering different Modes). These were taken after installing the module in NetBeans 6.8 IDE, created using the same release (based on NetBeans Platform 6.5).

Editor Mode

Explorer Mode

These are very few examples, one can take snapshots of every possible window that opens within the NetBeans IDE, whether dockable or not. There are total 5 modes I am currently aware of;

  1. Explorer
  2. Editor
  3. Properties
  4. Output
  5. Anonymous

One needs to explore more to know more about NetBeans Platform, as this is just the beginning!

taT4Nix | Using visudo To Run Commands With Root Privileges

Recently, I wrote a script to automate the task of mounting the shared folders in Guest OS (Ubuntu 9.10) from Host OS (Windows 7), courtesy of VirtualBox. Now, I couldn’t run that script because I needed root privileges 😐 Although, I have installed the Guest OS, so I know the root password.

However, when VM boots, I am logging in with user account created while installing Guest OS. So, to run certain commands that need root privileges, I have to explicitly use sudo and then enter password, when prompted.

Using visudo

Now, surfing the web led me to a solution, i.e. edit /etc/sudoers file that contains list of privileges given to root and other users. So, to edit this sudoers list, one must run the following;

sudo visudo

You’re prompted for password, enter it and then, you would see something like this;

# /etc/sudoers
## This file MUST be edited with the 'visudo' command as root.
## See the man page for details on how to write a sudoers file.#

Defaults    env_reset

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL) ALL

# Uncomment to allow members of group sudo to not need a password
# (Note that later entries override this, so you might need to move
# it further down)
# %sudo ALL=NOPASSWD: ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

As you can see, the last statement implies that if any user account belongs to admin group, then that user account will have privileges of a super user. So, if I run the following command again, for instance;

sudo visudo

I won’t get any prompt for password and /etc/sudoers without delay.

Trick 2 Gain Root Privileges..

So, go to “System > Administration > Users and Groups” and click on the account properties you want to assign root privileges. Then, switch to “Advanced Tab“, change the “Main Group” to admin. Now, if you have followed till here, it’s time to make following changes to /etc/sudoers file;

# Members of the admin group may gain root privileges
%admin ALL=(ALL)NOPASSWD: ALL   

Now, you have root privileges, you may now run the commands that need root permissions without getting prompt for password. Try doing the same for the script I mentioned in the beginning.

taT4Nix | Mount Shared Folders from Host OS in Guest OS

Brief Overview

I have Ubuntu 9.10 as 64-bit Guest OS running on top of Windows 7 as 64-bit Host OS using VirtualBox 3.1.2. Now, I have also added 3 folders from Windows Filesystem for sharing with Guest OS. As you know, I have to mount the folders by running commands in Terminal for accessing data in shared folders. So, I have to do it manually every time I start the VM.

Latest Updates

Chances are you might not be able to run the following scripts without root privileges, as commands like mount need root privileges. So, refer this blog entry for more details to solve the problem.

Problem Description

I was looking forward to automate this and I came across “System > Preferences > Startup Applications” in my Guest OS. However, I need to run the mount command 3 times for 3 shared folders I have. So, I wrote the following scripts to automate the task and assigning just one script in “Startup Applications”.

Also, I need this because I have created playlists for videos (Movie Player/VLC) and music (Audacious 2), also set desktop background of my choice. These files are only available in shared folders. Now, using single startup script, I would be able to make a copy of music, pictures, videos, etc. every time I login with specific account after starting VM.

Getting Started

To do the needful, I came up with the following script, which would run two more scripts in background, when I login with specific user account. Let’s get started;

share_hostos_data.sh

#!/bin/sh

nohup $HOME/mnt_cp_hostos_data.sh A B C > /dev/null
nohup $HOME/unmnt_hostos_data.sh A B C > /dev/null

Now, that we have startup script ready that will first mount shared folders and then, unmount them. This seems to be stupid if you don’t go below for more details about those two scripts.

Mount Shared Folders and Copy Them Recursively

Now, the following script will accept N arguments where each argument will be the name assigned to shared folder path in Host OS. So, the script will not run with ZERO arguments and it will make the directory under /media/win7-share in Guest OS with the argument name and then, mount the folder before copying the data in the Public directory of specific account’s $HOME.

mnt_cp_hostos_data.sh

#!/bin/sh

if [ $# -eq 0 ]; then
 echo "Exiting.." > /dev/null
 exit 0
fi

for dname in $@
do

 SHARE_DIR=/media/win7-share/$dname

 if [ ! -d ${SHARE_DIR} ]; then
 mkdir ${SHARE_DIR}

 if [ $? -eq 0 ]; then
 echo "${SHARE_DIR} created successfully.."
 else
 exit 1
 fi
 fi

 mount -t vboxsf $dname ${SHARE_DIR}

 if [ $? -eq 0 ]; then
 echo "Mounted $dname successfully.."
 else
 exit 1
 fi

 cp -nr --target-directory=$HOME/Public/windows7/ ${SHARE_DIR}

 if [ $? -eq 0 ]; then
 echo "Copied data into $HOME/Public/windows7/$dname successfully.."
 else
 exit 1
 fi

done

Unmount Shared Folders..

The following script also accepts N arguments where each argument will be the name assigned to shared folder path in Host OS. So, the script will not run with ZERO arguments and it is just going to unmount the directory made the directory under /media/win7-share in Guest OS with the argument name.

unmnt_hostos_data.sh

#!/bin/sh

if [ $# -eq 0 ]; then
 echo "Exiting.." > /dev/null
 exit 0
fi

for dname in $@
do

 SHARE_DIR=/media/win7-share/$dname

 if [ -d ${SHARE_DIR} ]; then
 umount -f ${SHARE_DIR}

 if [ $? -eq 0 ]; then
 echo "Unmounted ${SHARE_DIR} successfully.."
 else
 exit 1
 fi
 fi

done

Work Ahead

As you might have understood by know the shared folders names are A, B, C (see share_hostos_data.sh). That’s something I had to hardcode inside the script for passing them as arguments to mnt_cp* and unmnt* scripts. I ran this command VBoxMange showvminfo “<VM-Name>” in my Host OS command prompt, to find out the names of shared folders.

If I could access the names somehow, then I can pick up these names and supply them as an argument to the necessary scripts created above. That’s all for today, have a great time..

SVN, Batch Scripts and NetBeans Platform

Wrote a single-line batch program to add the sources of NetBeans Platform to the local working copy at my system. Recently, I had downloaded the sources for NetBeans Platform 6.0, 6.1, 6.5 and 6.7.  Now, I want to commit and tag these sources at PrayogShala. So, I extracted the zip files and checkout the project’s root (let it be denoted by SVNROOT_DIR) on my system.

As I have SVN repository, so I can add any file/folder locally using svn add to become part of the working copy (SVNROOT_DIR). This way, when I will commit using svn commit, I will be able to commit all the locally added files.

Batch Program

This is easily done using IDE’s like NetBeans IDE. However, I was in no mood to run the IDE and open 50+ projects. Then, individually commit them. So, I thought of creating a batch program for this purpose. The batch program should be run from within the working copy, as the svn add command will work only under working copy.

for /d %%X in (%1\*) do svn add %%X

Look simple, isn’t it? Well, it isn’t for a newbie like me. I had to refer internet to learn some batch programming for the purpose. Hence, I was able to code the above. You can refer to one more batch program, I wrote last month for different purpose. Although, its still related to NetBeans IDE.

Explanation

This program loops (for … in … do) through the immediate sub-directories (%%X) of the root passed as runtime argument (%1). Then, each sub-directory is added (svn add) to the working copy.

For instance, I extracted the zip file for NetBeans Platform 6.5 under C:\nb65 directory. Then, this directory will be passed as runtime argument to the batch program. As, this directory would have sources for all the netbeans module projects. Now, svn add command will automatically scan and add any files/folders found under each module project’s directory.

Further Usage

Well, one can tweak the above batch program to create a text file, that will list full-path-name of all the files, under those directories.

for /d %%X in (%1\*) do echo %%X >> list_of_files.txt

Update (July 18th)

I have created another batch file, named it nbsvn.bat

svn add %1
svn commit -m "Tagging %1 of NB 6.5" %1

When I tried to commit all the added folders at one go, I had problems. So, I changed the method, I have created the SVNROOT_DIR and added all the modules once again. Now, I will run the above script for every module found inside the SVNROOT_DIR.

for /d %%X in (%1\*) do call nbsvn %%X

Hope this helps..