RSync Examples

download RSync Examples

of 2

description

Rsync commandline tools example

Transcript of RSync Examples

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. Furthermore this synchronization happens for all newly created files or modified files or directories. During copy the file system permissions are preserved.#Some advantages and features of Rsync command#1) It efficiently copies and sync files to or from a remote system.#2) Supports copying links, devices, owners, groups and permissions.#3) Its faster than scp (Secure Copy) because rsync uses remote-update protocol which allows to transfer just the differences between two sets of files. #4) 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.#5) Rsync consumes less bandwidth as it uses compression and decompression method while sending and receiving data both ends.Some common options used with rsync commands-v : verbose-r : copies data recursively (but dont 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 "ssh options" : specify the ssh as remote shell# 1. Copy/Sync Files and Directory Locally i.e. inside a serverrsync -zh /home/scriptcu/ /home/santos_c/# 2. Copy/Sync Files and Directory to or From a Serverrsync -az rpmpkgs/ [email protected]:/home/# 3. Rsync Over SSH : With rsync, using SSH protocol you can be ensured that your data is being transferred in a secured connection with encryption.# Task: Copy a File from a Remote Server to a Local Server with SSH:rsync -avzhe ssh [email protected]:/root/install.log /tmp/# Task: Copy a File from a Local Server to a Remote Server with SSHrsync -avz -e ssh /home/ramesh/ [email protected]:/backup/ramesh/rsync -avzhe ssh backup.tar [email protected]:/backups/rsync -ave "ssh" /home/user/sync_dir/ remote_user@remote_host:/home/user/remote_sync_dir/rsync -azh /local/path/file [email protected]:/remote/path/filersync -azh /local/path/file -e 'ssh -p 22334' [email protected]:/remote/path/file# 5. Use of include and exclude Options# Task: Here in this example, rsync command will include those files and directory only which starts with R and exclude all other files and directory.rsync -avze ssh --include 'R*' --exclude '*' [email protected]:/var/lib/rpm/ /root/rpm# Task: Copy only the directory structure without copying any files:rsync -a -f"+ */" -f"- *" source/ destination/rsync -a --include='*/' --exclude='*' source/ destination/rsync -v -d [email protected]:/var/lib/ .# Task: Copy only directories and sh files:rsync -a -f"+ */" -f"+ *.sh" -f"- *" source/ destination/# Task: Copy everything but exclude .git directories:rsync -a -f"- .git/" -f"+ *" source/ destination/# Exclude multiples files and directories at the same time$ cat > exclude-list.txtfile1.txtdir3/file4.txtWhen having multiple directories and/or files to exclude, make a text file and use the "--exclude-from" switch. Make a file called exclude-list.txt and in it list your exclusions. e.g.:$ cat > exclude-list.txtfile1.txtdir3/file4.txtThen your rsync would look something like:rsync -avz --exclude-from 'exclude-list.txt' source/ destination/# 6. Use of delete Option# If a file or directory not exist at the source, but already exists at the destination, you might want to delete that existing file/directory at the target while syncing .rsync -avz --delete [email protected]:/var/lib/rpm/ .# 7. Set the Max Size of Files to be Transferredrsync -avzhe ssh --max-size='200k' /var/lib/rpm/ [email protected]:/root/tmprpm# 8. Automatically Delete source Files after successful Transferrsync --remove-source-files -zvh backup.tar /tmp/backups/# 9. Do a Dry Run with rsync# Task: If you are new to rsync and dont know what exactly your command going do then Rsync could really mess up the things in your destination folder. To aovid this use dry run.rsync --dry-run --remove-source-files -zvh backup.tar /tmp/backups/ssh remote_host1_user@remote_host1 "rsync -ave ssh source_sync_dir remote_host2_user@remote_host2:target_sync_dir"# Task: Synchronize a remote directory with a local directory$ rsync -r -a -v -e "ssh -l jerry" --delete openbsd.nixcraft.in:/webroot/ /local/webroot# Task: Synchronize a local directory with a remote rsync server or vise-versa. You can ignore delete option.$ rsync -r -a -v --delete rsync://rsync.nixcraft.in/cvs /home/cvs#OR$ rsync -r -a -v --delete /home/cvs rsync://rsync.nixcraft.in/cvs# Task: Mirror a directory between my "old" and "new" servers. You can mirror a directory between my "old" (my.old.server.com) and "new" web server with the command (assuming that ssh keys are set for password less authentication)$ rsync -zavrR --delete --links --rsh="ssh -l vivek" my.old.server.com:/home/lighttpd /home/lighttpd# Task: Execute remote shell command to rsync filesrsync -avR calomel@somemachine:'`find /data/video -name "*.[avi]"`' /download/