`rsync` is a powerful and widely-used command-line tool for copying and synchronizing files and directories between different locations. It's particularly popular for its ability to efficiently transfer data, especially over network connections. `rsync` stands for "remote synchronization".
### Key Features of rsync:
1. **Efficient Data Transfer:**
- `rsync` uses a delta-transfer algorithm, which only sends the differences between the source and destination files, reducing the amount of data transferred.
- It can resume interrupted transfers, saving time and bandwidth.
2. **Synchronization:**
- `rsync` can synchronize the contents of two directories, ensuring that both locations have the same set of files and directories.
- It can delete files from the destination that have been removed from the source, keeping the two locations in sync.
3. **Remote and Local Copying:**
- `rsync` can work both locally (between directories on the same system) and remotely (between different systems over a network).
- When used for remote copying, `rsync` can securely transfer files using SSH.
4. **Preservation of File Metadata:**
- `rsync` can preserve various file attributes such as permissions, ownership, timestamps, and symbolic links.
5. **Bandwidth Limiting and Compression:**
- `rsync` allows you to limit the bandwidth used during transfers to prevent network congestion.
- It can compress data during transfer to reduce the amount of data sent over the network.
### Basic Usage of rsync:
The basic syntax of `rsync` is:
```sh
rsync [options] source destination
```
- `source`: The source directory or file you want to copy.
- `destination`: The destination directory where you want to copy the files.
### Example Usage:
1. **Local Copy:**
```sh
rsync -avh /path/to/source /path/to/destination
```
2. **Remote Copy over SSH:**
```sh
rsync -avh -e ssh user@remote_host:/path/to/source /path/to/destination
```
3. **Synchronize Directories:**
```sh
rsync -avh --delete /path/to/source/ /path/to/destination/
```
- The `--delete` option ensures that any files in the destination not present in the source are deleted.
### Conclusion:
`rsync` is a versatile and efficient tool for copying and synchronizing files and directories, making it invaluable for tasks such as backups, mirroring, and remote file transfers. Its ability to handle complex synchronization tasks while minimizing data transfer makes it a popular choice for system administrators and users alike.
Automate the rsync
To achieve automatic synchronization whenever a file is added to the source directory, you can use `rsync` in conjunction with `inotifywait`, a Linux command-line tool that waits for changes to files or directories and then executes a command when an event occurs. Here's how you can set it up:
### 1. Install `inotify-tools`
First, ensure that `inotify-tools` is installed on your system. You can install it using your package manager. For Ubuntu, you can use:
```bash
sudo apt-get install inotify-tools
```
### 2. Create a Bash Script
Create a Bash script that listens for file changes in the source directory and triggers `rsync` when a file is added. Here's an example:
```bash
#!/bin/bash
# Source and destination directories
source_dir="/path/to/source"
destination_dir="/path/to/destination"
# Run rsync command when a file is added to the source directory
inotifywait -m -e create --format '%f' "$source_dir" | while read FILE
do
rsync -avh "$source_dir/$FILE" "$destination_dir"
done
```
Save this script in a file, for example, `sync_on_add.sh`, and make it executable:
```bash
chmod +x sync_on_add.sh
```
### 3. Run the Script
Run the script in the background:
```bash
./sync_on_add.sh &
```
### How It Works
- The script continuously monitors the source directory (`/path/to/source`) for the creation of new files (`-e create`).
- When a new file is created, `inotifywait` outputs the filename.
- The `while` loop reads the filename and triggers `rsync` to synchronize the new file to the destination directory (`/path/to/destination`).
### Notes:
- Ensure that the script is running continuously in the background to monitor changes (`&` at the end of the command).
- Adjust the source and destination directories in the script to match your setup.
- This setup will only sync files when they are added to the source directory. If you want to sync on other events (e.g., modification or deletion), you can adjust the `inotifywait` options accordingly (`-e modify`, `-e delete`, etc.).
With this setup, `rsync` will automatically synchronize files to the destination directory whenever a new file is added to the source directory.
Comments
Post a Comment