Skip to content

Commit 44525d4

Browse files
committed
wip
1 parent 3285239 commit 44525d4

File tree

4 files changed

+50
-32
lines changed

4 files changed

+50
-32
lines changed
Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,14 @@
1-
Role Name
2-
=========
1+
# Backup Qemu VMs
32

4-
A brief description of the role goes here.
3+
Backup all qemu virtual machines.
54

6-
Requirements
7-
------------
5+
## Variables
86

9-
Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required.
7+
The following variable is defined in `defaults/main.yml`:
108

11-
Role Variables
12-
--------------
9+
- backup_retention: `3` (numbers of backups to keep).
1310

14-
A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well.
11+
The following variables are defined in `vars/main.yml`:
1512

16-
Dependencies
17-
------------
18-
19-
A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles.
20-
21-
Example Playbook
22-
----------------
23-
24-
Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too:
25-
26-
- hosts: servers
27-
roles:
28-
- { role: username.rolename, x: 42 }
29-
30-
License
31-
-------
32-
33-
BSD
34-
35-
Author Information
36-
------------------
37-
38-
An optional section for the role authors to include contact information, or a website (HTML is not allowed).
13+
- source_dir: `/data/qemu/vms/` (directory containing the qemu vms to backup).
14+
- backup_base_dir: `/backup/qemu/` (base directory to store backups).
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
#SPDX-License-Identifier: MIT-0
22
---
33
# defaults file for backup_qemu_vms
4+
5+
backup_retention: 3
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,40 @@
11
#SPDX-License-Identifier: MIT-0
22
---
33
# tasks file for backup_qemu_vms
4+
5+
- name: "Create backup directory"
6+
ansible.builtin.file:
7+
path: "{{ backup_base_dir }}{{ ansible_facts['date_time']['date'] }}"
8+
state: directory
9+
mode: "0750"
10+
11+
- name: "Backup qemu vms via RSYNC" # noqa: command-instead-of-module
12+
ansible.builtin.shell:
13+
cmd: rsync -aAXHv --numeric-ids --delete "{{ source_dir }}" "{{ ansible_facts['date_time']['date'] }}"
14+
changed_when: true
15+
args:
16+
chdir: "{{ backup_base_dir }}"
17+
18+
- name: "Get backup directories list"
19+
ansible.builtin.find:
20+
paths: "{{ backup_base_dir }}"
21+
file_type: directory
22+
register: list_backup_dirs
23+
24+
- name: "Delete old backup directories"
25+
ansible.builtin.file:
26+
path: "{{ item.path }}"
27+
state: absent
28+
loop: "{{ (list_backup_dirs.files | sort(attribute='ctime'))[:-backup_retention] }}"
29+
when: list_backup_dirs.files | length > backup_retention
30+
31+
- name: "List backup directories"
32+
ansible.builtin.shell:
33+
cmd: ls -ltrh "{{ backup_base_dir }}"
34+
register: ls_backup_dirs
35+
changed_when: false
36+
37+
- name: "Show backup directories"
38+
ansible.builtin.debug:
39+
msg: "{{ ls_backup_dirs.stdout_lines }}"
40+
when: ls_backup_dirs.stdout_lines | length > 0
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
#SPDX-License-Identifier: MIT-0
22
---
33
# vars file for backup_qemu_vms
4+
5+
source_dir: "/data/qemu/vms"
6+
backup_base_dir: "/backup/qemu/"

0 commit comments

Comments
 (0)