# Cheat Sheet #day36 - rsync

## rsync Cheatsheet

### Basic Syntax

```bash
rsync [options] source destination
```

### Common Options

* `-a` : Archive mode (recursive, preserve permissions, timestamps, symlinks, etc.)
    
* `-v` : Verbose output
    
* `-z` : Compress file data during the transfer
    
* `-r` : Recursive mode (copy directories recursively)
    
* `-u` : Skip files that are newer on the receiver
    
* `-h` : Human-readable numbers
    
* `--progress` : Show progress during transfer
    
* `--delete` : Delete files in the destination that are not in the source
    
* `-e` : Specify remote shell program (e.g., `-e ssh`)
    

### Examples

#### Basic Local Copy

```bash
rsync -avh source/ destination/
```

* Copy all files and directories from `source` to `destination` preserving permissions, timestamps, and symlinks.
    

#### Copy to Remote Server

```bash
rsync -avzhe ssh source/ user@remote_host:/path/to/destination/
```

* Copy files to a remote server using SSH.
    

#### Copy from Remote Server

```bash
rsync -avzhe ssh user@remote_host:/path/to/source/ destination/
```

* Copy files from a remote server using SSH.
    

#### Show Progress During Transfer

```bash
rsync -avh --progress source/ destination/
```

* Show progress of each file transfer.
    

#### Sync Directories (Bidirectional)

```bash
rsync -avzhe ssh --delete source/ user@remote_host:/path/to/destination/
```

* Sync directories and delete files in the destination that are not in the source.
    

#### Exclude Files or Directories

```bash
rsync -avh --exclude='*.log' --exclude='temp/' source/ destination/
```

* Exclude files matching `*.log` and the directory `temp/`.
    

#### Dry Run (Preview Changes)

```bash
rsync -avh --dry-run source/ destination/
```

* Show what would have been transferred without actually doing it.
    

#### Limit Bandwidth Usage

```bash
rsync -avh --bwlimit=1000 source/ destination/
```

* Limit bandwidth usage to 1000 KB/s.
    

#### Using a Non-Standard SSH Port

```bash
rsync -avzhe 'ssh -p 2222' source/ user@remote_host:/path/to/destination/
```

* Use a non-standard SSH port (e.g., 2222).
    

#### Preserve Hard Links

```bash
rsync -avH source/ destination/
```

* Preserve hard links in the transfer.
    

#### Copy Only Modified Files

```bash
rsync -avh --ignore-existing source/ destination/
```

* Copy only files that don’t exist at the destination.
    

#### Syncing with Checksum

```bash
rsync -avc source/ destination/
```

* Use checksum to compare files instead of mod-time and size.
    

### Useful Tips

* **Trailing Slash on Source**: The presence of a trailing slash on the source directory (`source/`) indicates that the contents of the directory should be copied, but not the directory itself. Without the trailing slash (`source`), the entire directory is copied.
    
* **Using Cron for Scheduled Syncs**:
    
    ```bash
    crontab -e
    ```
    
    Add a cron job:
    
    ```bash
    0 2 * * * rsync -avzhe ssh /local/path/ user@remote_host:/remote/path/
    ```
    
    * This will run `rsync` every day at 2 AM.
        
* **Logging**: To log the output of `rsync`, use:
    
    ```bash
    rsync -avh source/ destination/ >> /path/to/logfile.log 2>&1
    ```
    

### Advanced Options

* `--backup` : Make backups of (replaced) destination files.
    
* `--backup-dir` : Make backups into this directory.
    
* `--suffix` : Define a backup suffix (default is `~`).
    
* `--exclude-from` : Read exclude patterns from a file.
    
* `--include` : Include files matching a pattern.
    
* `--include-from` : Read include patterns from a file.
    
* `--chown` : Change the user and/or group of the files to the specified `USER:GROUP`.
    

#### Example with Advanced Options

```bash
rsync -avh --backup --backup-dir=/path/to/backup --suffix=_old --exclude-from='exclude.txt' source/ destination/
```

* Backup replaced files to `/path/to/backup` with a suffix `_old`, and exclude patterns listed in `exclude.txt`.
    

This cheatsheet should help you get started with `rsync` and cover most common scenarios and options you might need. Adjust the commands according to your specific requirements.
