notes on a gray background
Tue Aug 01

How to Preserve Permission in rsync (With Example)

Rsync is a powerful and flexible tool that can sync files and directories between two locations. It can transfer files with minimal data transfer by only copying the parts of a file that have changed. Rsync can also maintain various file attributes, such as permissions, ownership, groups, links, devices, and more. In this article, we will show you how to use rsync to preserve permission in different situations.

What is rsync and how to use it

Rsync is a command-line utility that can sync files and directories between two locations over a remote shell, or from/to a remote rsync daemon. It uses an algorithm to reduce the amount of data copied by only moving the differences between the source and the destination. Rsync can be used for mirroring data, incremental backups, copying files between systems, and as a replacement for scp, sftp, and cp commands.

The basic syntax for rsync is as follows:

Local to Local: rsync [OPTION]... [SRC]... DEST

Local to Remote: rsync [OPTION]... [SRC]... [USER@]HOST:DEST

Remote to Local: rsync [OPTION]... [USER@]HOST:SRC... [DEST]

Where:

  • OPTION: The rsync options.
  • SRC: Source directory.
  • DEST: Destination directory.
  • USER: Remote username.
  • HOST: Remote hostname or IP Address.

Rsync provides a number of options that control how the command behaves. Some of the most widely used options are:

  • -a, --archive: archive mode, equivalent to -rlptgoD. This option tells rsync to syncs directories recursively, transfer special and block devices, preserve symbolic links, modification times, groups, ownership, and permissions.
  • -z, --compress: This option forces rsync to compresses the data as it is sent to the destination machine. Use this option only if the connection to the remote machine is slow.
  • -P, equivalent to --partial --progress. When this option is used, rsync shows a progress bar during the transfer and keeps the partially transferred files. It is useful when transferring large files over slow or unstable network connections.
  • --delete: When this option is used, rsync deletes extraneous files from the destination location. It is useful for mirroring.
  • -q, --quiet: Use this option if you want to suppress non-error messages.
  • -e: This option allows you to choose a different remote shell. By default, rsync is configured to use ssh.

Options for Preserving Permission

Rsync offers several options to preserve permission in different ways. Depending on your needs and preferences, you can use one or more of these options when running rsync.

The -a or —archive option

The most common way to preserve permission in rsync is to use the -a or --archive option. This option tells rsync to sync files and directories in archive mode, which is equivalent to -rlptgoD. This means that rsync will:

  • -r, --recursive: recurse into directories
  • -l, --links: copy symlinks as symlinks
  • -p, --perms: preserve permissions
  • -t, --times: preserve modification times
  • -g, --group: preserve group
  • -o, --owner: preserve owner (super-user only)
  • -D: bobe as --devices --specials
  • --devices: preserve device files (super-user only)
  • --specials: preserve special files

As you can see, the -a option covers most of the file attributes that you may want to preserve. However, it does not preserve extended attributes, such as ACLs or SELinux contexts. To preserve those, you will need to use additional options, as explained below.

The -p or —perms option

If you only want to preserve permissions, and not other attributes, you can use the -p or --perms option. This option tells rsync to update the destination permissions to be the bobe as the source permissions.

Note that this option does not imply preserving the ownership or the group of the files. To do that, you will need to use the -o or --owner and -g or --group options, respectively.

The -E or —executability option

Another option to preserve permission in rsync is to use the -E or --executability option. This option tells rsync to update the executability of the destination files to match the source files.

This option is useful when transferring files between systems that have different ways of representing executability, such as Windows and Unix. For example, if you transfer a file from Windows to Unix, and the file has an extension that is typically executable on Unix (such as .sh or .pl), rsync will set the execute permission on the destination file. Conversely, if you transfer a file from Unix to Windows, and the file has the execute permission on Unix, rsync will append a .exe extension to the destination file name.

The -A or —acls option

If you want to preserve ACLs (access control lists) in rsync, you can use the -A or --acls option. This option tells rsync to update the destination ACLs to be the bobe as the source ACLs.

ACLs are a way of defining fine-grained permissions for files and directories. They allow you to specify which users or groups can access a file or directory, and what kind of access they have (read, write, execute, etc.).

Note that this option requires both the source and the destination systems to support ACLs. Also, this option implies the -p option, since ACLs are meaningless without regular permissions.

The -X or —xattrs option

If you want to preserve extended attributes in rsync, you can use the -X or --xattrs option. This option tells rsync to update the destination extended attributes to be the bobe as the source extended attributes.

Extended attributes are a way of storing arbitrary metadata for files and directories. They can be used for various purposes, such as storing SELinux contexts, file capabilities, user-defined tags, etc.

Note that this option requires both the source and the destination systems to support extended attributes. Also, this option does not imply any other options, so you may need to combine it with other options to preserve other attributes.

Examples

Now that we have seen how to preserve permission in rsync using different options, let’s look at some examples of how to use them in practice.

Local to local sync example

In this example, we will sync two local directories using rsync with the -a option. We will also use the -v option for verbose output and the -n option for dry-run mode (to see what would happen without actually making any changes).

Suppose we have a directory called src with some files and subdirectories in it:

$ ls -l src
total 12
drwxr-xr-x 2 bob bob 4096 Jul 28 15:30 dir1
drwxr-xr-x 2 bob bob 4096 Jul 28 15:30 dir2
-rw-rw-r-- 1 bob bob   12 Jul 28 15:30 file1.txt
-rwxrwxr-x 1 bob bob   12 Jul 28 15:30 file2.sh

We also have an empty directory called dest:

$ ls -l dest
total 0

Now we run rsync with the -avn options:

$ rsync -avn src/ dest/
sending incremental file list
created directory dest
./
dir1/
dir2/
file1.txt
file2.sh

