rsync

Table of Contents

1. Description

Some notes about specific things with rsync

2. Incremental backup

rsync <options> --backup-dir="../dir1-$(date -I)" src/dir1/ remote:dest/backup/dir1/

--backup, -b: preexisting destination files are renamed as each file is transferred or deleted

The --backup-dir= option implies the --backup option and tells rsync to store all backups in the specified directory on the receiving side. This can be used for incremental backups

The --backup-dir="../dir1-$(date -I)" option will create the dir1-2023-08-05 directory (next to dir1) and will included the old version of any deleted/modified files, see example below AFTER (on remote)

BEFORE (on remote)                  |  AFTER (on remote)
------------------------------------|---------------------------------------
dest/                               |  dest/
└── backup/                         |  └── backup/
    └── dir1/                       |          ├── dir1/
        ├── file1  <--- modified    |          │   ├── file1  <------ modified
        └── file2                   |          │   └── file2
                                    |          └── dir1-2023-08-05/
                                    |              └── file1  <------ old

3. Trailing slash

3.1. Source

Source directory dir1/ example

dir1/
├── another-dir/
│   ├── file6
│   └── file7
├── file1
├── file2
├── file3
├── file4
└── file5

If dir1/ path ends in a /, the contents of the directory are copied inside backup/ rather than the directory itself

# SCENARIO 1
rsync <options> src/dir1/ remote:dest/backup/

# SCENARIO 2
rsync <options> src/dir1 remote:dest/backup/

Result on the remote backup/ directory

SCENARIO 1 (TRAILING SLASH ON SRC)  |  SCENARIO 2 (NO TRAILING SLASH ON SRC)
------------------------------------|---------------------------------------
dest/                               |  dest/
└── backup/                         |  └── backup/
    ├── another-dir/  --,           |      └── dir1/
    │   ├── file6       |           |          ├── another-dir/
    │   └── file7       | not       |          │   ├── file6
    ├── file1           | inside    |          │   └── file7
    ├── file2           | dir1/     |          ├── file1
    ├── file3           |           |          ├── file2
    ├── file4           |           |          ├── file3
    └── file5         --'           |          ├── file4
                                    |          └── file5

One solution for SCENARIO 1

rsync <options> src/dir1/ remote:dest/backup/dir1/

3.2. Destination

From rsync(1) section COPYING TO A DIFFERENT NAME

The single-item copy rule might accidentally bite you if you unknowingly copy a single item and specify a destination dir that doesn’t exist (without using a trailing slash). For example, if src/*.c matches one file and save/dir doesn’t exist, this will confuse you by naming the destination file save/dir:

rsync -ai src/*.c save/dir

To prevent such an accident, either make sure the destination dir exists or specify the destination path with a trailing slash:

rsync -ai src/*.c save/dir/

4. Examples

4.1. Download framboise home directory and delete extraneous files on receiving side

rsync -avh --delete --dry-run --info=stats2 'framboise:~/' ~/backup/framboise/

5. References