Skip to content

Instantly share code, notes, and snippets.

@remoteur
Last active February 8, 2018 23:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remoteur/d81045b0e6098a3076bc617cf4364831 to your computer and use it in GitHub Desktop.
Save remoteur/d81045b0e6098a3076bc617cf4364831 to your computer and use it in GitHub Desktop.
backup-restore
---
- name: register image files
find:
path: /var/lib/libvirt/images/
pattern: "{{ vm_name }}.*qcow2$"
use_regex: yes
register: vm_images
- command: "cp --sparse=always {{ item.path }} {{ item.path }}.{{ restore_point }}"
with_items: "{{ vm_images.files }}"
---
- name: register image files
find:
path: /var/lib/libvirt/images/
pattern: "{{ vm_name }}.*qcow2$"
use_regex: yes
register: vm_images
- command: "cp --sparse=always {{ item.path }}.{{ restore_point }} {{ item.path }}"
with_items: "{{ vm_images.files }}"
- name: vms backup/restore actions
hosts: localhost
gather_facts: false
vars:
action: noop
restore_point: orig
backup_items: '.'
tasks:
- name: register vms
shell: |
virsh list --all | grep -v Name | awk {'print $2'} | grep -v ^$ | grep {{ backup_items }}
register: vms
- block:
- name: destroy vms
command: virsh destroy {{ item }}
with_items: "{{ vms.stdout_lines }}"
- include: disks_backup.yaml
with_items: "{{ vms.stdout_lines }}"
loop_control:
loop_var: vm_name
- name: power on vms
command: virsh start {{ item }}
with_items: "{{ vms.stdout_lines }}"
when: action == 'backup'
- block:
- name: destroy vms
command: virsh destroy {{ item }}
with_items: "{{ vms.stdout_lines }}"
- include: disks_restore.yaml
with_items: "{{ vms.stdout_lines }}"
loop_control:
loop_var: vm_name
- name: power on vms
command: virsh start {{ item }}
with_items: "{{ vms.stdout_lines }}"
when: action == 'restore'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment