Posted in

Linux:使用远程同步工具 Rsync 同步本地和远程目录_AI阅读总结 — 包阅AI

包阅导读总结

1. 关键词:Linux、Rsync、同步目录、备份、自动化

2. 总结:本文介绍了在 Linux 中使用 Rsync 工具同步本地和远程目录的方法,包括安装、配置目的地、测试连接、自动化同步以及双向同步,适用于多服务器管理和数据共享场景。

3. 主要内容:

– 介绍 Rsync 工具及其用途

– 准备工作

– 需要两台 Linux 机器和有 sudo 权限的用户

– 机器配置静态 IP 地址

– 安装 Rsync

– 在 Debian 和 Fedora 上的安装命令

– 启动并启用 Rsync

– 配置目的地

– 在目的地服务器创建目录和配置文件

– 配置文件内容及参数解释

– 测试连接

– 测试命令及参数解释

– 自动化 Rsync

– 创建用于 cron 执行的 bash 脚本

– 设置 cronjob 定时执行同步

– 双向同步

– 双向同步的命令及在脚本中的应用

思维导图:

文章地址:https://thenewstack.io/linux-synchronize-local-and-remote-directories-with-rsync/

文章来源:thenewstack.io

作者:Jack Wallen

发布时间:2024/7/9 16:22

语言:英文

总字数:1073字

预计阅读时间:5分钟

评分:84分

标签:Linux,Rsync,同步,备份,自动化


以下为原文内容

本内容来源于用户推荐转载,旨在分享知识与观点,如有侵权请联系删除 联系邮箱 media@ilingban.com

If you manage multiple Linux servers, you might find yourself in a situation where you have two machines with directories that need to remain in sync with one another. This can be used as a backup solution or maybe you have different servers assigned to different users but have that one directory housing data for which all users need to have access.

There are several use cases for such a setup and one tool to make setting it up quite simple. That tool is rsync.

Rsync stands for remote sync and has been around for a long, long time. I’ve used rsync as a reliable (scriptable) backup solution on Linux but have also found instances where it was necessary that local and remote directories are always in sync with one another.

Nearly every Linux distribution ships with rsync, so there’s nothing to install; it’s just a matter of configuring everything. Of course, if you find yourself with a distribution that doesn’t include rsync, I’ll show you how to take care of that.

Let’s get those directories in sync.

What You’ll Need

To make this work, you’ll need two Linux machines and a user with sudo privileges. Both machines should be configured with static IP addresses, so you don’t have to worry about either address changing, which would cause the sync to fail.

With that in mind, let’s get to work.

Installing Rsync

Just in case you run into a situation where rsync isn’t installed, here’s how you can do it on both Debian and Fedora-based distributions:

  • Debian – sudo apt-get install rsync -y
  • Fedora – sudo dnf install rsync -y

Once rsync is installed, make sure it’s started and enabled with the command:

sudo systemctl enable now rsync

Rsync is now ready for configuration.

Configuring the Destination

Let’s say we have two different Linux servers:

  • 192.168.1.115 — source
  • 192.168.1.116 — destination

The first thing we’ll do is create a directory that will house the synced data. Let’s do that on the destination server with the command:

Still, on the destination server, we need to create an rsync configuration file. Create a new file to house the configuration with the command:

sudo nano /etc/rsyncd.conf

In that file, paste the following content:

Where DIRECTORY is our newly created/data directory (or whatever you named it) and SOURCE_IP is the IP address of the source machine.

Let’s break this down.

  • [backup] – this is the title of the block that will be used when syncing from the source.
  • path
  • path – the full path to the directory that will house the synced data.
  • hosts allow – the IP address of the source machine.
  • hosts deny – this instructs rsync that all other IP addresses are to be denied.
  • list – determines if the module is listed when a client requests.
  • uid – the user ID which can be set to root, nobody, or a Linux user.
  • gid – the group ID
  • read only – defines if clients can upload files or not.

Make sure to change DIRECTORY and SOURCE_IP. Once you’ve done that, save and close the file.

Test the Connection

What we’re going to do now is test to make sure rsync works as expected. On the source machine (the machine housing the directory that will be synced with /data on the destination server), run the command:

rsync avz DIRECTORY DESTINATION_IP::backup

Let’s break that command down:

  • rsync – the main command.
  • -avz – options for archive, verbose output, and compress.
  • DIRECTORY – the directory on the source machine that is to be synced with the destination directory.
  • DESTINATION_IP – the IP address of the destination machine.
  • backup – the name of the job listed in the rsyncd.conf file.

After running this command, everything in the source directory should now be found in the destination directory. If that’s the case, you’ve successfully configured rsync. The only issue at the moment is that it’s currently capable of syncing manually. Let’s automate that process.

Automating Rsync

Linux has a very powerful tool for automation called cron. With cron, you can set up jobs that run at just about any interval you need. Let’s say you want those folders to be synced every five minutes. The first thing we must do is create a bash script that will be run by cron.

Create the bash script with the command:

In that file, add the following:

#! /bin/bash

rsync avz DIRECTORY DESTINATION_IP::backup

Where DIRECTORY to the full path of the directory and DESTINATION_IP is the IP address for the remote machine.

Once you’ve taken care of that, give the file executable permission with:

Now, we create our cronjob. Open the cronjob editor with:

At the bottom of the file, add the following:

Make sure to change /path/to/rsync.sh to the exact path of the rsync.sh bash script. The > /dev/null 2>&1 section silences the output of the rsync command, otherwise, it would error out when the cronjob attempts to run.

Save and close the file.

The rysnc.sh script will now start running every five minutes, to ensure the destination directory is in sync with the source directory.

Two-Way Syncing

Let’s say you need both source and destination to be in sync with one another. To do that, it’s essentially the same, the only difference is you have to run rsync twice (which can be added to the rsync.sh script). We’ll do that with the -au options like so:

  • Source to destination – rsync -avz DIRECTORY DESTINATION_IP::backup
  • Destination to source – rsync -avz DESTINATION_IP::backup DIRECTORY

For this, our rsync.sh script will be:

Where DIRECTORY to the full path of the directory and DESTINATION_IP is the IP address for the remote machine.

Now, both local and remote directories will be in sync every five minutes (or whatever interval you’ve set for your cron job).

And that’s how you can use rsync to synchronize two Linux directories on your local network. Yes, there are easier ways to do this, but using rsync and cron is a quick and flexible method that will never fail you.

YOUTUBE.COM/THENEWSTACK

Tech moves fast, don’t miss an episode. Subscribe to our YouTubechannel to stream all our podcasts, interviews, demos, and more.

GroupCreated with Sketch.