Rsync Between Two Local Folders

broken image


Rsync is a command-line tool for copying files and directories between local and remote systems that should be in every Linux sysadmin's toolbox.
  1. Rsync Folder To Folder
  2. How To Rsync Folders
  3. Rsync Between Two Local Folders Windows 10
  4. Rsync Two Directories
  5. Rsync How To Use
  • Rsync is very powerful tool to take backups, or sync files and directories between two different locations (or servers). You know this already, as we presented you with practical examples on rsync earlier. In a typical backup situation, you might want to exclude one or more files (or directories) from the backup.
  • The only local files synced back to the server are files that also exist on the server and were modified locally (more recently than the server versions) - which is not likely in the OP's scenario. As you hint at, methinks true 2-way syncing can't be done with rsync, because saving state between runs is required. – mklement0 Jun 6 '14 at 23:56.
  • You need to use the rsync command. The rsync remote-update protocol allows rsync to transfer just the differences between two sets of files across the network connection, using an efficient checksum-search algorithm. Make sure it is installed on all servers for remote copy.

More Linux resources

Rsync -recursive /home/jon/Desktop/data /home/jon/Desktop/data2 The -recursive option provides an easy way to copy an entire folder to a different location, like to the /data2/ folder in the example above. This command copies the entire folder and all of its contents to the new location.

As a sysadmin, I spend most of my energy on two things (other than making sure there is coffee): Worrying about having backups and figuring out the simplest, best way to do things. One of my favorite tools for solving both problems is called rsync.

Rsync Folder To Folder

Rsync was created by one of the same people who invented Samba, Andrew Tridgell. It is such a useful and flexible tool that it's included in every version of Linux and has been ported to other operating systems. Most simply, rsync is a tool for copying files. However, it's much more powerful than that.

  • It keeps two sets of files up to date and synchronized.
  • It runs as a command and can be scripted.
  • It compresses and encrypts the data stream.
  • It uses multiple types of remote access clients (SSH and RSH, for example).

So it's no surprise that it's a favorite of many systems administrators.

The basics

Like the mv and cp command, in its most basic form rsync just needs a source and a destination:

We copied testfoo to the bar directory. No big deal, really.

Now, let's add a file to ./foo and re-sync:

At this point, we want to note a couple of things. First, when we re-ran rsync, it re-copied testfoo and updated the atime. Also, each time it copies a file, it gives the file a new inode number. Therefore, as far as the filesystem is concerned, it's a totally different file (because it is—it copied all the information each time). Finally, note that when we rsync the file, it changes the ownership to the user who executed the command (root, in this case).

All this is important if we want to make backups. This behavior is the same as the cp command. We can also use the cp command to copy directories recursively, as well as preserve attributes and ownership. The big difference is that rsync can do a checksum of the file and compare source and destination files, where cp just looks at the atime value. Rsync's additional functionality is useful for preserving the backup's integrity (we'll get into integrity later in this series).

So let's update just one of these files and see what rsync does:

Note that this time we used some switches:

  • -a Archive mode, preserves mtime, permissions and symlinks.
  • -r Recursive mode, drills down into any directories and syncs those (should be redundant with the -a switch, but I always specify it anyway).
  • -u Only updates files if the mtime on the source is newer.
  • -v Verbose mode, tells you what it's doing (it's always nice to be able to monitor what's happening. Another useful trick is to pipe this output to a file and check it later).

Restore file with rsync

So let's pretend it's a few weeks later. The CFO calls and says something's wrong—many files are missing from his /foo directory.

We take a look at the backups, and see the missing files:

A quick rsync restore:

And:

The missing files are restored or updated from the more recent backups, but the existing files—which did not change—are left alone. Also, note that the ownership of footoo.txt was preserved.

Wrap up

I encourage you to take a look (as always) at the man page for rsync, and try out this useful command.

Here are a few more switches to consider:

  • -r (recursive)
  • -b (backups)
  • -R (relative)
  • -u (update - copy only changed files)
  • -P (progress)
  • -c (compress)
  • -p (preserve permissions)

In the next article in this series, we'll go a little further and look at remote rsync and some of the other more advanced features of this command.

[ Free online course: Red Hat Enterprise Linux technical overview. ]

What to read next

Rsync is very powerful tool to take backups, or sync files and directories between two different locations (or servers).

You know this already, as we presented you with practical examples on rsync earlier.

Local

In a typical backup situation, you might want to exclude one or more files (or directories) from the backup. You might also want to exclude a specific file type from rsync.

This article explains how to ignore multiple files and/or directories during rsync with examples.

First, create a sample directory structure as shown below (with some empty files) that can be used for testing purpose.

The above command will create a source directory (under your home directory) with the following structure.

How To Rsync Folders

1. Exclude a specific directory

Rsync Between Two Local Folders Windows 10

If you don't want to sync the dir1 (including all it's subdirectories) from the source to the destination folder, use the rsync –exclude option as shown below.

