SysAdmin‎ > ‎

Rsync

How to use rsync

For more info you can look at the sites I used or Google rsync

http://manpages.ubuntu.com/manpages/maverick/man1/rsync.1.html

https://calomel.org/rsync_tips.html

http://www.thegeekstuff.com/2010/09/rsync-command-examples/

http://www.admin-magazine.com/Articles/Using-rsync-for-Backups

http://blog.kdecherf.com/2012/06/30/incremental-backup-with-rsync/

http://www.createdbypete.com/articles/a-practical-guide-to-using-rsync/

http://microrants.blogspot.com/2008/12/how-to-avoid-overlapping-rsync.html

http://linux.die.net/man/1/flock

http://stackoverflow.com/questions/9390134/rsync-cronjob-that-will-only-run-if-rsync-isnt-already-running

http://stackoverflow.com/questions/185451/quick-and-dirty-way-to-ensure-only-one-instance-of-a-shell-script-is-running-at


The basic use of rsync is copying a source to a destination

Basic command is

rsync -av source destination

Replace source with a path to what you want to copy(a file or folder) and replace destination with a path to where you want it to be copied to

Both source and destination can be local or via network

-a option keeps the date, time, permissions, etc of the file the same when it is copied over

-v option increases verbosity or the amount of feedback you get while it runs

An example of local rsync is

rsync -av /home/user/Desktop/someFile /home/user/Desktop/someFolder

An example of network rsync is

rsync -av /home/user/Desktop/someFile someIp:/home/user/Desktop

When the source is a folder, there is a difference between

/home/user/Desktop/someFolder

and

/home/user/Desktop/someFolder/

Without the slash in someFolder, it will copy the folder itself along with the content of the folder

With the slash in someFolder, it will copy only the content of the folder and not the folder itself

Another useful option is

--delete

this deletes files that don't exist in the source folder but still exist in the destination folder

You have to be careful of this command as it will delete files

You can just append the option after -av like this

rsync -av --delete source destination

A good use of rsync is to do incremental backups

Files deleted or modified can be stored in another folder using incremental backups

The command for incremental backups is

rsync -av --backup --backup-dir=”directory” source destination

The directory is where you want to back up modified and deleted files into

For example

rsync -av --delete --backup --backup-dir=”/home/user/Desktop/backupFolder” /home/user/Desktop/someFile /home/user/Desktop/someFolder

A method of using incremental backups is to make the backup folder named by the date by naming directory “backup_$(date + '%Y-%m-%d')”, Y stands for year, m stands for month, d stands for day

You can also add in hour, minute, and second using %H, %M, and %S

An example of this would be

rsync -av --delete --backup --backup-dir=”/home/user/Desktop/backup_$(date + '%Y-%m-%d')” source destination

Comments