Tag Archives: LINUX

most useful rsync commands

Rsync (Remote Sync) is a most commonly used command for copying and synchronizing files and directories remotely as well as locally in Linux/Unix systems. With the help of rsync command you can copy and synchronize your data remotely and locally across directories, across disks and networks, perform data backups and mirroring between two Linux machines.
Advantages of RSYNC:

Supports copying links, devices, owners, groups and permissions.
It’s faster than scp (Secure Copy) because rsync uses remote-update protocol which allows to transfer just the differences between two sets of files. First time, it copies the whole content of a file or a directory from source to destination but from next time, it copies only the changed blocks and bytes to the destination.
Rsync consumes less bandwidth as it uses compression and decompression method while sending and receiving data both ends.

Options for rsync command:
-v : verbose
-r : copies data recursively (but don’t preserve timestamps and permission while transferring data
-a : archive mode, archive mode allows copying files recursively and it also preserves symbolic links, file permissions, user & group ownerships and timestamps
-z : compress file data
-h : human-readable, output numbers in a human-readable format
-e : used to specify the remote shell protocol.

Examples:
1) Copy/Sync files between two directories in local machine.
[root@localhost source]# pwd
/tmp/source
[root@localhost source]# ls -ltr
total 0
-rw-r–r–. 1 root root 0 Feb  3 22:44 t
-rw-r–r–. 1 root root 0 Feb  3 22:44 t1
-rw-r–r–. 1 root root 0 Feb  3 22:44 t2
[root@localhost source]#
[root@localhost source]# ls -l /tmp/destination/
total 0

There is no files in destination and there is files in the source directory. We use the rsync to copy the files.
[root@localhost source]# rsync -avzh /tmp/source/ /tmp/destination/
sending incremental file list
source/
source/t
source/t1
source/t2

sent 188 bytes  received 73 bytes  522.00 bytes/sec
total size is 0  speedup is 0.00

The files are copied to destination folder. 

2) Copy/Sync two directories between two servers.

. rsync
Source and destination can be any server details.

Source:
[root@localhost ~]# hostname -I
192.168.41.136 192.168.1.3 192.168.122.1
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# ls -l /tmp/source/
total 0
-rw-r–r–. 1 root root 0 Feb  3 22:44 t
-rw-r–r–. 1 root root 0 Feb  3 22:44 t1
-rw-r–r–. 1 root root 0 Feb  3 22:44 t2

Destination:

[root@localhost ~]# hostname -I
192.168.41.135 192.168.122.1
[root@localhost ~]#
[root@localhost ~]# ls -l /tmp/destination
ls: cannot access /tmp/destination: No such file or directory

Below command copy the files to destination server, also create the directory destination.

[root@localhost ~]# rsync -avhz /tmp/source/ root@192.168.41.135:/tmp/destination/
root@192.168.41.135’s password:
sending incremental file list
created directory /tmp/destination
source/
source/t
source/t1
source/t2

sent 188 bytes  received 73 bytes  74.57 bytes/sec
total size is 0  speedup is 0.00
[root@localhost ~]#

3) Rsync over ssh protocol

We can use the ssh protocol to copy the content from one server to another server. Ssh is a secure protocol which perform the encryption of the data when we transfer the data over the wire.

[root@localhost ~]# rsync -avhze ssh /tmp/source/ root@192.168.41.135:/tmp/destination/
root@192.168.41.135’s password:
sending incremental file list

sent 77 bytes  received 13 bytes  25.71 bytes/sec
total size is 0  speedup is 0.00

4) To see the progress while transferring the file use below command

[root@localhost ~]# rsync -avhze ssh /tmp/source/ –progress root@192.168.41.135:/tmp/destination/
root@192.168.41.135’s password:
sending incremental file list
source/
source/content
       9.40M 100%  107.63MB/s    0:00:00 (xfer#1, to-check=3/5)

sent 32.12K bytes  received 35 bytes  9.19K bytes/sec
total size is 9.40M  speedup is 292.33
5) Include and exclude options

            –exclude=PATTERN       exclude files matching PATTERN
            –exclude-from=FILE      read exclude patterns from FILE
            –include=PATTERN       don’t exclude files matching PATTERN
            –include-from=FILE      read include patterns from FILE
            –files-from=FILE            read list of source-file names from FILE

Here we specify the exclude to exclude some files and use include to give exception for some files from excluding.

[root@localhost ~]# rsync -avhze ssh /tmp/source/ –include ‘t2’ –exclude ‘*’ root@192.168.41.135:/tmp/destination/
root@192.168.41.135’s password:
sending incremental file list
t2

sent 63 bytes  received 31 bytes  26.86 bytes/sec
total size is 0  speedup is 0.00

The above command send only the t2 and exclude all the files.

6) “-delete” option

If you want to sync the source and destination and you want to delete files in destination which are not in the source.