Verify to make sure dir1 is not copied from source directory to destination directory.

2. Exclude multiple directories that matches a pattern

The following example will exclude any directory (or subdirectories) under source/ that matches the pattern 'dir*'

Verify the destination directory to make sure it didn't copy any directories that has the keyword 'dir' in it.

3. Exclude a specific file

Rsync Between Two Local Folders

To exclude a specific file, use the relative path of the file in the exclude option as shown below.

Verify the destination directory to make sure it didn't copy the specific file ( dir1/dir2/file3.txt in this example).

4. Exclude path is always relative

Rsync Two Directories

If you are not careful, you might make this mistake.

In the following example, the exclude option seems to have a full path (i.e /dir1/dir2/file3.txt). But, from rsync point of view, exclude path is always relative, and it will be treated as dir1/dir2/file3.txt. In the example below, rsync will look for dir1 under source directory (and not under / root directory).

So, the above command is exactly same as the following. Just to avoid confusion (and to make it easy to read), don't give / in front of the exclude path.

5. Exclude a specific file type

Rsync How To Use

To exclude a specific file type that has a specific extension, use the appropriate pattern. For example, to exclude all the files that contains .txt as extension, do the following.

Verify the destination directory to make sure it didn't copy the *.txt files.

Note: The above is very helpful, when you want to backup your home directory, but exclude all those huge image and video files that has a specific file extension.

6. Exclude multiple files and directories at the same time

When you want to exclude multiple files and directories, you can always specify multiple rsync exclude options in the command line as shown below.

Wait. What if I had tons of files that I want to exclude from rsync?

I can't keep adding them in the command line using multiple –exclude, which is hard to read, and hard to re-use the rsync command for later.

Between

In a typical backup situation, you might want to exclude one or more files (or directories) from the backup. You might also want to exclude a specific file type from rsync.

This article explains how to ignore multiple files and/or directories during rsync with examples.

First, create a sample directory structure as shown below (with some empty files) that can be used for testing purpose.

The above command will create a source directory (under your home directory) with the following structure.

How To Rsync Folders

1. Exclude a specific directory

Rsync Between Two Local Folders Windows 10

If you don't want to sync the dir1 (including all it's subdirectories) from the source to the destination folder, use the rsync –exclude option as shown below.

Verify to make sure dir1 is not copied from source directory to destination directory.

2. Exclude multiple directories that matches a pattern

The following example will exclude any directory (or subdirectories) under source/ that matches the pattern 'dir*'

Verify the destination directory to make sure it didn't copy any directories that has the keyword 'dir' in it.

3. Exclude a specific file

To exclude a specific file, use the relative path of the file in the exclude option as shown below.

Verify the destination directory to make sure it didn't copy the specific file ( dir1/dir2/file3.txt in this example).

4. Exclude path is always relative

Rsync Two Directories

If you are not careful, you might make this mistake.

In the following example, the exclude option seems to have a full path (i.e /dir1/dir2/file3.txt). But, from rsync point of view, exclude path is always relative, and it will be treated as dir1/dir2/file3.txt. In the example below, rsync will look for dir1 under source directory (and not under / root directory).

So, the above command is exactly same as the following. Just to avoid confusion (and to make it easy to read), don't give / in front of the exclude path.

5. Exclude a specific file type

Rsync How To Use

To exclude a specific file type that has a specific extension, use the appropriate pattern. For example, to exclude all the files that contains .txt as extension, do the following.

Verify the destination directory to make sure it didn't copy the *.txt files.

Note: The above is very helpful, when you want to backup your home directory, but exclude all those huge image and video files that has a specific file extension.

6. Exclude multiple files and directories at the same time

When you want to exclude multiple files and directories, you can always specify multiple rsync exclude options in the command line as shown below.

Wait. What if I had tons of files that I want to exclude from rsync?

I can't keep adding them in the command line using multiple –exclude, which is hard to read, and hard to re-use the rsync command for later.

So, the better way is to use rsync –exclude-from option as shown below, where you can list all the files (and directories) you want to exclude in a file.

First, create a text file with a list of all the files and directories you don't want to backup. This is the list of files and directories you want to exclude from the rsync.

Next, execute the rsync using –exclude-from option with the exclude-list.txt as shown below.

Verify the desitination directory to make sure the files and directories listed in the exclude-list.txt file is not backed-up.





broken image