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
有没有办法恢复它,或者获取备份文件的文件名以便恢复它?
@guido 在他的回答中演示了如何使用模块返回的backup_file
属性copy
。正如我在不同评论中指出的,如果满足以下条件,这真的很方便:
如果您没有该信息或不想管理该信息,可以使用以下另一种解决方案。为了清楚起见,我以您想要恢复磁盘上可用的最新备份文件为例。您可以完美地调整它以满足任何其他特定要求。
\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这使:
\nPLAY [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
,并在需要时演示该文件与最新备份具有相同的内容。
备份文件的绝对文件名(如果生成,因此如果“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)
归档时间: |
|
查看次数: |
6554 次 |
最近记录: |