sent 238 bytes received 77 bytes 630.00 bytes/sec
total size is 24 speedup is 0.08 (DRY RUN)

The output shows that rsync would create a directory called dest and copy all the files and subdirectories from src into it. Note that we used a trailing slash after src/ to indicate that we want to copy its contents, not the directory itself. If we omit the slash, rsync would create a subdirectory called src inside dest and copy everything there.

To actually perform the sync, we need to remove the -n option:

$ rsync -av src/ dest/
sending incremental file list
created directory dest
./
dir1
dir2
file1.txt
file2.sh

sent 406 bytes received 107 bytes 1,026.00 bytes/sec
total size is 24 speedup is 0.05

The output shows that rsync has created the dest directory and copied all the files and subdirectories from src into it. We can verify that by listing the dest directory:

$ ls -l dest
total 12
drwxr-xr-x 2 bob bob 4096 Jul 28 15:32 dir1
drwxr-xr-x 2 bob bob 4096 Jul 28 15:32 dir2
-rw-rw-r-- 1 bob bob   12 Jul 28 15:30 file1.txt
-rwxrwxr-x 1 bob bob   12 Jul 28 15:30 file2.sh

We can also see that rsync has preserved the permissions, ownership, and modification times of the files and subdirectories, as indicated by the -a option.

Local to remote sync example

In this example, we will sync a local directory to a remote server using rsync with the -a option. We will also use the -z option to compress the data during the transfer, and the -e option to specify the remote shell as ssh.

Suppose we have a local directory called src with some files and subdirectories in it:

$ ls -l src
total 12
drwxr-xr-x 2 bob bob 4096 Jul 28 15:30 dir1
drwxr-xr-x 2 bob bob 4096 Jul 28 15:30 dir2
-rw-rw-r-- 1 bob bob   12 Jul 28 15:30 file1.txt
-rwxrwxr-x 1 bob bob   12 Jul 28 15:30 file2.sh

We also have a remote server with the hostname example.com and the username bob. We have set up SSH keys to access the server without a password.

We want to sync the src directory to a directory called dest on the remote server. We run rsync with the -azve options and specify the source and destination locations:

$ rsync -azve ssh src/ [email protected]:dest/
sending incremental file list
created directory dest
./
dir1/
dir2/
file1.txt
file2.sh

sent 406 bytes received 107 bytes 342.00 bytes/sec total size is 24 speedup is 0.05

The output shows that rsync has created a directory called dest on the remote server and copied all the files and subdirectories from src into it. We can verify that by logging into the remote server and listing the dest directory:

$ ssh [email protected]
[email protected]'s password:
Welcome to Ubuntu!
Last login: Wed Jul 28 15:34:22 UTC from x.x.x.x

$ ls -l dest
total 12
drwxr-xr-x 2 bob bob 4096 Jul 28 15:34 dir1
drwxr-xr-x 2 bob bob 4096 Jul 28 15:34 dir2
-rw-rw-r-- 1 bob bob 12 Jul 28 15:30 file1.txt
-rwxrwxr-x 1 bob bob 12 Jul 28 15:30 file2.sh

We can also see that rsync has preserved the permissions, ownership, and modification times of the files and subdirectories, as indicated by the -a option.

Remote to local sync example

In this example, we will sync a remote directory to a local directory using rsync with the -a option. We will also use the -z option to compress the data during the transfer, and the -e option to specify the remote shell as ssh.

Suppose we have a remote server with the hostname example.com and the username bob. We have set up SSH keys to access the server without a password.

We also have a local directory called dest that is empty:

$ ls -l dest
total

The remote server has a directory called src with some files and subdirectories in it:

$ ssh [email protected]
[email protected]'s password:
Welcome to Ubuntu!
Last login: Wed Jul 28 15:34:22 UTC from x.x.x.x

$ ls -l src
total 12
drwxr-xr-x 2 bob bob 4096 Jul 28 15:34 dir1
drwxr-xr-x 2 bob bob 4096 Jul 28 15:34 dir2
-rw-rw-r-- 1 bob bob 12 Jul 28 15:30 file1.txt
-rwxrwxr-x 1 bob bob 12 Jul 28 15:30 file2.sh

We want to sync the src directory from the remote server to a local directory called dest. We run rsync with the -azve options and specify the source and destination locations:

$ rsync -azve ssh [email protected]:src/ dest/
receiving incremental file list
created directory dest
./
dir1/
dir2/
file1.txt
file2.sh

sent 107 bytes received 406 bytes 342.00 bytes/sec
total size is 24 speedup is 0.05

The output shows that rsync has created a directory called dest on the local system and copied all the files and subdirectories from src on the remote server into it. We can verify that by listing the dest directory:

$ ls -l dest
total 12
drwxr-xr-x 2 bob bob 4096 Jul 28 15:36 dir1
drwxr-xr-x 2 bob bob 4096 Jul 28 15:36 dir2
-rw-rw-r-- 1 bob bob   12 Jul 28 15:30 file1.txt
-rwxrwxr-x 1 bob bob   12 Jul 28 15:30 file2.sh

We can also see that rsync has preserved the permissions, ownership, and modification times of the files and subdirectories, as indicated by the -a option.

Conclusion

We have seen how to use various options to preserve different file attributes, such as permissions, ownership, groups, links, devices, ACLs, and extended attributes. We have also seen some examples of how to use rsync to sync files and directories between local and remote locations.

Rsync is a very useful tool for syncing data across different systems. It can save you time and bandwidth by only transferring the changes between the source and the destination. It can also help you maintain the security and functionality of your files by preserving their attributes.

If you want to learn more about rsync, you can check out its official website or its man page. We hope you have enjoyed this article and found it helpful. Thank you for reading!