如何在 Linux 中同步本地和远程目录

[ad_1]

本教程描述了如何在 Linux 中使用同步本地和远程目录 同步 (实时同步守护程序)。 使用 Lsyncd,我们可以将本地镜像到本地目录或本地到远程目录。 当您想要在经常使用新内容更新的目录之间同步数据时,它会派上用场。 Lsyncd 旨在将本地目录树与远程镜像的预期更改的低配置文件同步。 将数据从安全区域同步到不太安全的区域特别有用。

内容

  1. 在 Linux 中安装 Lsyncd
  2. Debian 和 Ubuntu 及其衍生产品上的 Lsyncd 配置
  3. RHEL 和 CentOS 上的 Lsyncd 配置
  4. 使用 Lsyncd 在 Linux 中同步本地和远程目录
    1. 1.在Debian、Ubuntu上同步本地目录
    2. 2.在RHEL、CentOS上同步本地目录
    3. 3.在Debian、Ubuntu系统上同步远程目录
      1. 帮助我们帮助您:

在 Linux 中安装 Lsyncd

在 Debian 及其衍生产品(如 Ubuntu)上,Linux Mint 运行以下命令来安装 Lsyncd:

$ sudo apt-get install lsyncd

在 RHEL、CentOS 上,您需要先启用 EPEL 存储库。

$ sudo yum install epel-release

然后,使用命令安装 Lsyncd:

$ sudo yum install lsyncd

Debian 和 Ubuntu 及其衍生产品上的 Lsyncd 配置

Lsyncd 在基于 DEB 的系统中不提供任何默认配置文件。 我们需要手动创建它。 但是,Lsyncd 包含一些示例配置文件,这些文件有助于基本了解同步的内容/方式。 示例配置文件位于 “/usr/share/doc/lsyncd-*/examples” 目录。

$ ls /usr/share/doc/lsyncd/examples/

样本输出:

lbash.lua lgforce.lua lpostcmd.lua lrsyncssh.lua
lecho.lua limagemagic.lua lrsync.lua

正如您在上面的输出中看到的,每个配置文件都以 .lua 延期。 这是因为 lsync 配置文件是使用 Lua 编程语言. 让我们看一个示例配置文件。

$ cat /usr/share/doc/lsyncd/examples/lrsync.lua

这是 Lrsync 配置文件的样子:

----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync.
--
settings = {
 statusFile = "/tmp/lsyncd.stat",
 statusInterval = 1,
}

sync{
 default.rsync,
 source="src",
 target="trg",
}

RHEL 和 CentOS 上的 Lsyncd 配置

在 RHEL 和 CentOS 系统上,会自动创建 Lsyncd 配置文件。 您可以使用以下命令查看此文件的内容:

$ cat /etc/lsyncd.conf

并且,示例配置文件位于以下位置。

$ ls /usr/share/doc/lsyncd-2.1.5/examples/

使用 Lsyncd 在 Linux 中同步本地和远程目录

如前所述,Lyncd 可以将两个本地目录和一个本地目录的内容同步到远程目录。 首先,我们将了解如何同步本地目录。

1.在Debian、Ubuntu上同步本地目录

现在,我们将同步两个本地目录。

让我们创建两个包含一些示例内容的目录。

$ sudo mkdir source_dir
$ sudo mkdir dest_dir

在 source_dir 目录中创建一些随机文件:

$ sudo touch source_dir/file{1..10}

或者,您可以为 Lysyncd 创建一些日志文件。 这只是可选的。 如果您不想要日志文件,请跳过此步骤。

$ sudo mkdir /var/log/lsyncd
$ sudo touch /var/log/lsyncd/lsyncd.{log,status}

接下来,为 Lsyncd 创建配置文件:

$ sudo mkdir /etc/lsyncd
$ sudo nano /etc/lsyncd/lsyncd.conf.lua

编辑/修改以下行:

settings {
        logfile = "/var/log/lsyncd/lsyncd.log",
        statusFile = "/var/log/lsyncd/lsyncd.status"
}

sync {
        default.rsync,
        source = "/home/sk/source_dir",
        target = "/home/sk/dest_dir",
}

用您自己的值替换源目录和目标目录路径。 Save 和 close 文件。

使用命令启用并启动或重新启动 Lsyncd 服务:

$ sudo systemctl enable lsyncd
$ sudo systemctl restart lsyncd

现在,检查 source_dir 和 dest_dir 目录的内容。

$ ls source_dir/

样本输出:

file1 file10 file2 file3 file4 file5 file6 file7 file8 file9

检查 dest_dir 内容:

$ ls dest_dir/

样本输出:

file1 file10 file2 file3 file4 file5 file6 file7 file8 file9

看? 我们仅在 source_dir 目录中创建了文件,并且该目录的内容已自动同步到 dest_dir 目录。

此外,您可以查看日志文件以验证复制是否完成。

$ tail -10 /var/log/lsyncd/lsyncd.log

样本输出:

Sat Apr 15 17:07:34 2017 Normal: recursive startup rsync: /home/sk/source_dir/ -> /home/sk/dest_dir/
Sat Apr 15 17:07:34 2017 Normal: Startup of "/home/sk/source_dir/" finished.

