Tuesday, September 18, 2012

Demonstrating Syncing two directories using rsync utility


Assumption:

 -    Directory “source” need to be sync with “destination” directory
-    For ease of demonstration, destination files I also on same box where source directory is .


=> Initially destination directory is empty. Source directory have 6 files

LocalSystem# ll source destination/
destination/:
total 0

source:
total 0
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file1
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file2
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file3
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file4
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file5
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file6


=> Here is how we can sync destination directory with source directory

LocalSystem# rsync -avz /tmp/source/      RemotSystem:/tmp/destination/

LocalSystem# ll source destination
destination:
total 0
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file1
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file2
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file3
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file4
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file5
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file6

source:
total 0
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file1
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file2
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file3
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file4
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file5
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file6


=> Some of the files have been deleted from source directory. rsync will delete them from destination on next sync.

LocalSystem#  rsync -avz --delete /tmp/source/      RemotSystem:/tmp/destination/
root@     RemotSystem's password:
sending incremental file list
./
deleting file6
deleting file4
deleting file2

sent 65 bytes  received 15 bytes  14.55 bytes/sec
total size is 0  speedup is 0.00
LocalSystem# ll source destination/
destination/:
total 0
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file1
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file3
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file5

source:
total 0
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file1
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file3
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file5


=> Now some new files are created and some more files deleted, rsync will copy new file to destination directory and delete already deleted file from source.

LocalSystem#  rsync -avz --delete /tmp/source/      RemotSystem:/tmp/destination/
root@     RemotSystem's password:
sending incremental file list
./
deleting file5
deleting file3
file7
file8

sent 145 bytes  received 53 bytes  44.00 bytes/sec
total size is 0  speedup is 0.00
LocalSystem# ll source destination/
destination/:
total 0
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file1
-rw-r--r--. 1 root root 0 2012-09-18 09:29 file7
-rw-r--r--. 1 root root 0 2012-09-18 09:29 file8

source:
total 0
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file1
-rw-r--r--. 1 root root 0 2012-09-18 09:29 file7
-rw-r--r--. 1 root root 0 2012-09-18 09:29 file8

Sunday, August 5, 2012

How to find which are the NICs have physical link UP ?

Scenario -1: Assume you have 4 network Card in you Linux box. 2 out of 4 network card are connected with switch but you do not know which of them ?

Scenario -2: Network team says that they have connected a cable in one of 4 NICs of Linux box. how to find in which NIC?

  • Bring up all interfaces
     for i in $(ifconfig -a|grep ^eth|cut -d " " -f1) ; do ifup $i ;done
     
  • Take note of MAC addresses of all NIC
    grep "" /sys/class/net/eth*/address 
     
  • Check Link status. Use any of below.(use any one of below)
    grep "" /sys/class/net/eth*/carrier 2>/dev/null  #Status 1 is means linked
    grep "" /sys/class/net/eth*/operstate 2>/dev/null
    for i in $(ifconfig -a|grep ^eth|cut -d " " -f1) ; do 
        echo $i;ethtool $i| grep Link;done| paste - -
    mii-tool 2>/dev/null
    ip link show |grep LOWER_UP
    


Friday, May 11, 2012

"screen" : Unix utility to manage many ssh session

Those who most of time work on Unix like system or GNU/Linux and do not use screen, they are really not leveraging simple but powerful facility provided by  screen utility.

Suppose you are one of many Techies who spend most of time works on command line in a ssh session and start many many program on same system or need login on many many system (40) at same time screen is for you.

There is high chances that you login on some of system almost daily and need 10 or 15 or 30 or 40 server login session at same time - all active, whole month !!  screen is your pet.

QuickStart on a Linux system

=> login toleast critical system

=> Check screen package is installed
     $ rpm -q screen
     screen-4.0.3-1.el5_4.1

