Copying files and directories remotely.

Keeping Linux files and directories in sync.

Everything about "rsync"

A Thread πŸ‘‡
What Is Rsync?

Rsync, or Remote Sync, is a fast, versatile and free command-line tool that lets you transfer and sync files and directories to local and remote destinations in an efficient and secure way. .
Why use Rsync?

When copying or moving large no. of files using tools like cp and mv may leave ur data in inconsistent state with part of it still in the original location and part of it in the target destination in case of any interruptions.
These tools takes more time as well. Even for a plain copy, restarting the copy is less than ideal as it will recopy everything that already got copied. This same issue applies to the SSH copy utility scp.
Whereas Rsync is very fast and efficient, copying only the changes from the source and offering customization options.
How to Install Rsync?

On Ubuntu -
apt install rsync

On RedHat -
yum install rsync
Rsync syntax -

rsync options SOURCE DESTINATION

-rAllows to sync data recursively but does not keep ownership for users and groups, permissions, timestamps, or symbolic links.
-aThe archive mode behaves like d recursive mode but keeps all file permissions, symbolic links etc.
-zUsed to compress data during transfers to save space.
-bPerforms a backup during data synchronization.
-hShows the numbers in the output in a human-readable format.
-nDoes a dry run. Used for testing before the actual synchronization takes place.
-eInstructs the rsync to use the SSH protocol for remote transfers.
-progressDisplays the transfer progress during synchronization.
-vVerbose output. Displays the details of the transfer.
-qUsed to suppress the output for the rsync command and options.
SRC - define the Source directory.
DEST - define the Destination directory.
USER - define the Remote username.
HOST - define the Remote hostname or IP Address.
Synchronize Directories locally -

To copy one file to another directory on a local machine, type in the source file's full path, followed by the target destination.

# rsync -r test1/ test2
Plz Note that there is a trailing slash (/) at d end of the first argument (test1/) in d above commands.

rsync -r test1/ test2

This is necessary to mean β€œd contents of test1”. The alternative, w/o d trailing slash, would place test1, including d directory, within test2.
Always run rsync command in a dry run mode first.

Rsync provides a method for doing this by passing the -n or --dry-run options. The -v flag (for verbose) is also necessary to get the appropriate output.

# rsync -rnv test1/ test2

# rsync -rnv test1 test2
Copy Multiple Files Locally -

To copy multiple files with rsync, add full paths of the source files:

# rsync -v test1/abc.txt test1/def.txt test2/
Copying a File or Directory from Local to Remote Machine -

Add the IP address & d destination after d source directory. Remember to put a colon (:) after d remote host's IP address, with no spaces before the destination.

# rsync -avzP test1 root@lco-linux-worker2:/root/rsync
Please note that the target directory on the remote host was not created before. The rsync tool takes care of that and create a directory for you and copy the data.
Copy a File or Directory from a Remote to a Local Machine -

Rsync supports transferring files from a remote server to your local machine.

To pull a directory from a server, specify its IP address before the source.

# rsync -avzP root@lco-linux-worker2:/root/rsync test2/
Delete a Nonexistent Source File or Directory from Destination -

Use the --delete option to keep the source and the target in sync.

This option tells rsync to delete any file or directory at the destination if the source does not have it.
Delete Source Files After Transfer -

sometimes you may want to delete d source files after d transfer. For e.g. we no longer need weekly backups at d source after d move to backup server.

# rsync -vP --remove-source-files test1/abc.txt root@lco-linux-worker2:/root/rsync/test1
Exclude Files and Directories -

We can exclude files or directories with rsync.

To exclude a file while transferring the contents of a folder with rsync, specify the file and the relative path.

# rsync -avP --exclude 'jkl.txt' test1/ root@lco-linux-worker2:/root/rsync/test1
Exclude a Specific Directory -

Specify a directory you want to exclude (instead of a file name):

# rsync -avP --exclude 'exclude_demo' test1/ root@lco-linux-worker2:/root/rsync/test1
Exclude Files or Directories Based on a Pattern -

Use an asterisk * (wildcard) when defining a file or directory name to exclude everything that matches the pattern.

To exclude files that start with demo, run this command:

rsync -av --exclude 'demo*' sourcedir/ destinationdir/
Exclude a Specific File Type -

