Tuesday, December 28, 2010

Executing cronjobs on specific days in a month

You can use this post as a reference to schedule the Cronjobs in more Granular fashion.

Situation:
We had a requirement to execute couple of scripts on specific Saturdays of a month throughout the year  i.e., First script (say /opt/firstscript.sh) should be executed on all Saturdays EXCEPT first Saturday of the month, and the Second script (say /opt/secondscript.sh) should be executed ONLY on first Saturday of the month. This we required to do for scheduling Weekly Tape backups.

Executing a script on all Saturdays is fairly simple, we need to just specify number 6 on 5th column on crontab entry, but here the scenario is different.
The solution for this is shown below. This uses ‘date’ command to check if the current date is a first Saturday of a month.

0  2  *  *  6     [ $(date +%d) -gt 7 ]  && /opt/firstscript.sh               
# Executes at 2am on all Saturdays Except the first Saturday of the month (Saturdays other than 1st  can fall only after 7th ).

0  22  *  *  6   [ $(date +%d) -le 7 ]  && /opt/secondscript.sh        
# Executed at 10 PM ONLY on first Saturday of the month (A first Saturday can either be 7th or less than that ).

Thursday, December 23, 2010

Checking NFS and SAMBA shares

NFS

How to check the NFS shares available from a NFS Client ?
#  showmount -e <NFS Server IP>

Example
[root@hostxyz /]# showmount -e nfsserver.domain.com
Export list for nfsserver.domain.com:
/VOL0           stoash01-d3,staat01-d3,rdbash01,10.20.23.16,10.20.64.31,10.100.14.96 ,10.100.16.12 
/VOL1           (everyone)
/ORAAPPS        (everyone)
/ret_vol3       (everyone)
/nas_server_bkp (everyone)
/testnfs        (everyone)
[root@hostxyz /]#


From NFS server, how to find the list of NFS clients accessing the NFS share currently ?

Execute the following script:
for i in `cat /var/lib/nfs/rmtab | awk -F":" '{print $1}' | sort -u`; do nslookup $i | tail -2 | awk '{print $4}'; done

---------------------------------------------------------------------------------------------------------------------------------------------------------
SAMBA

How to find the list of Samba shares available from a Windows Server?

# smbclient -L Samba server IP –Uusername
Password:


[root@hostxyz /]# smbclient -L windows.tcprod.com -Uadevaraju
Password:
Domain=[TCPROD] OS=[Windows Server 2003 R2 3790 Service Pack 2] Server=[Windows Server 2003 R2 5.2]

        Sharename       Type      Comment
        ---------       ----      -------
        IPC$            IPC       Remote IPC
        sqldb_htk       Disk
        webash01-dev    Disk
        C$              Disk      Default share
        VOL3            Disk
        testsmb         Disk
        SMBSYSENG       Disk
        HPUniver.3      Printer   HP Universal Printing PS
        HPUniver.2      Printer   HP Universal Printing PCL 5
        ADMIN$          Disk      Remote Admin
        HPUniver        Printer   HP Universal Printing PCL 6
        print$          Disk      Printer Drivers
        F$              Disk      Default share
        nas_server_bkp  Disk
        SMBVOL1         Disk
        windowsprodbackup Disk
        SMBVOL0         Disk
        Report_server_bkp Disk
        E$              Disk      Default share
        exchange_db     Disk
        sysengdata      Disk


How to mount a Windows Share on to a Linux mount point:


Syntax: 
Put this entry in /etc/fstab and do 'mount -a'
//Windows server IP/Sambashare  /Linux_mount cifs username=XXX,password=XXXX 0 0


Example:
//Windows_server/sambashare      /linux_mnt cifs username=ashok,password=XXXX  0 0

Sudo access to a specific command set

Lets say we have a requirement to give sudo access only to a particular command set.  Let’s take couple of scenarios like this:

1.   We want to give privilege to DBA team to mount/umount ONLY a particular filesystem ( /oracle_data) but  we don’t want them to mount/umount other filesystem.
2.   We want to give privilege to NOC team to start/stop ONLY the httpd service but we don’t want them to start/stop other services.

The syntax in /etc/sudoers file should be as follows:

%dbateam          ALL=(ALL) NOPASSWD:  /bin/mount /oracle_data, /bin/umount /oracle_data

%nocteam          ALL=(ALL) NOPASSWD:  /sbin/service httpd start, /sbin/service httpd stop, /sbin/service httpd status


Having set like this, the respective team members can execute the commands as follows:

# sudo /bin/mount /oracle_data      # Works
# sudo /bin/umount /oracle_data    # Works

# sudo /bin/mount /other_filesystem     # This will fail


# sudo /sbin/service httpd start       #  Works
# sudo /sbin/service httpd stop       #  Works
# sudo /sbin/service httpd restart   #   Fails. Since restart is not specified

# sudo /sbin/service nfs start           #  This will fail

Wednesday, December 22, 2010

Finding total size of files owned by a particular user

Syntax:

# find <pathname> -user <username> -ls | awk '{sum += $7} END {printf "Total size: %8.4f MB\n", sum/1024/1024}'

Example:
Foldername to search: /nasllm-ih
Username:  rram

[root@hostxyz ~]# find /nasllm-ih -user rram -ls | awk '{sum += $7} END {printf "Total size: %8.4f MB\n", sum/1024/1024}'                   
Total size: 958.7282 MB
[root@hostxyz ~]#


Validation:

Here I have created a folder /root/ashok consists files owned by user “adevaraju” of total size: 160 MB under its various sub-directories.

[root@hostxyz ~]# ls -lRh /root/ashok
/root/ashok:
total 21M
drwxr-xr-x 3 adevaraju root 4.0K Dec 14 06:30 d1
drwxr-xr-x 2 adevaraju root 4.0K Dec 14 06:35 d2
-rw-r--r-- 1 adevaraju root  10M Dec 14 06:21 file1
-rw-r--r-- 1 adevaraju root  10M Dec 14 06:22 file4

/root/ashok/d1:
total 21M
drwxr-xr-x 2 adevaraju root 4.0K Dec 14 06:35 dx
-rw-r--r-- 1 adevaraju root  10M Dec 14 06:22 file2
-rw-r--r-- 1 adevaraju root  10M Dec 14 06:22 file3

/root/ashok/d1/dx:
total 21M
-rw-r--r-- 1 adevaraju root 10M Dec 14 06:30 file5
-rw-r--r-- 1 adevaraju root 10M Dec 14 06:30 file6

/root/ashok/d2:
total 101M
-rw-r--r-- 1 adevaraju root 100M Dec 14 06:30 file7
[root@oralsb11-new ~]#
 [root@oralsb11-new ~]# du -sh ashok/
161M    ashok/

[root@hostxyz ~]# find /root/ashok -user adevaraju -ls | awk '{sum += $7} END {printf "SUM: %8.4f MB\n", sum/1024/1024}'
SUM: 160.0156 MB
[root@hostxyz ~]#


PS: By replacing the search folder to '/', we can find the total size of files owned by a specified user in the entire system.