如何使用 Ansible 的 synchronize 模块和 rsync 工具自动化备份 “examples” 目录,同时最小化网络使用?以下是基于 Linux 的实现方法。
Ansible 使用 rsync 进行备份
模块名称:ansible.posix.synchronize
功能:synchronize 是一个围绕 rsync 的封装工具,用于简化 Ansible 剧本中的常见任务。
rsync 是一种高效的文件传输和同步工具,通过比较文件的修改时间和大小来同步文件,广泛用于本地和远程主机之间的数据传输。
rsync 需要在本地和远程主机上都安装。
目前,synchronize 模块仅支持以下连接类型:ssh、paramiko、local 和 docker。
主要参数
src(字符串):源路径,可以是绝对路径或相对路径。dest(字符串):目标路径,可以是绝对路径或相对路径。archive(布尔值):默认启用,等同于rsync的归档模式(-a),包括递归、链接、权限、时间戳、所有者、组等标志。rsync_opts(字符串):用于指定额外的rsync参数。
src 和 dest 是必需参数,分别指定源主机上的路径和目标主机上的路径。路径可以是本地路径或远程路径,具体取决于需求。
示例:使用 Ansible 和 rsync 进行备份
Playbook 文件:rsync_backup.yml
---
- name: synchronize module demo
hosts: all
become: false
vars:
source: "~/prj/github/ansible-pilot/copy files to remote hosts/examples" # 源目录
destination: "example-backup" # 备份目标目录
tasks:
- name: 确保 rsync 已安装
ansible.builtin.package:
name: rsync
state: present
become: true
- name: 数据同步
ansible.posix.synchronize:
src: "{{ source }}"
dest: "{{ destination }}"
执行命令
$ ansible-playbook -i inventory rsync_backup.yml
执行结果
PLAY [synchronize module demo] ****************************************************
TASK [Gathering Facts] *****************************************************
ok: [demo.example.com]
TASK [确保 rsync 已安装] *****************************************************
ok: [demo.example.com]
TASK [数据同步] *************************************************************
changed: [demo.example.com]
PLAY RECAP *********************************************************************
demo.example.com : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
执行前后对比
执行前:
$ ls -al copy\ files\ to\ remote\ hosts/examples
total 16
drwxr-xr-x 4 lberton staff 128 Jan 17 11:54 .
drwxr-xr-x 9 lberton staff 288 Mar 28 14:13 ..
-rw-r--r-- 1 lberton staff 16 Sep 4 2021 report.txt
-rw-r--r-- 1 lberton staff 16 Jan 17 11:52 report2.txt
$ cat copy\ files\ to\ remote\ hosts/examples/report.txt
test report.txt
$ cat copy\ files\ to\ remote\ hosts/examples/report2.txt
test report.txt
$ ssh devops@demo.example.com
[devops@demo ~]$ ls -al
total 20
drwx------. 4 devops wheel 127 Mar 28 12:22 .
drwxr-xr-x. 4 root root 35 Mar 24 17:44 ..
drwx------. 3 devops wheel 17 Mar 24 17:44 .ansible
-rw-------. 1 devops wheel 178 Mar 28 12:21 .bash_history
-rw-r--r--. 1 devops wheel 18 Jul 26 2021 .bash_logout
-rw-r--r--. 1 devops wheel 141 Jul 26 2021 .bash_profile
-rw-r--r--. 1 devops wheel 376 Jul 26 2021 .bashrc
drwx------. 2 devops wheel 29 Mar 24 17:44 .ssh
-rw-------. 1 devops wheel 1756 Mar 28 10:38 .viminfo
[devops@demo ~]$ rpm -qa | grep rsync
rsync-3.1.3-12.el8.x86_64
[devops@demo ~]$
执行后:
$ ssh devops@demo.example.com
[devops@demo ~]$ ls -al
total 20
drwx------. 5 devops wheel 149 Mar 28 12:24 .
drwxr-xr-x. 4 root root 35 Mar 24 17:44 ..
drwx------. 3 devops wheel 17 Mar 24 17:44 .ansible
-rw-------. 1 devops wheel 211 Mar 28 12:24 .bash_history
-rw-r--r--. 1 devops wheel 18 Jul 26 2021 .bash_logout
-rw-r--r--. 1 devops wheel 141 Jul 26 2021 .bash_profile
-rw-r--r--. 1 devops wheel 376 Jul 26 2021 .bashrc
drwx------. 2 devops wheel 29 Mar 24 17:44 .ssh
-rw-------. 1 devops wheel 1756 Mar 28 10:38 .viminfo
drwxr-xr-x. 3 devops wheel 22 Mar 28 12:24 example-backup
[devops@demo ~]$ tree example-backup/
example-backup/
└── examples
├── report2.txt
└── report.txt
1 directory, 2 files
[devops@demo ~]$ cat example-backup/examples/report.txt
test report.txt
[devops@demo ~]$ cat example-backup/examples/report2.txt
test report.txt
[devops@demo ~]$ ls -al example-backup/examples/
total 8
drwxr-xr-x. 2 devops wheel 43 Jan 17 10:54 .
drwxr-xr-x. 3 devops wheel 22 Mar 28 12:24 ..
-rw-r--r--. 1 devops wheel 16 Sep 4 2021 report.txt
-rw-r--r--. 1 devops wheel 16 Jan 17 10:52 report2.txt
[devops@demo ~]$
源目录更改后的操作
在源目录中添加新文件 report3.txt:
$ touch copy\ files\ to\ remote\ hosts/examples/report3.txt
$ ansible-playbook -i inventory rsync_backup.yml
再次执行后:
$ ssh devops@demo.example.com
[devops@demo ~]$ ls -al example-backup/
total 0
drwxr-xr-x. 3 devops wheel 22 Mar 28 12:24 .
drwx------. 5 devops wheel 149 Mar 28 12:24 ..
drwxr-xr-x. 2 devops wheel 62 Mar 28 12:44 examples
[devops@demo ~]$ ls -al example-backup/examples/
total 8
drwxr-xr-x. 2 devops wheel 62 Mar 28 12:44 .
drwxr-xr-x. 3 devops wheel 22 Mar 28 12:24 ..
-rw-r--r--. 1 devops wheel 16 Sep 4 2021 report.txt
-rw-r--r--. 1 devops wheel 16 Jan 17 10:52 report2.txt
-rw-r--r--. 1 devops wheel 0 Mar 28 18:44 report3.txt
[devops@demo ~]$
注意事项
rsync安装:确保本地和远程主机上都安装了rsync。- 路径问题:
src和dest路径可以是绝对路径或相对路径,具体取决于需求。 - 幂等性:如果目标目录中的文件未发生变化,
synchronize模块不会重复同步,确保操作的幂等性。 - 网络优化:
rsync通过增量同步减少网络传输,非常适合备份任务。
希望这个示例对您有帮助!