To exclude a specific file type, in this case .mpv, run this command:

rsync -av --exclude '*.mpv' sourcedir/ destinationdir/
Exclude Files by Size-

To exclude all files larger than 800MB, run this command:

rsync -av --max-size=800m sourcedir/ destinationdir/
Set Maximum File Size for Transfer -

To transfer files no larger than 200KB, use this command:

rsync -av --max-size=200k /home/test/dev test@lco-linux-worker2:/home/test/dev/rsync/
Set Minimum File Size for Transfer -

To skip any file smaller than 50KB, run this command:

# rsync -av --min-size=50k /home/test/dev test@lco-linux-worker2:/home/test/dev/rsync/
Define rsync Bandwidth Limit -

To set the maximum transfer speed to 100KB/s, enter:

# rsync -av --bwlimit=100 --progress /home/test/dev test@lco-linux-worker2:/home/test/dev/rsync/
Avoid Copying Source File if d Same Destination File is Modified -

Sometimes it may happen that you modify a file at d destination and do not want to let rsync overwrite it.

Use the -u option for this.

rsync -avu /home/test/dev test@lco-linux-worker2:/home/test/dev/rsync/
Retweet if you like the thread and follow me for more content. PC: fayzanzahid

β€’ β€’ β€’

Missing some Tweet in this thread? You can try to force a refresh
γ€€

Keep Current with Rakesh Jain

Rakesh Jain Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @devops_tech

9 Oct
These days we are hearing a lot about #web3.

What's that?

A short thread πŸ‘‡
What was web1 or web?
The very first stage of development on the World Wide Web where only simple static websites used to exist.

Those were the times when Personal web pages (mostly static pages) used to be hosted on ISP-run web servers, or on free web hosting services.
What is web2?

Today's internet is all about web2. As per @Wikipedia Web 2.0 refers to websites that emphasize user-generated content, ease of use, participatory culture and interoperability for end users where websites and apps allow anyone to create content and share.
Read 5 tweets
9 Oct
Kubernetes security best practices in short -

A Thread πŸ‘‡ PC: Threat Stack
1. Keep the Kubernetes cluster up to date.
2. Ensure That Only Authorized Images are Used in Your Environment
3. Limit Direct Access to Kubernetes Nodes
4. Create Administrative Boundaries between Resources
5. Keep Secrets Secret!
6. Define Resource Quota
7. Implement Network Segmentation
8. Apply Security Context to Your Pods and Containers
9. Restrict API access.
10. Restrict SSH access.
Read 6 tweets
9 Oct
Linux Networking Tools -

ss, nmap, dig, netcat, ping, tcpdump, socat, top, ethtool

A Thread πŸ‘‡
SS -

ss command is a tool that is used for displaying network socket related information on a Linux system.
nmap -

Nmap is short for Network Mapper. It is an open-source Linux cmd-line tool that is used to scan IPs & ports in a nw & to detect installed apps. Nmap allows nw admins to find which devices r running on their nw, discover open ports & services, and detect vulnerabilities.
Read 12 tweets
7 Oct
SSL Decoded!

Everything you need to know about Secure Socket Layer.

A Thread πŸ‘‡
What is SSL ?

It’s a protocol for encrypting and securing communications that take place on the Internet. It's now replaced by an updated protocol called TLS (Transport Layer Security) some time ago.
The main use case for SSL/TLS is securing communications between a client and a server, but it can also secure email, VoIP, and other communications over unsecured networks.
Read 23 tweets
7 Oct
I rarely use password login. And many folks out there even today aren't sure about ssh keys and their usage.

Everything about SSH Keys.

A Thread πŸ‘‡
What is SSH?

SSH is a secure protocol used as the primary means of connecting to Linux servers remotely. It provides a text-based interface by spawning a remote shell. After connecting, all commands you type in your local terminal are sent to d remote server and executed there.
Today, the SSH protocol is widely used to login remotely from one system into another, and its strong encryption makes it ideal to carry out tasks such as issuing remote commands and remotely managing network infrastructure and other vital system components.
Read 27 tweets
5 Oct
Python Cheat Sheet πŸ‘‡
Part - 1 Image
Part -2 Image
Read 4 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(