[root@localhost source]# rsync -avhz –delete /tmp/source/ root@192.168.41.135:/tmp/destination/
root@192.168.41.135’s password:
sending incremental file list
./
deleting test.txt
deleting t2

sent 65 bytes  received 15 bytes  22.86 bytes/sec
total size is 9.40M  speedup is 117500.00

7) Set maximum file size for the files to transfer.

We can see the maximum size for the files rsync to transfer. If the files size exceeds more than that files won’t get transferred.

[root@localhost source]# rsync -avhz –max-size=’1024k’ /tmp/source/ root@192.168.41.135:/tmp/destination/
root@192.168.41.135’s password:
sending incremental file list
./
t
t1
we

sent 206 bytes  received 72 bytes  111.20 bytes/sec
total size is 868.33M  speedup is 3123478.79
The above command don’t transfer the files whose size is more than 1MB.

8) Automatically delete the source files after the transfer to destination.

After transferring the files to destination it removed all files from source folder.

[root@localhost source]# rsync –remove-source-files -avhz /tmp/source/ root@192.168.41.135:/tmp/destination/
root@192.168.41.135’s password:
sending incremental file list
content
donttf

sent 2.95M bytes  received 50 bytes  137.14K bytes/sec
total size is 868.33M  speedup is 294.50
 [root@localhost source]#
[root@localhost source]# ls -l /tmp/source/
total 0

9) Set bandwidth limit to transfer the data.

–bwlimit=KBPS          limit I/O bandwidth; KBytes per second

[root@localhost source]# rsync –bwlimit=100 -avhz /tmp/source/ root@192.168.41.135:/tmp/destination/
root@192.168.41.135’s password:
sending incremental file list
./

sent 29 bytes  received 15 bytes  12.57 bytes/sec
total size is 0  speedup is 0.00
10) Synchronize only the directory structure not the files.

Use the “-d” option to synchronize only the directory structure not the data files.

[root@localhost source]# rsync -vd /tmp/source/ root@192.168.41.135:/tmp/destination/
root@192.168.41.135’s password:
building file list … done

sent 22 bytes  received 12 bytes  5.23 bytes/sec
total size is 0  speedup is 0.00

11) Don’t create new files in destination just copy existing files from source to destination which are already exist in the source.

[root@localhost source]# rsync –existing -avhz /tmp/source/ root@192.168.41.135:/tmp/destination/
root@192.168.41.135’s password:
sending incremental file list

sent 26 bytes  received 12 bytes  10.86 bytes/sec
total size is 0  speedup is 0.00

Create local repository using CD/DVD in CentOS/Redhat

Follow below steps to create yum repository using the CD/DVD

“/etc/yum.repos.d” is the location which contain all the files related to repository.

1) Login as root user
2) mount the cd/dvd

3) create a file in “/etc/yum.repos.d” named as “localrepo.repo” with below content

4) Execute “yum-config-manager –enable localrepo” to enable the repository

5) Execute “yum repolist” command to list the repos which are enabled

Now the creation of repository is completed and you can install the packages required using yum command.

EX:-

yum -y install gcc*

yum update or yum install failing yummain.user_main(sys.argv[1:], exit_code=True)

Yum install or yum update command failing with below errors