=> Create a .screenrc file in your home directory similar to this.  Another example is here
$ cat /home/nasimuddin.ansari/.screenrc
hardstatus on
hardstatus alwayslastline
hardstatus string "%{.bW}%-w%{.rW}%n %t%{-}%+w %=%{..G} %H %{..Y} %m/%d %C%a "
###    Keybindings    ##
#
## bind <F7> to detach screen from this terminal
## bind <F8> to kill current session
## bind <F10> to create a new screen
## bind <F9> to rename an existing window
## bind <F11> to move to previous window
## bind <F12> to move to next window
bindkey -k k7 detach
bindkey -k k8 kill
# space in keyboard
bindkey -k k; screen
bindkey -k k9 title
bindkey -k F1 prev
bindkey -k F2 next
displays
multiuser off
hardstatus string "%{.kG}%-Lw%{.KW}%n%f %t%{-}%+Lw %=%{..G} %H %{..W} %d/%m %C%a "
#Start screen windows with blank title
shelltitle ""
# name tiltle window on F1
bindkey -k k1 title
bindkey -k k2 windowlist
## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-END =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= #

=> AND your are ready to use ! Just type screen on $ prompt !
=> My config file has following key mapping:
F1   -  Set a particulate window title e.g. server101
F2   - Show list of all 40 windows I have open
F8  - Kill a window  (Message : window number 34 killed)
F10 - Create new window to run something on this system or open a ssh session to another system
F11 - Go to just previous windows
F12 - Go to Just next window
Cntr-a Ctrl-a - Toggle between 2 windows
Cntrl-d : Detach screen session. It will keep all your session live. Leave office and go home !

=> I know, on reaching home, you are going to work again and need to start where you left. See all your screen session :



$ screen -list
There are screens on:
        5842.pts-0.MyDevSystem01    (Detached)
        10634.pts-22.MyDevSystem01   (Dead ???)


=> Bring your session to life - re-attach it

    $ screen -r 5842.pts-0.MyDevSystem01

=> Remove a dead session

    $rm -rf /tmp/10634.pts-22.MyDevSystem01


=> If I am not working in a session, I will run "vmstat 500" and leave it breathing until I need it use it  again.

Sharing screen session with another user

=> Start a screen session using


# screen -d -R user1_session

(Replace user1_session with any other helpful name for the session you’re about to create.)

=> Pick up phone and call other user to connect to your screen session. 

# screen -x user1_session

(Assuming another user is logged in using the same user account)

Multiple persons can type and work on the same terminal . Dimensions of terminal output will be the same for every user. A user can change his screen session dimensions - press Ctrl-A and then Shift+F. It will make the screen output fit your current terminal size, and change it to that size for every connected user.

Reference for hungry people:

man screen
screen -h
http://www.ibm.com/developerworks/aix/library/au-gnu_screen/
http://www.rackaid.com/resources/linux-screen-tutorial-and-how-to/
http://magazine.redhat.com/2007/09/27/a-guide-to-gnu-screen/
http://technonstop.com/screen-commands-for-terminal-sharing (share session)







Saturday, March 24, 2012

Richard Stallman - Prophet of Free Software


Recently I have chance to attend Richard Stallman talk on Free software at NUS Singapore. RSM is modern age Prophet of Free Software. He convinced University of Berkeley to release souce of BSD Operating system for world.

Though his views are widely available on internet in his numerous articles - and talk was repeatability of same words, I was more interested to see him in person.

He is St iGNUcious ( stallman.org/saint.html ) of GNU and advocate :

  • Free software for social benefit.
  • Free Software and GNU as preferred terms over Open Source software.
  • Use term GNU/Linux for Linux distributions because it has 28% GNU software.
  • 'Blob free' Free software even if it does not provide some essential feature e.g. remove Linux blobs code from kernel that will stop support of some devices (i.e. NVIDIA Video card, some NICs, Storage controller etc). One such distribution made available by GNU is Linux-libre.

What is RSM software definition ?

Freedom-0: Freedom to run program for any purpose
Freedom-1: Freedom to study code and change it as per your wish
Freedom-2: Freedom to redistribute other software to anyone
Freedom-3: Freedom to distribute your modified software to anyone

In 1983 he announced GNU project and in 1985 Free Software foundation. Linux kernel was announced many years later in 1991 under GNU License.

World is indebted for his contribution.