恢复使用副本创建的备份文件

Nan*_*ank 7 ansible

copy如果使用(with )创建备份文件backup: yes,例如,通过以下任务:

- name: copy file
  copy: 
    dest: path/to/dest
    src: path/to/src
    backup: yes
Run Code Online (Sandbox Code Playgroud)

如果文件path/to/dest已经存在,它将被移动到如下所示的文件中path/to/dest.12345.2006-07-08@09:10:11

有没有办法恢复它,或者获取备份文件的文件名以便恢复它?

Zei*_*tor 5

@guido 在他的回答中演示了如何使用模块返回的backup_file属性copy。正如我在不同评论中指出的,如果满足以下条件,这真的很方便:

\n
    \n
  • 您想要在同一次运行 ansible 期间恢复该文件
  • \n
  • 您可以在一次运行中复制时将值存储在某处(例如内存缓存中的变量、数据库条目、磁盘上的文件...),并在稍后的运行中检索它以执行恢复操作。
  • \n
\n

如果您没有该信息或不想管理该信息,可以使用以下另一种解决方案。为了清楚起见,我以您想要恢复磁盘上可用的最新备份文件为例。您可以完美地调整它以满足任何其他特定要求。

\n

基本工作流程:

\n
    \n
  • 使用该模块列出目标磁盘上可用的所有备份文件find
  • \n
  • 选择最近的一个。
  • \n
  • 将该文件复制回原位。
  • \n
\n

请注意,虽然我的演示剧本以本地主机为目标,但只要该文件具有备份候选,它就可以与任何目标一起使用。我将让您根据自己的要求以自己的方式强化这一点。

\n

在运行我的剧本之前,我创建了一个文件并复制了几次,每次都会使用随机内容创建备份。我使用以下剧本来做到这一点:

\n
---\n- name: Create some file with backup\n  hosts: localhost\n  gather_facts: false\n\n  vars:\n    my_file: /tmp/test_restore/toto.txt\n\n  tasks:\n    - name: copy file\n      ansible.builtin.copy:\n        dest: "{{ my_file }}"\n        content: "{{ 1000 | random }}"\n        backup: true\n
Run Code Online (Sandbox Code Playgroud)\n

生成的文件夹内容如下:

\n
/tmp/test_restore/\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 toto.txt\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 toto.txt.14644.2020-12-19@10:39:43~\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 toto.txt.14752.2020-12-19@10:39:45~\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 toto.txt.14861.2020-12-19@10:39:48~\n
Run Code Online (Sandbox Code Playgroud)\n

现在是恢复手册:

\n
/tmp/test_restore/\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 toto.txt\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 toto.txt.14644.2020-12-19@10:39:43~\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 toto.txt.14752.2020-12-19@10:39:45~\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 toto.txt.14861.2020-12-19@10:39:48~\n
Run Code Online (Sandbox Code Playgroud)\n

这使:

\n
PLAY [Restore latest file backup] ******************************************************************************************************************************************************************************************************\n\nTASK [Find all backups for /tmp/test_restore/toto.txt] *********************************************************************************************************************************************************************************\nok: [localhost]\n\nTASK [Select the latest backup found on disk] ******************************************************************************************************************************************************************************************\nok: [localhost]\n\nTASK [Show the latest selected backup] *************************************************************************************************************************************************************************************************\nok: [localhost] => {\n    "latest_backup": "/tmp/test_restore/toto.txt.14861.2020-12-19@10:39:48~"\n}\n\nTASK [Restore latest backup of /tmp/test_restore/toto.txt] *****************************************************************************************************************************************************************************\nchanged: [localhost]\n\nPLAY RECAP *****************************************************************************************************************************************************************************************************************************\nlocalhost                  : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   \n
Run Code Online (Sandbox Code Playgroud)\n

如果您第二次运行该 playbook,则最后一个任务将报告ok而不是changed,并在需要时演示该文件与最新备份具有相同的内容。

\n


gui*_*ido 4

备份文件的绝对文件名(如果生成,因此如果“changed”为 true)在输出对象中返回backup_file,因此(将以下内容作为伪代码,因为我没有测试它):

- name: copy file
  ansible.builtin.copy: 
     dest: path/to/dest
     src: path/to/src
     backup: yes
  register: copy_file

- ansible.builtin.debug: var=copy_file.backup_file

- name: restore backup
  ansible.builtin.copy:
     dest: path/to/dest
     src: copy_file.backup_file
     remote_src: true
  when: copy_file.changed and some condition of yours
Run Code Online (Sandbox Code Playgroud)

请参阅:https ://docs.ansible.com/ansible/latest/collections/ansible/builtin/copy_module.html#return-backup_file