Traceback (most recent call last):
  File “/bin/yum”, line 29, in
    yummain.user_main(sys.argv[1:], exit_code=True)
  File “/usr/share/yum-cli/yummain.py”, line 365, in user_main
    errcode = main(args)
  File “/usr/share/yum-cli/yummain.py”, line 271, in main
    return_code = base.doTransaction()
  File “/usr/share/yum-cli/cli.py”, line 773, in doTransaction
    resultobject = self.runTransaction(cb=cb)
  File “/usr/lib/python2.7/site-packages/yum/__init__.py”, line 1736, in runTransaction
    if self.fssnap.available and ((self.conf.fssnap_automatic_pre or
  File “/usr/lib/python2.7/site-packages/yum/__init__.py”, line 1126, in
    fssnap = property(fget=lambda self: self._getFSsnap(),
  File “/usr/lib/python2.7/site-packages/yum/__init__.py”, line 1062, in _getFSsnap
    devices=devices)
  File “/usr/lib/python2.7/site-packages/yum/fssnapshots.py”, line 158, in __init__
    self._vgnames = _list_vg_names() if self.available else []
  File “/usr/lib/python2.7/site-packages/yum/fssnapshots.py”, line 56, in _list_vg_names
    names = lvm.listVgNames()
lvm.LibLVMError: (0, ”)

Solution :

reboot server
yum clean metadata
yum clean all
yum update

Scheduling in crontab

CRONTAB

This is a scheduler at OS level, which we used to schedule the jobs/scripts/commands at a particular time.

Format of crontab is

Minute Hour “DATE OF MONTH” “MONTH” “DAY OF WEEK” COMMAND/SCHRIPT(with full path)


*     *     *   *    *     COMMAND/SCHRIPT
–     –     –   –    –
|     |     |   |    |
|     |     |   |    +—– day of week (0 – 6) (Sunday=0)
|     |     |   +——- month (1 – 12)
|     |     +——— day of        month (1 – 31)
|     +———– hour (0 – 23)
+————- min (0 – 59)


If you want to schedule a script. 

execute “crontab -e”, this will open a file in vi editor there you need to give the details when you want to run the script and save the file.

Example:

[root@localhost ~]# cat /tmp/test.sh
echo “I am executing the job `date`” > /tmp/logfile

crontab -e

* * * * * /tmp/test.sh

[root@localhost ~]# cat /tmp/logfile
I am executing the job Mon Dec 18 09:53:01 IST 2017

Here I scheduled the job to run every minute. You can schedule the job as per your requirement.

PostgreSQL auto start script

#! /bin/bash
#
# chkconfig: 2345 80 05
# description: start and stop PostgreSQL Database Enterprise Edition
#

#
# Note: Change the value of POSTGRES to the login name of the PostgreSQL owner
POSTGRES=postgres

export PGDATA=/home/postgres/DATA
export PGPORT=5432
export PGSOFTWARE=/home/postgres/9.6_SOFTWARE #location where we installed the software


PATH=${PATH}:$PGSOFTWARE/bin
export PATH

case $1 in
‘start’)
        echo $”Starting PostgreSQL: “
        if [[ `whoami` == ‘postgres’ ]]
        then
        $PGSOFTWARE/bin/pg_ctl start
        else
        su $POSTGRES -c “$PGSOFTWARE/bin/pg_ctl start”
        fi
        ;;
‘stop’)
        echo $”Shutting down PostgreSQL: “
        if [[ `whoami` == ‘postgres’ ]]
        then
        $PGSOFTWARE/bin/pg_ctl start
        else
        su $POSTGRES -c “$PGSOFTWARE/bin/pg_ctl stop”
        fi
        ;;
‘status’)
        echo $”Status of PostgreSQL: “
        if [[ `whoami` == ‘postgres’ ]]
        then
        $PGSOFTWARE/bin/pg_ctl status
        else
        su $POSTGRES -c “$PGSOFTWARE/bin/pg_ctl status”
        fi
        ;;
‘restart’)
        echo $”Restarting PostgreSQL: “
        if [[ `whoami` == ‘postgres’ ]]
        then
        $PGSOFTWARE/bin/pg_ctl restart
        else
        su $POSTGRES -c “$PGSOFTWARE/bin/pg_ctl restart”
        fi
        ;;
*)
        echo “usage: $0 {start|stop|restart|status}”
        exit
        ;;
esac
exit

Get data from Oracle table into Linux array

I have a table “machines” in Oracle database with below content.

select * from machines;

HOSTNAME             IPADDRESS
——————– ——————–
machine1             192.68.1.1
machine2             192.68.1.2

I need to run a function for each hostname and ipaddress and repeat this for all the rows. I am taking the content into array and loop over the results and run the function for each row.

Below is the code for the same.


#!/bin/bash
export ORACLE_HOME=/home/oracle/app/oracle/product/12.1.0/dbhome_1
export ORACLE_SID=orcl
export PATH=/home/oracle/app/oracle/product/12.1.0/dbhome_1/bin:$PATH

elements=`sqlplus -s / as sysdba <<EOF
set feedback off
set echo off
set trimspool on
set termout off
set serveroutput on size 100000 format wrapped
set lines 500
set pages 0
select * from test;
EOF`

declare -a my_array=($elements) # Declaring the array and loading the rows 
my_array_length=${#my_array[@]} # getting the lenght of the array
loop_var=0 # loop variable to retrieve the data
while(($loop_var<$my_array_length))
do
echo “Machine “:${my_array[$loop_var]};
echo “IP ADDRESS “:${my_array[$loop_var+1]};
loop_var=$loop_var+2;
done



[oracle@OracleED ~]$ sh /tmp/t.sh
Machine         :machine1

IP ADDRESS      :192.68.1.1

Machine         :machine2

IP ADDRESS      :192.68.1.2

Download software directly from GitHub to Linux(Redhat)

1) Install the git package.

yum -y install git

Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: centos.uhost.hk
 * extras: centos.uhost.hk
 * updates: centos.uhost.hk
Package git-1.8.3.1-6.el7_2.1.x86_64 already installed and latest version

2) Clone the package using the URL.

