Backup-Using-RSynC

Backup-Using-RSync
=========================


# yum install rsync

# rsync -avz /home/svk/Pictures/T/ root@192.168.43.55:/Backup/1/
    --------->BackUP
# rsync -avz root@192.168.43.55:/Backup/1/ /home/svk/Pictures/T/    --------->ReStore

===========================

Use Rsync with SSH for Auto Backup
-----------------------------------------------------------------


Run the command on server where the data to backup exist

# ssh-keygen
# ssh-copy-id root@192.168.1.9

('192.168.1.9' is the IP of The server where Backup is Stored)

The below script will backup the Directory '/S-Backup' to Remote server(192.168.1.9) in the '/Backup/Back-' Directory

# cat Back-rsync.sh
D1=BKP-$(date +%I:%M%p-%d-%b-%Y)
rsync -avz /S-Backup/ 192.168.1.9:/Backup/$D1
unset D1
exi
t
#
# chmod +x /root/Back-rsync.sh


Below Cronjob will run Backup script on hourly Basis
# crontab -l
# m h  dom mon dow   command
# Run Backup Script on every Hour
05 * * * *    /root/Back-rsync.sh

#

Backup and Send Log file to Mail
=====================================

# mkdir /test
# vi backup.sh

D1=BKP-$(date +%I:%M%p-%d-%b-%Y)
rsync --delete --logfile=/test/$D1.log -avzq /Data 192.168.1.9:/Backup/$D1
cat /test/$D1.log|mail -s "Backup-Log" user1@test.com
#
# chmod +x backup.sh
# crontab -e

00 22 * * * /root/backup.sh
#
# service crond restart


==========================

Below command synchronize the contents of Directory1 to Directory2, and leave no differences between the two.
If rsync finds that Directory2 has a file that Directory1 does not, it will delete it.
If rsync finds a file that has been changed, created, or deleted in Directory1, it will reflect those same changes to Directory2.

# rsync -av --delete /Directory1/ /Directory2/

Exclude Files From Backup

-------------------------------------------------
# rsync --delete -avz /Data /Test/Backup --exclude=*.tmp
( Where "/Data" is source directory and "/Test/Backup" is destination to save backup,also avoid *.tmp file during backup)



Do Not Overwrite the Modified Files at the Destination
------------------------------------------------------------------------------------------


Use rsync -u option to do exactly that. (i.e do not overwrite a file at the destination, if it is modified).
In the following example, the file called Basenames is already modified at the destination.
So, it will not be overwritten with rsync -u.

# rsync -avzu user1@192.168.1.10:/var/lib/rpm /root/temp

Synchronize only the Directory Tree Structure (not the files)
------------------------------------------------------------------------------------------------------


# rsync -v -d user1@192.168.1.10:/var/lib/ .

Delete the Files Created at the Target
-------------------------------------------------------------------


If a file is not present at the source, but present at the target,
you might want to delete the file at the target during rsync.
In that case, use –delete option as shown below.
rsync delete option deletes files that are not there in source directory.

# rsync -avz --delete user1@192.168.1.10:/var/lib/rpm/ .



Do Not Transfer Large Files
-----------------------------------------------------


# rsync -avz --max-size='100K' user1@192.168.1.10:/var/lib/rpm/ /root/temp/

max-size=100K makes rsync to transfer only the files that are less than or equal to 100K.
You can indicate M for megabytes and G for gigabytes.

Include and Exclude Pattern during File Transfer
--------------------------------------------------------------------------------------


rsync allows you to give the pattern you want to include and exclude files or directories while doing synchronization.

# rsync -avz --include 'P*' --exclude '*' user1@192.168.1.10:/var/lib/rpm/

In the above example, it includes only the files or directories starting with ‘P’ (using rsync include) and excludes all other files.
(using rsync exclude ‘*’ )

To transfer the whole file
----------------------------------------------


We talked, until now, about how good rsync is for incremental backups.
But if you may want to transfer the whole file, all over again, you are free to do so. Just use -W

# rsync -avzW /home/user/mvs/test.avi /backupmedia/

======================================