Friday, May 14, 2010

How do I use cpio command under Unix?

Q. Can you explain how do I use cpio under Linux / UNIX?

A. GNU cpio is a tool for creating and extracting archives, or copying files from one place to another. It handles a number of cpio formats as well as reading and writing tar files. cpio command works just like tar, only better.

As I said earlier, cpio works like tar but it can read input from the "find" command. This is nifty feature. For example you can find out all *.c files and backup with cpio command.

# find / -name "*.c" | cpio -o --format=tar > c-file.backup.tar
# find / -iname "*.pl" | cpio -o -H tar > perl-files.tar


You can also specify file name using -F option:

# find / -iname "*.pl" | cpio -o -H tar -F perl-files.tar

Where,

-o: Create archive
-F: Archive filename to use instead of standard input or output. To use a tape drive on another machine as the archive.
-H format: Specify file format to use.
-i: Restore archive

You can extract archive with the following command:

# cpio -i -F perl-files.tar

You can list file inside archive i.e. list contents of the cpio file with following command:

# cpio -it -F perl-files.tar


You can write archive of /home to tape (drive /dev/nst0), type the following command:

# find /home | cpio -o -H tar -F /dev/nst0

Restore backup using following command:

# cpio -i -F /dev/nst0Backup /home dir,

to remote system tape drive:
# find /home | cpio -o -H tar -F user@backup.nixcraft.in:/dev/nst0 --rsh-command=/usr/bin/ssh

HP-UX UNIX: Start / Stop and Configure Cron Services

Q. I need to run a backup and other stuff everyday. How do I check and start cron service under HP-UX UNIX operating system? How do I write cron jobs?

A. Cron service is required to run jobs and tasks such as backup.

You must login as the root to run following commands. Each user that is using the cron service must have a cron configuration file in the /var/spool/cron/crontab directory. Also users are permitted if their name appeared in /var/adm/cron/cron.allow file

Task: Find out if cron is running under HP-UX

Type the following command at a shell prompt

# ps -ef | grep cron

Open /etc/rc.config.d/cron file

vi /etc/rc.config.d/cron

Set control variable to 1 to enable cron : CRON=1
Set control variable to 0 to disable : CRON=0

Close and save the file. To start or stop cron you can type the following command:

# /sbin/init.d/cron start <-- start cron
# /sbin/init.d/cron stop <-- stop cron

Task: HP-UX Start cron service

If cron is not running, simply type:

# cron

Task: Edit / create cron jobs

Type the following command to submit a cron job:

# crontab -e

List your cron file:

# crontab -l

Backup all your cron jobs:

# crontab -l > ~/backup.cron.jobs

Remove ALL cron job:

# crontab -r

Task: Crontab file format

Cron file format is as follows:

MIN HOUR DATE MONTH DAY /PATH/TO/COMMAND
0-59 0-23 1-31 1-12 0-6 /root/scripts/backup2tape.sh
Easy to remember crontab file format:

* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)To run /root/script/backup at 23:00, every day, enter:

# crontab -e

Append following

0 23 * * * /root/script/backupRun foo job very weekday (MON-Fri) at 6am, enter:

0 6 * * 1-5 /root/script/backup

HP-UX UNIX Configure an NTP Server

Q. How do I configure an NTP (network time protocol) Server under HP-UX UNIX server as my applications rely on the system clock accuracy to get current time / run reports?

A. NTP is used for time synchronization. Many applications such as backup shell/perl scripts, make utility and others depends upon accurate system time.

HP-UX xntpd server configuration

xntpd server (daemon) is used by HP-UX. Open file /etc/rc.config.d/netdaemons:

# vi /etc/rc.config.d/netdaemons
Make sure following variables are defined and XNTPD is set to 1:

export NTPDATE_SERVER='ntp.mycorp.com pool.ntp.org in.pool.ntp.org'
export XNTPD=1
export XNTPD_ARGS=
Save and close the file. Where,

■NTPDATE_SERVER : space-separated list of timeserver / IPs - If server uses the radio clock / system time, leave the NTPDATE_SERVER blank.
■XNTPD : set it to 1 to enable NTP
■XNTPD_ARGS : set options, see man page
Make sure correct timezone is setup in /etc/TIMEZONE file (look for TZ variable):