[root@replication extension]# git clone https://github.com/EnterpriseDB/mysql_fdw.git
Cloning into ‘mysql_fdw’…
remote: Counting objects: 399, done.
remote: Total 399 (delta 0), reused 0 (delta 0), pack-reused 399
Receiving objects: 100% (399/399), 211.40 KiB | 134.00 KiB/s, done.
Resolving deltas: 100% (240/240), done.
[root@replication extension]# cd mysql_fdw/
[root@replication mysql_fdw]# ls -ltr
total 180
-rw-r–r– 1 root root  8965 Oct 25 12:05 README.md
-rw-r–r– 1 root root  1155 Oct 25 12:05 META.json
-rw-r–r– 1 root root  1558 Oct 25 12:05 Makefile
-rw-r–r– 1 root root  1038 Oct 25 12:05 LICENSE
….
….
….

how to create a new filesystem in REDHAT Linux

1) First create a partition on the disk by using the “fdisk” command.

fdisk /dev/sda

enter n to create new partition.
enter the size of the partition.
enter “w” to write the partition table and restart the system if required.

2) Format the partition for the filesystem.

/sbin/mkfs -t ext3 /dev/sda11

[root@node3 ~]# /sbin/mkfs -t ext3 /dev/sda11
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
3260416 inodes, 6516365 blocks
325818 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
199 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000

Writing inode tables: done                           
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 33 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

3) Give a label to the partition

[root@node3 ~]# /sbin/e2label /dev/sda11 /oradisk

4) Add an entry in the /etc/fstab to make the filesystem mount on every reboot.

LABEL=/oradisk          /oradisk                ext3    defaults        1 2

5) Mount the new filesystem

[root@node3 ~]# mkdir /oradisk
[root@node3 ~]# chown oracle:oinstall /oradisk

[root@node3 ~]# mount /dev/sda11 /oradisk
[root@node3 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda5             9.5G  649M  8.4G   8% /
/dev/sda9             2.9G   69M  2.7G   3% /opt
/dev/sda8             4.8G  138M  4.4G   4% /home
/dev/sda7             4.8G  2.7G  1.8G  60% /usr
/dev/sda10            1.9G   36M  1.8G   2% /usr/local
/dev/sda6             4.8G  198M  4.3G   5% /var
/dev/sda3             9.5G  151M  8.9G   2% /tmp
/dev/sda1             2.9G   75M  2.7G   3% /boot
tmpfs                 501M     0  501M   0% /dev/shm
/dev/sda11             25G  173M   24G   1% /oradisk ===> filesystem got mounted

How to configure NFS in REDHAT Linux

If we want to share a folder in a server with another server we can do that using the NFS. Below is very simple configuration I have done , for the same in my Virtual machine.

Below is my configuration

NODE3 : (NFS Client)

192.9.1.103

NODE2 : (NFS Server)

192.9.1.101
Folder to share : /u01/SOFTWARE

Steps to configure

1) Install the required packages for the NFS on both the server(NODE2) and client(NODE3).

nfs-utils-1.0.9-42.el5.x86_64.rpm
nfs-utils-lib-1.0.8-7.6.el5.x86_64.rpm
nfs-utils-lib-devel-1.0.8-7.6.el5.x86_64.rpm
nfs4-acl-tools-0.3.3-1.el5.x86_64.rpm
portmap-4.0-65.2.2.1.x86_64.rpm

To install the package , use the below command.

rpm -ivh

2) ON NFS SERVER

    2a)    make an entry about the directory we want to share in the /etc/exports file like below.

    /u01/SOFTWARE 192.9.1.103(rw,sync,fsid=0)
   
    2b)    Start the services
   
    service nfs start
    service portmap start
   
    2c) Configure service will start automatically after reboot.
   
    chkconfig nfs on
    chkconfig portmap on

3) ON CLIENT

    3a)    execute the below command as root user.

    mkdir /SOFTWARE

    mount 192.9.1.101:/u01/SOFTWARE /SOFTWARE

    [root@node3 etc]# df -h
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/sda5             9.5G  647M  8.4G   8% /
    /dev/sda9             2.9G   69M  2.7G   3% /opt
    /dev/sda8             4.8G  138M  4.4G   4% /home
    /dev/sda7             4.8G  2.7G  1.9G  60% /usr
    /dev/sda10            1.9G   36M  1.8G   2% /usr/local
    /dev/sda6             4.8G  194M  4.3G   5% /var
    /dev/sda3             9.5G  151M  8.9G   2% /tmp
    /dev/sda1             2.9G   75M  2.7G   3% /boot
    tmpfs                 501M     0  501M   0% /dev/shm
    192.9.1.101:/u01/SOFTWARE 59G   12G   44G  21% /SOFTWARE ===> Filesystem got mounted.

    3b)    Make below entry in /etc/fstab of nfs client (NODE2) to mount the filesystem automatically after reboot.

    192.9.1.103:/u01/SOFTWARE /SOFTWARE             nfs4     soft,intr,rsize=8192,wsize=8192,nosuid

« Older Entries