检查 Lsyncd 状态文件:

$ more /var/log/lsyncd/lsyncd.status

样本输出:

Lsyncd status report at Sat Apr 15 17:07:44 2017

Sync1 source=/home/sk/source_dir/
There are 0 delays
Excluding:
 nothing.


Inotify watching 1 directories
 1: /home/sk/source_dir/

下载免费电子书:“GNU/Linux 高级管理”

2.在RHEL、CentOS上同步本地目录

编辑 Lsyncd 配置文件:

$ sudo nano /etc/lsyncd.conf

编辑和修改以下行:

----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync, but executing moves through on the target.
--
-- For more examples, see /usr/share/doc/lsyncd*/examples/
-- 
settings {
 logfile = "/var/log/lsyncd.log",
 statusFile = "/var/log/lsyncd.status"
}

sync {
 default.rsync,
 source = "/root/source_dir",
 target = "/root/dest_dir",

用您自己的值替换源目录和目标目录路径。 Save 和 close 文件。

使用命令启用并启动 Lsyncd 服务:

$ sudo systemctl enable lsyncd
$ sudo systemctl start lsyncd

现在,检查 source_dir 和 dest_dir 目录的内容。

$ ls source_dir/

样本输出:

file1 file10 file2 file3 file4 file5 file6 file7 file8 file9

检查 dest_dir 内容:

$ ls dest_dir/

样本输出:

file1 file10 file2 file3 file4 file5 file6 file7 file8 file9

恭喜! source_dir 的内容已成功同步到 dest_dir 目录。

此外,您可以查看日志文件以验证复制是否完成。

$ tail -10 /var/log/lsyncd.log

检查 Lsyncd 状态文件:

$ more /var/log/lsyncd.status

3.在Debian、Ubuntu系统上同步远程目录

我们要 设置无密码 SSH 登录。 这是因为 Lsyncd 会自动将本地目录的内容复制到远程目录,而无需用户干预。

$ sudo su
# ssh-keygen -t rsa

不要输入任何密码。 只需按 ENTER 即可接受默认值。

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/home/sk/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:dHc2aKk5F2wBq7CIc5sVHJwYNjoPdwgxDKb1KuBBkgs [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|o*=.++.. ... |
|E..*.+o. o + |
|+o+ + = . o O + |
|+ .B o = o * + . |
|..+ + o S + . |
| . o + o |
| o |
| |
| |
+----[SHA256]-----+

现在,将 SSH 公钥复制到远程系统:

# ssh-copy-id [email protected]

样本输出:

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.43.150 (192.168.43.150)' can't be established.
ECDSA key fingerprint is SHA256:U7I0O1OOzzbHFlhIG0HoGDr1usHzLBju6Jmr6bUB9Es.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password: 

Number of key(s) added: 1

Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.

这里,192.168.43.150 是远程系统的 IP 地址。

现在,您无需输入密码即可通过 SSH 连接到远程系统:

# ssh [email protected]

使用命令在远程系统中创建目标目录:

# mkdir remotesync

从远程系统注销:

# exit

接下来,在源机器中编辑 Lsyncd 配置文件:

$ sudo nano /etc/lsyncd/lsyncd.conf.lua

编辑/修改以下行:

settings {
 logfile = "/var/log/lsyncd/lsyncd.log",
 statusFile = "/var/log/lsyncd/lsyncd.status"
}

sync {
 default.rsync,
 source = "/home/sk/source_dir",
 host = "192.168.43.150",
 targetdir = "/root/remotesync",
}

这里,192.168.43.150 是我的远程主机的 IP 地址。 而且,/root/remotesync/ 是我的远程系统中的目标目录。 用您自己的值替换这些值。 Save 和 close 文件。

重新启动 Lsyncd 服务以将源目录的内容复制到目标。

$ sudo systemctl restart lsyncd

现在,通过 SSH 连接到您的远程系统:

$ ssh [email protected]

并且,检查目标目录的内容(即 /root/remotesync/ 在我们的例子中)。 您将看到本地系统中源目录中的所有文件都已复制到目标系统中的目标目录。

[email protected] ~]# ls remotesync/
file1 file10 file2 file3 file4 file5 file6 file7 file8 file9

此过程对于 RHEL 和 CentOS 系统是相同的。 设置无密码 SSH 登录并确保您在 RHEL 或 CentOS 系统中的 Lsyncd 主配置文件 (/etc/lsyncd.conf) 中修改了正确的源和目标路径,其余步骤相同。

下载免费指南:“恢复 Linux 系统的 10 种简单方法”

您知道如何使用 Lsyncd 在 Linux 中同步本地和远程目录。 如您所见,这是一种非常简单直接的方法。

希望这可以帮助。

资源:

  • Lsyncd GitHub 页面

感谢您的光临!

帮助我们帮助您:

  • 订阅我们的电子邮件通讯: 立即注册
  • 支持 OSTechNix: 通过贝宝捐款
  • 下载免费的电子书和视频: TradePub 上的 OSTechNix
  • 联系我们: Facebook | 推特 | 谷歌加 | 领英 | RSS订阅

祝你有美好的一天!!

命令行工具LinuxLsyncdRsync在Linux中同步本地和远程目录

[ad_2]

Related Posts