# vi /etc/TIMEZONE
Save and close the file. Open /etc/ntp.conf file:

vi /etc/ntp.conf
Make sure server are defined:

server unix-box-ntp
server delhi-ntp
peer delhi-noc-ntp
driftfile /etc/ntp.drift
Close and save the file. Where,

■server : ntp server used (poll) to obtain time
■peer : a peer relationship with another ntp server
■driftfile : track local clock time (drift of the local clock)
Start HP-UX ntp service:

# /sbin/init.d/xntpd start
# /sbin/init.d/xntpd stop <-- stop ntp service
Verify that everything is working fine:

# ntpq -p
Watch out log file /var/adm/syslog/syslog.log for errors:

# tail -f /var/adm/syslog/syslog.log
Make sure xntpd is running:

# ps -ef | grep xntpd
SAM - GUI Admin Tool
=> SAM can also be used to configure NTP service under HP-UX, just start SAM and open time window.

HP-UX: How Do I configure routing or add route?

You can use route command to configure routing. Syntax is as follows:
route add net {network-address} netmask {subnet} {router-address}

Let us assume your router address is 192.168.1.254 and network ID is 192.168.1.0/24, then you can type route command as follows:

# route add net 192.168.1.0 netmask 255.255.255.0 192.168.1.254

OR

To add a default route:

# route add default 192.168.1.254

Verify that (display) routing table is updated (display routing table):

# netstat -nr

Test it i.e. try to ping or send nslookup request:

# ping mycorp.com

To flush all routing entries use command :

# route -f

However if I reboot HPUX box then above routing entries gets removed. To pick up your setting upon each reboot your need to configure Routes in HPUX networking configuration file - /etc/rc.config.d/netconf. To add default router/gateway 192.168.1.254:

# vi /etc/rc.config.d/netconf


Add or modify following entries

ROUTE_DESTINATION[0]="default"
ROUTE_MASK[0]=""
ROUTE_GATEWAY[0]="192.168.1.254"
ROUTE_COUNT[0]="1"
ROUTE_ARGS[0]=""

Reboot HP-UX system/server to take effect

# shutdown -ry 0

How to mount CDROM in hpux

HP-UX UNIX Mount CDROM ( CDFS ) Filesystem Command

Q. How do I mount CDROM media under HP-UX UNIX System from a shell prompt? How do I automount CDFS file system using /etc/fstab file?

A. You need to use standard mount command to mount the CD (data CD or ISO9600 format cd). You must login as the root user to perform following operations.

Step # 1: Find CDROM device name

Ypu need to use ioscan command to find out the block device file name located at /dev/dsk directory. Without the block device name, you will not able to use CDROM. Type the following command to view CD-ROM device name:

# isoscan -funC disk

Where,

-F : Generate a full listing, displaying the module's class, instance number, hardware path, driver, software state, hardware type, and a brief description.
-u : Scan and list usable I/O system devices instead of the actual hardware.
-n : List device file names in the output. Only special files in the /dev directory and its subdirectories are listed.
-C disk : Scan the system hardware and list all the devices belonging to the disk device class.

Step # 2: Create a mount point

Next create a mount point for CD-ROM such as /cddisk, enter:

# mkdir /cddisk
Step # 3: Mount the CD
Mount the cd, enter:

# mount -o ro /dev/dsk/cXtYdZ /cddisk
Replace /dev/dsk/cXtYdZ with actual device name such as /dev/dsk/c1t3d0:

# mount -o ro /dev/dsk/c1t3d0 /cddisk
To access file use normal UNIX cd, ls, cp commands, enter:

# cd /cddisk
# ls
# cp file /somewhere/else

Step # 4: Update /etc/fstab

Update your /etc/fstab file for automatic mounting, enter:

# vi /etc/fstab
Append following line, enter:

/dev/dsk/c1t3d0 /cddisk cdfs ro 0 0
Save and close the file. Use mountall command to mount all file system listed in /etc/fstab, enter:

# mountall
Above will mount all unmounted file systems listed in /etc/fstab.

Step # 5: Unmount the CD

To unmount the cd, enter:

# umount /cddisk