facebook youtube pinterest twitter reddit whatsapp instagram

Transferring files with rsync In [Ubuntu]

If you are coming from a GUI world, then you know transferring files from one location or folder to another is as easy as doing a few mouse clicks, the way it works in GNU/Linux is a bit different, although similar.

In GNU/Linux, rsync is one of the most useful utilities available for transferring data, it is a fast, powerful and versatile copying tool, it can copy locally, to/from another host over any remote shell, or to/from a remote rsync daemon.

Unlike in the GUI environment that features a minimum option for transferring, rsync offers a large number of options for the transfer of data, what I find interesting is that it can be used for backups and mirroring files.

Additional Features are:

  • support for copying links, devices, owners, groups, and permissions
  • exclude and exclude-from options similar to GNU tar
  • a CVS exclude mode for ignoring the same files that CVS would ignore
  • can use any transparent remote shell, including ssh or rsh
  • does not require super-user privileges
  • pipelining of file transfers to minimize latency costs
  • support for anonymous or authenticated rsync daemons (ideal for mirroring)

Note: Rsync doesn't support the transfer of files between two remote hosts, it only copies files from a remote host to local, and vice versa.

To get started with rsync, I'll suggest you create a test folder, and a test files in the folder for illustration, so, we would be backing up the test folder.

First, create a test folder:

sudo mkdir /home/test

I am creating the test folder in the home directory of my current user

Change your working directory into the the test folder, and create a random text file:

sudo touch file.txt

The next point of action is utilizing the rsycn command to backup our test folder:

sudo rsync -r /home/test /backup

The above command is using rsync as root to copy the content of the test directory (the source directory) to a backup directory (/backup) (which is the target directory, it would automatically create one if you don't already have a specified folder)

The -r option copies the file recursively, say you have multiple folder in the test directory, it would copy the directories, sub directories, and any files it find in there.

The directory is already owned by the root user, but in cases where you need to retain permission of a specific folder, and you've already copied the file with rsync, you can fix it by adding an -a option, e.g:

sudo rsync -a /home/test /backup

rsync won't copy the file again since it has already been copied before, the only thing it would do here is copy and replace the permission of the source directory to the target directory, so, it basically copies what was different.

If you want to display what the process is doing as it runs, you can activate the verbose mode, e.g

sudo rsync -av /home/test /backup

Copying Data Over SSH

You can use rsync to copy data locally to a remote server via SSH, for example:

sudo rsync -av /home/test ServerA:/backup

The command above is copying the test folder directory to the /backup directory on ServerA, ServerA is a simplification of my SSH Connection, learn how it's done below:

https://devsrealm.com/cloud-computing/ubuntu/simplifying-ssh-connections-with-a-config-file/embed/

Synchronizing Files With Rsycn

Up until now, we've been copying files from one location to another, but what if we actually want to move files from one directory to another?

This is where the --delete option comes in, with this option you can keep files in sync, for example, if you copy a file from location A to B, and you've deleted some files in point A, with the --delete option it would look what has been removed in point A, and also apply the same to B, meaning it delete the removed file in point A to keep it in sync.

An example of the --delete option:

sudo rsync -av --delete /home/test /backup

Keeping the Same File as Separate in Target Location

If you want to copy, and keep the same file as separate in the target location, you use the -b (backup) option, The -b option renames files on the target that are being overwritten, this way you'll still have the original file, example:

sudo rsync -avb --delete /src  /target

Related Post(s)

  • Monitoring Multiple Log Files In RealTime With MultiTail (Ubuntu)

    Oh my... I really find scanning through the logs file time consuming, and painful. Luckily for me, I founded Multitail, which is an awesome, and powerful tool for not only browsing through several f

  • How To Find Files Using Locate in Ubuntu

    It's kind of frustrating when you are searching for a particular file, and you have no idea of how to find it, In this guide, I'll walk you through on two different ways you can find a file in your G

  • Managing MariaDB Databases (Ubuntu Server)

    In this guide, you'll learn how to manage MariaDB databases in your terminal, from connecting to the database server using the mariadb command, creating a database, removing (drop) database, and mana

  • Viewing Disk Usage in Ubuntu

    There are several ways to view disk usage in your Linux system, and that is what we would be going over in this guide... Out of the box, Linux provides us the df command, which is the standard Unix c

  • Understanding & Using Symbolic & Hard Links (Ubuntu)

    Have you ever wondered how you can create shortcuts that link to other files in Linux? Well, let me introduce you to symbolic and hard links. The two types of links in Linux are symbolic links and ha

  • Installing and Using dig in Ubuntu

    dig is an awesome utility for querying DNS name servers. It performs DNS lookups and displays the answers that are returned from the name server(s) that were queried. In this guide, you'll learn how