Wednesday, May 21, 2014

Rsync Command to Sync files in Unix/Linux.

Rsync (Remote Sync) command is used 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 data remotely and locally across directories, across disks and networks, perform data backups and mirroring between two Linux/Unix machines.
  • It supports copying links, devices, owners, groups and permissions.
  • It’s faster than scp (Secure Copy) because rsync uses remote-update protocol which allows us 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. From next time. it copies only the changed blocks and bytes to the destination.
  • It consumes less bandwidth as it uses compression and decompression method while sending and receiving data both ends.
  • It copy files from locally, to/from another host over any remote shell, or to/from a remote rsync daemon.
  • Its widely used for backups and mirroring and as an improved copy command for everyday use.
  • It can use exclude and exclude-from options similar to GNU tar.
  • It does not require root privileges.
  • It supports anonymous or authenticated rsync servers which is ideal for mirroring.
Syntax

Local use:
rsync [OPTION...] SRC... [DEST]

Access via remote shell (PULL):
rsync [OPTION...] [USER@]HOST:SRC... [DEST]

Access via remote shell (PUSH):
rsync [OPTION...] SRC... [USER@]HOST:DEST

Access via rsync daemon (PULL):
rsync [OPTION...] [USER@]HOST::SRC... [DEST]
rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]

Access via rsync daemon (PUSH):
rsync [OPTION...] SRC... [USER@]HOST::DEST
rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST

Common Examples

Copy/Sync Files and Directory Locally

rsync -zvh aaa.tar /tmp/aaa/ destination_folder

destination_folder is current folder by default.

Copy a Directory from Local Server to a Remote Server

rsync -avz anand_files/ anand@192.168.0.1:/home/anand_files/
rsync -avzh anand@192.168.0.1:/home/anand_file /tmp/anand_files

Copy a File from a Remote Server to a Local Server with SSH

rsync -avzhe ssh anand@192.168.0.1:/anand/A.log /tmp/anand/
rsync -avzhe ssh /tmp/a.log anand@192.168.0.1:/backup/anand/

Show Progress While Transferring Data with rsync

rsync -avzhe ssh --progress /home/soft anand@192.168.0.1:/anand/soft

Use of –include and –exclude Options

It excludes/includes particular char/string file names while copying.

rsync -avze ssh --include 'RAJ*' --exclude '*' anand@192.168.0.1:/anand/unix/ /root/anand/unix/

Set the Max/Min Size of Files to be Transferred

rsync -avzhe ssh --min-size='5k' --max-size='2048k' /anand/unix/ anand@192.168.0.1:/anand/unix

Automatically Delete source Files after successful Transfer

rsync --remove-source-files -zvh aaa.tar /tmp/anand/

Set Bandwidth Limit (KBPS) and Transfer File

rsync --bwlimit=1024 -avzhe ssh  /anand/unix/  anand@192.168.0.100:/anand/unix/temp/

What is difference between Row Chaning and Row Migration and how to resolve it?

Row migration & Row chaining mainly depends on Orale DB Block Size will be 8K, will ranging from 2K to 32K, which will depend on OS. It occurs at a time when table row data is INSERTED or UPDATED. 

Row migration is typically caused by an UPDATE & Row chaining is typically caused by an INSERT.

Row Migration:

When we performing any Update statement on a table row, if the content of new data is not able to fit in one database block. It means in update of a larger content, overall row length increases and free space of database block is already full.

It replaces content of original block to a pointer address - rowid and entire row is moved to the rowid defined in original block.

Row Migration is caused due to lesser value of PCTFREE for the table.There is not enough place in the BLOCK for UPDATES.

To avoid row migration, table should have PCTFREE value to be set in such a way that there is some enough space within the BLOCKS for UPDATES.

select table_name,chain_cnt,pct_free,pct_used from dba_tables where table_name = table_name ;

alter table table_name pctfree 30;

Row Chaining:

When a row data is too large to fit into a single database block. For example, if your database is of 8K db block size, and we need to insert a row of 20K into it. At this time, Oracle will use 3 db blocks and store the row in pieces.

Below are the reasons at a time when row chaining is occurs:
  • Tables having rowsize exceeds the db blocksize.
  • Tables with LONG and LONG RAW or any other LOB columns.
  • Tables with more then 255 columns.
Oracle internally stores the data in multiple blocks, one block stores all forwarding address - rowids and the data on another two or more blocks.

Row Chaining is UNAVOIDABLE but Row Migration can be MANAGED and resolved through reorganization.Whenever a row migration or row chaining will occur, I/O performance will be decrease as Oracle has to scan more than one data block to retrieve data for the row.

We can identify Row Changing and Row Migration by querying table CHAINED_ROWS, USER_TABLES and V$SYSSTAT or in an ADDM/Statspack report and using ANALYZE command.

Run below command on user to create a system table named - CHAINED_ROWS.

@$ORCL_HOME\ADMIN\utlchain.sql

select owner_name,table_name,count(head_rowid) row_count from CHAINED_ROWS where table_name = table_name GROUP BY owner_name,table_name;

SELECT owner, table_name, chain_cnt , initial_extent, num_rows, blocks, empty_blocks, avg_space, avg_row_len 
FROM user_tables WHERE chain_cnt > 0 and table_name = table_name;

SELECT name, value FROM v$sysstat WHERE name = 'table fetch continued row';

analyze table table_name list chained rows;

select count(*) from chained_rows where table_name = table_name ;


To eliminate row migration / row chaining .

alter table table_name MOVE;

This statement does is to move the table to a new segment. Oracle reads the data from current table and rebuilds the table completely. During this reorganization all migrated rows get fixed.

This causes indexes on the table to be marked UNUSABLE, because it changes the rowids of rows in the table. It generates ORA-01502 error, before REBUILDING of a table .

Do remember that you have to REBUILD all your indexes and repopulate table/index statistics in DB before using the table.

We can perform Import/Export activity as an approach to eliminate the migrated rows from a table.