首页 > 解决方案 > 恢复使用 Copy 创建的备份文件

问题描述

如果使用 Copy(backup=yes)创建备份文件,例如,使用此任务:

 - name: copy file
      copy: 
        dest: path/to/dest
        src: path/to/src
        backup: yes

如果文件path/to/dest已经存在,它将被移动到一个看起来像的文件中path/to/dest.12345.2006-07-08@09:10:11

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

标签: ansible

解决方案


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

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

- debug: var=copy_file.backup_file

- name: restore backup
  copy:
     dest: path/to/dest
     src: copy_file.backup_file
     remote_src: true
  when: copy_file.changed and some condition of yours

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


推荐阅读