From 99f82917c0aa37476d909423b26571886ec23d25 Mon Sep 17 00:00:00 2001 From: daniel-wtd Date: Mon, 22 Jul 2019 13:49:14 +0200 Subject: [PATCH] rework: release 1.0.0 - update: all the meta files - remove: multi-rsnapshot feature - add: tags - remove: duplicate testing - update: default testing - remove: unused files and features - update: variable names and handling - add: defaults for templates - update: tasks and templates --- .travis.yml | 4 +- README.md | 319 ++++++----- defaults/main.yml | 114 ++-- kudos.txt | 23 +- meta/main.yml | 33 +- molecule/default/Dockerfile.j2 | 3 +- molecule/default/molecule.yml | 6 +- molecule/default/playbook.yml | 21 +- molecule/default/prepare.yml | 1 - molecule/default/tests/test_default.py | 28 +- molecule/multiple/Dockerfile.j2 | 22 - molecule/multiple/molecule.yml | 60 -- molecule/multiple/playbook.yml | 38 -- molecule/multiple/prepare.yml | 41 -- molecule/multiple/tests/test_default.py | 59 -- .../multiple/tests/test_default.py_backup | 99 ---- requirements.yml | 2 - tasks/main.yml | 102 ++-- tasks/notify.yml | 20 - templates/rsnapshot.conf.j2 | 540 ++++-------------- templates/rsnapshot.service.j2 | 13 + templates/rsnapshot.timer.j2 | 9 +- templates/rsnapshot@.service.j2 | 8 - 23 files changed, 508 insertions(+), 1057 deletions(-) delete mode 100644 molecule/multiple/Dockerfile.j2 delete mode 100644 molecule/multiple/molecule.yml delete mode 100644 molecule/multiple/playbook.yml delete mode 100644 molecule/multiple/prepare.yml delete mode 100644 molecule/multiple/tests/test_default.py delete mode 100644 molecule/multiple/tests/test_default.py_backup delete mode 100644 tasks/notify.yml create mode 100644 templates/rsnapshot.service.j2 delete mode 100644 templates/rsnapshot@.service.j2 diff --git a/.travis.yml b/.travis.yml index d0009a3..c2576c8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,8 +18,6 @@ env: # - molecule/defaults/tests/* - name: "centos7" image: "centos:7" - - name: "redhat7" - image: "centos:7" - name: "fedora29" image: "fedora:29" pygroup: "python3" @@ -29,7 +27,7 @@ env: install: # Install test dependencies. - - pip install ansible==2.7.10 molecule[docker] + - pip install ansible==2.7.12 molecule[docker] before_script: # Use actual Ansible Galaxy role name for the project directory. diff --git a/README.md b/README.md index 41d71c8..004da0a 100644 --- a/README.md +++ b/README.md @@ -20,193 +20,218 @@ license: BSD-3-Clause # Ansible Role: srv_rsnapshot -| Install and configure rsnapshot. + +An Ansible Role to install and configure rsanpshot. ## Motivation -Easy, everyone needs backups. :) -And keep in mind: -**No Backup, No Mercy** +Everybody needs a valid backup. As often stated: "no backup, no mercy". + +## Description + +This Ansible Role installs and configures [rsnapshot](https://rsansphot.org). + +- install packages +- configure rsnapshot +- create systemd service +- create systemd timers + +## Requirements + +Used Modules: + +- [Ansible package Module](http://docs.ansible.com/ansible/latest/package_module.html) +- [Ansible template Module](http://docs.ansible.com/ansible/latest/template_module.html) +- [Ansible systemd Module](https://docs.ansible.com/ansible/latest/systemd_module.html) +- [Ansible service Module](https://docs.ansible.com/ansible/latest/service_module.html) ## Installation Install from [Ansible Galaxy](https://galaxy.ansible.com/while_true_do/srv_rsnapshot) - ``` -ansible-galaxy install while_true_do.rsnapshot +ansible-galaxy install while_true_do.srv_rsnapshot ``` -Install from [Github](https://github.com/while-true-do/srv_rsnapshot) - +Install from [Github](https://github.com/while-true-do/ansible-role-srv_rsnapshot) ``` -git clone https://github.com/while-true-do/srv_rsnapshot.git while_true_do.srv_rsnapshot +git clone https://github.com/while-true-do/ansible-role-srv_rsnapshot.git while_true_do.srv_rsnapshot ``` -## Requirements - -Used Modules: - -- [packages](http://docs.ansible.com/ansible/latest/package_module.html) -- [template](http://docs.ansible.com/ansible/latest/template_module.html) -- [systemd](https://docs.ansible.com/ansible/latest/systemd_module.html) -- [shell](https://docs.ansible.com/ansible/latest/shell_module.html) -- [command](https://docs.ansible.com/ansible/latest/command_module.html) -- [file](https://docs.ansible.com/ansible/latest/file_module.html) +Dependencies: -## Dependencies - - +## Usage -## Role Variables +### Role Variables -```yaml +``` +--- # defaults/main.yml for rsnapshot -# You can set the state to ["present"|"absent"|"latest"] -wtd_rsnapshot_state: "present" -wtd_rsnapshot_packages: "rsnapshot" - -wtd_rsnapshot_notify: false - -# -## Default Values -## can be overwrite -## -# -wtd_rsnapshot_config_version: 1.2 - -wtd_rsnapshot_config_snapshot_root: "/.snapshots/" - -wtd_rsnapshot_config_cmd_rm: "/usr/bin/rm" -wtd_rsnapshot_config_cmd_rsync: "/usr/bin/rsync" -wtd_rsnapshot_config_cmd_logger: "/usr/bin/logger" - -# for rsnapreport.pl verbose >= 3 is needed -# --stats also needed for rsnapreport.pl -wtd_rsnapshot_config_verbose: 3 - -wtd_rsnapshot_config_loglevel: 3 -wtd_rsnapshot_config_logfile: "/var/log/rsnapshot" - -wtd_rsnapshot_config_lockfile: "/var/run/rsnapshot.pid" - -wtd_rsnapshot_config_backups: - - backup: /home/ - target: localhost/ - -wtd_rsnapshot_timer_time: '00:30' -wtd_rsnapshot_service_execStart: /usr/bin/rsnapshot %I - -wtd_rsnapshot_configs: - - name: dummy - retains: - - name: "alpha" - value: "6" - # 6 alpha backups a day (once every 4 hours, at 0,4,8,12,16,20) - time: '00/4:00' - - name: "beta" - value: "7" - # 1 beta backup every day, at 11:50PM - time: '23:50' - - name: "gamma" - value: "4" - # 1 gamma backup every week, at 11:40PM, on Saturdays (6th day of week) - time: 'Sat *-*-* 23:40' - - name: "delta" - value: "3" - # 1 delta backup every month, at 11:30PM on the 1st day of the month - time: '*-*-01 23:30' +## Package Management +wtd_srv_rsnapshot_package: "rsnapshot" +# State can be present|latest|absent +wtd_srv_rsnapshot_package_state: "present" + +## Configuration Management +# Below you can find some example configuration. +# You MUST define "retains: []" and "backup: []" +# Please consult man rsnapshot for more information or https://rsnapshot.org +wtd_srv_rsnapshot_conf: [] +# version: 1.2 +# snapshot_root: "/.snapshots/" +# no_create_root: 1 +# cmd_cp: "/usr/bin/cp" +# cmd_rm: "/usr/bin/rm" +# cmd_rsync: "/usr/bin/rsync" +# cmd_ssh: "/usr/bin/ssh" +# cmd_logger: "/usr/bin/logger" +# cmd_du: "/usr/bin/du" +# cmd_rsnapshot_diff: "/usr/bin/rsnapshot-diff" +# cmd_preexec: "" +# cmd_postexec: "" +# linux_lvm_cmd_lvcreate: "/usr/sbin/lvcreate" +# linux_lvm_cmd_lvremove: "/usr/sbin/lvremove" +# linux_lvm_cmd_mount: "/usr/bin/mount" +# linux_lvm_cmd_umount: "/usr/bin/umount" +# retains: +# - name: "some name" # the name for the retains +# value: 15 # how many backups do want to keep +# time: "daily" # systemd timer time format +# enabled: true # define if the timer should be enabled (default true) +# verbose: 2 +# loglevel: 3 +# logfile: "/var/log/rsnapshot/rsnapshot.log" +# lockfile: "/var/run/rsnapshot.pid" +# stop_on_stale_lockfile: 0 +# rsync_short_args: "" +# rsync_long_args: "" +# ssh_args: "-p 22" +# du_args: "-csh" +# one_fs: 0 +# includes: [] # global include patterns +# excludes: [] # global exclude patterns +# include_files: [] # global include_files +# exclude_files: [] # global exclude_files +# link_dest: 0 +# sync_first: 0 +# use_lazy_deletes: 0 +# rsync_numtries: 0 +# linux_lvm_snapshotsize: "100M" +# linux_lvm_snapshotname: "rsnapshot" +# linux_lvm_vgpath: "/dev" +# linux_lvm_mountpath: "/path/to/mount/lvm/snapshot/during/backup" +# The backup list can take multiple statements +# backups: +# - comment: "" # useful, if you need a comment line +# - src: "/foo/" # define source and destination of your backups +# dest: "foo/" +# options: "" # you can backup specific options like excludes +# - script: "foo.sh" # define a script and a destination for a scripted backup +# dest: "foo/" +# - exec: "foo.sh" # Just run some command ``` -## Example Playbook +### Example Playbook -Simple Example: +Running Ansible +[Roles](https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.html) +can be done in a +[playbook](https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html). -```yaml -- hosts: servers - roles: - - { role: while_true_do.rsnapshot } - vars: - - wtd_rsnapshot_config_shapshot_root: '/backup/' - - wtd_rsnapshot_config_retains: - - name: 'daily' - value: '7' - - wtd_rsnapshot_config_backups: - - backup: /home/cinux - target: home/ -``` +#### Simple -Advanced Example: -rsnapshot is not designed to run multiple instance at the same time by using one config-file. -Below you find a playbook with variable to get an illustration how the role works if you want to use multiple configurations. -*NOTE:* Please only run this playbook in a test machine. Its for testing purpose. +Doing daily backups of your localhost and keep them for 14 days. -```yaml -- hosts: $VMIP - become: yes +``` +--- +- hosts: all roles: - - { role: while_true_do.rsnapshot } - vars: - wtd_rsnapshot_configs: - - name: 'root' - retains: - - name: 'daily' - time: '*-*-* 00:35' - value: 7 - - name: 'weekly' - time: 'Sun *-*-* 00:00' - value: 4 - - name: 'monthly' - time: '*-*-1 01:00' - value: 2 - backups: - - src: /root/doc - target: localhost/root - - src: /root/picture - target: localhost/root - - src: /root/videos - target: localhost/root - execStart: "/usr/bin/rsnapshot -c /etc/rsnapshot-root.conf %i" - - name: 'user' + - role: while_true_do.rpo_epel + - role: while_true_do.srv_rsnapshot + wtd_srv_rsnapshot_conf: retains: - - name: 'daily' - value: 7 + - name: "daily" + value: 14 + time: "daily" + enabled: true backups: - - src: /home/user/doc - target: localhost/user - - src: /home/user/picture - target: localhost/user - - src: /home/user/videos - target: localhost/user - execStart: "/usr/bin/rsnapshot -c /etc/rsnapshot-user.conf %i" + - comment: "# LOCALHOST" + - src: "/home/" + dest: "localhost/" + - src: "/var/log" + dest: "localhost/" + - src: "/etc/" + dest: "localhost/" ``` -After you have execute the playbook you can check with following commands: +#### Advanced + +This example does daily, weekly and monthly backups for localhost and +example.com (via backup user). + ``` -systemctl list-timers | grep rsnapshot -ls -la /etc/rsnapshot-* +- hosts: all + roles: + - role: while_true_do.rpo_epel + - role: while_true_do.srv_rsnapshot + wtd_srv_rsnapshot_conf: + retains: + - name: "daily" + value: 14 + time: "daily" + - name: "weekly" + value: 8 + time: "weekly" + - name: "monthly" + value: 12 + time: "monthly" + backups: + - comment: "LOCALHOST" + - src: "/etc/" + dest: "localhost/" + - src: "/home/" + dest: "localhost/" + - src: "/var/log/" + dest: "localhost/" + - comment: "EXAMPLE.COM" + - src: "backup@example.com:/etc/" + dest: "example.com/" + - src: "backup@example.com:/home/" + dest: "example.com/" + - src: "backup@example.com:/var/log/" + dest: "example.com/" ``` -For each entry in wtd_rsnapshot_config a separate configuration file gets created under /etc/rsnapshot-*. -Additionally to this for each retains a timer will be created. And of course you can create for each retains a different time. This helps to run different retains at different times. +## Known Issues +1. RedHat Testing is currently not possible in public, due to limitations + in subscriptions. +2. Some services and features cannot be tested properly, due to limitations + in docker. ## Testing -Most of the "generic" tests are located in the Test Library. -Ansible specific testing is done with Molecule. -Infrastructure testing is done with testinfra. -Automated testing is done with Travis CI. -## Contribute / Bugs +Most of the "generic" tests are located in the +[Test Library](https://github.com/while-true-do/test-library). + +Ansible specific testing is done with +[Molecule](https://molecule.readthedocs.io/en/stable/). + +Infrastructure testing is done with +[testinfra](https://testinfra.readthedocs.io/en/stable/). + +Automated testing is done with [Travis CI](https://travis-ci.com/while-true-do). + +## Contribute Thank you so much for considering to contribute. We are very happy, when somebody is joining the hard work. Please fell free to open @@ -218,7 +243,7 @@ See who has contributed already in the [kudos.txt](./kudos.txt). ## License -This work is licensed under a [BSD License](https://opensource.org/licenses/BSD-3-Clause). +This work is licensed under a [BSD-3-Clause License](https://opensource.org/licenses/BSD-3-Clause). ## Contact diff --git a/defaults/main.yml b/defaults/main.yml index bed25f4..384c2a2 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,57 +1,65 @@ --- # defaults/main.yml for rsnapshot -# You can set the state to ["present"|"absent"|"latest"] -wtd_rsnapshot_state: "present" -wtd_rsnapshot_packages: "rsnapshot" +## Package Management +wtd_srv_rsnapshot_package: "rsnapshot" +# State can be present|latest|absent +wtd_srv_rsnapshot_package_state: "present" -wtd_rsnapshot_notify: false - -# -## Default Values -## can be overwrite -## -# -wtd_rsnapshot_config_version: 1.2 - -wtd_rsnapshot_config_snapshot_root: "/.snapshots/" - -wtd_rsnapshot_config_cmd_rm: "/usr/bin/rm" -wtd_rsnapshot_config_cmd_rsync: "/usr/bin/rsync" -wtd_rsnapshot_config_cmd_logger: "/usr/bin/logger" - -# for rsnapreport.pl verbose >= 3 is needed -# --stats also needed for rsnapreport.pl -wtd_rsnapshot_config_verbose: 3 - -wtd_rsnapshot_config_loglevel: 3 -wtd_rsnapshot_config_logfile: "/var/log/rsnapshot" - -wtd_rsnapshot_config_lockfile: "/var/run/rsnapshot.pid" - -wtd_rsnapshot_config_backups: - - backup: /home/ - target: localhost/ - -wtd_rsnapshot_timer_time: '00:30' -wtd_rsnapshot_service_execStart: /usr/bin/rsnapshot %I - -wtd_rsnapshot_configs: - - name: dummy - retains: - - name: "alpha" - value: "6" - # 6 alpha backups a day (once every 4 hours, at 0,4,8,12,16,20) - time: '00/4:00' - - name: "beta" - value: "7" - # 1 beta backup every day, at 11:50PM - time: '23:50' - - name: "gamma" - value: "4" - # 1 gamma backup every week, at 11:40PM, on Saturdays (6th day of week) - time: 'Sat *-*-* 23:40' - - name: "delta" - value: "3" - # 1 delta backup every month, at 11:30PM on the 1st day of the month - time: '*-*-01 23:30' +## Configuration Management +# Below you can find some example configuration. +# You MUST define "retains: []" and "backup: []" +# Please consult man rsnapshot for more information or https://rsnapshot.org +wtd_srv_rsnapshot_conf: [] +# version: 1.2 +# snapshot_root: "/.snapshots/" +# no_create_root: 1 +# cmd_cp: "/usr/bin/cp" +# cmd_rm: "/usr/bin/rm" +# cmd_rsync: "/usr/bin/rsync" +# cmd_ssh: "/usr/bin/ssh" +# cmd_logger: "/usr/bin/logger" +# cmd_du: "/usr/bin/du" +# cmd_rsnapshot_diff: "/usr/bin/rsnapshot-diff" +# cmd_preexec: "" +# cmd_postexec: "" +# linux_lvm_cmd_lvcreate: "/usr/sbin/lvcreate" +# linux_lvm_cmd_lvremove: "/usr/sbin/lvremove" +# linux_lvm_cmd_mount: "/usr/bin/mount" +# linux_lvm_cmd_umount: "/usr/bin/umount" +# retains: +# - name: "some name" # the name for the retains +# value: 15 # how many backups do want to keep +# time: "daily" # systemd timer time format +# enabled: true # define if the timer should be enabled (default true) +# verbose: 2 +# loglevel: 3 +# logfile: "/var/log/rsnapshot/rsnapshot.log" +# lockfile: "/var/run/rsnapshot.pid" +# stop_on_stale_lockfile: 0 +# rsync_short_args: "" +# rsync_long_args: "" +# ssh_args: "-p 22" +# du_args: "-csh" +# one_fs: 0 +# includes: [] # global include patterns +# excludes: [] # global exclude patterns +# include_files: [] # global include_files +# exclude_files: [] # global exclude_files +# link_dest: 0 +# sync_first: 0 +# use_lazy_deletes: 0 +# rsync_numtries: 0 +# linux_lvm_snapshotsize: "100M" +# linux_lvm_snapshotname: "rsnapshot" +# linux_lvm_vgpath: "/dev" +# linux_lvm_mountpath: "/path/to/mount/lvm/snapshot/during/backup" +# The backup list can take multiple statements +# backups: +# - comment: "" # useful, if you need a comment line +# - src: "/foo/" # define source and destination of your backups +# dest: "foo/" +# options: "" # you can backup specific options like excludes +# - script: "foo.sh" # define a script and a destination for a scripted backup +# dest: "foo/" +# - exec: "foo.sh" # Just run some command diff --git a/kudos.txt b/kudos.txt index 978411a..0b76fbd 100644 --- a/kudos.txt +++ b/kudos.txt @@ -10,23 +10,16 @@ project: site: https://while-true-do.io mail: hello@while-true-do.io -# TODO: Fill in the below content and remove this line. contributors: - - name: - code: - home: , - mail: - role: - site: - work: , + - name: daniel-wtd + mail: daniel@while-true-do.io + role: Founder / Owner + site: https://while-true-do.io + + - name: cinux90 + mail: c.hofmann90@gmx.de + role: Developer partners: - name: kudos.txt site: https://kudos-txt.io - - - name: - site: - -software: - - name: - site: \ No newline at end of file diff --git a/meta/main.yml b/meta/main.yml index 66e8e90..a5c7348 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -1,22 +1,39 @@ --- -dependencies: [] +# meta file for while_true_do.srv_rsnapshot galaxy_info: role_name: srv_rsnapshot - author: while-true-do.org - description: Install and configure rsnapshot. - company: while-true-do.org - - github_branch: master + author: while-true-do.io + description: An Ansible Role to install and configure rsanpshot. license: BSD - min_ansible_version: 2.0 + min_ansible_version: 2.7 + # Supported platforms: https://galaxy.ansible.com/api/v1/platforms/ + # Please also maintain + # - molecule/default/molecule.yml + # - molecule/default/tests/* + # - .travis.yml platforms: - name: EL versions: - 7 + - name: Fedora + versions: + - 29 + - 30 + + # Some very common tags are: + # system, virtualization, development, monitoring, packaging, container + # configuration, cloud, security galaxy_tags: - - backup - rsnapshot + - backup + - configuration + - package + - scheduler + - service + - system + +dependencies: [] diff --git a/molecule/default/Dockerfile.j2 b/molecule/default/Dockerfile.j2 index 4d391c2..cd49404 100644 --- a/molecule/default/Dockerfile.j2 +++ b/molecule/default/Dockerfile.j2 @@ -6,7 +6,6 @@ FROM {{ item.image }} {% endif %} - RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get install -y python sudo bash ca-certificates && apt-get clean; \ elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install python sudo python-devel python*-dnf bash && dnf clean all; \ elif [ $(command -v yum) ]; then yum makecache fast && yum install -y python sudo yum-plugin-ovl bash && sed -i 's/plugins=0/plugins=1/g' /etc/yum.conf && yum clean all; \ @@ -19,4 +18,4 @@ ENV ANSIBLE_USER=ansible SUDO_GROUP=wheel RUN useradd -m ${ANSIBLE_USER} \ && usermod -aG ${SUDO_GROUP} ${ANSIBLE_USER} \ - && sed -i "s/^%${SUDO_GROUP}.*/%${SUDO_GROUP} ALL=(ALL) NOPASSWD: ALL/g" /etc/sudoers \ No newline at end of file + && sed -i "s/^%${SUDO_GROUP}.*/%${SUDO_GROUP} ALL=(ALL) NOPASSWD: ALL/g" /etc/sudoers diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml index f60f72f..e67d3d2 100644 --- a/molecule/default/molecule.yml +++ b/molecule/default/molecule.yml @@ -12,6 +12,7 @@ scenario: - idempotence - side_effect - verify + - destroy lint: name: yamllint @@ -25,12 +26,11 @@ driver: name: docker platforms: - - name: rsnapshot-centos7-default + - name: ${name:-instance} image: ${image:-centos:7} command: /usr/sbin/init - volume_mounts: + volumes: - /sys/fs/cgroup:/sys/fs/cgroup:ro - - /tmp/wtd_testing:/mnt/files tmpfs: - /run - /tmp diff --git a/molecule/default/playbook.yml b/molecule/default/playbook.yml index 769e07c..d6959a2 100644 --- a/molecule/default/playbook.yml +++ b/molecule/default/playbook.yml @@ -2,5 +2,24 @@ - name: Converge hosts: all roles: - # TODO: Provide a proper playbook, to test features of your role. - role: while_true_do.srv_rsnapshot + wtd_srv_rsnapshot_conf: + retains: + - name: 'daily' + value: 8 + time: 'daily' + - name: 'weekly' + time: 'weekly' + value: 4 + enabled: true + - name: 'monthly' + time: 'monthly' + value: 2 + backups: + - comment: "LOCALHOST" + - src: "/etc/" + dest: "localhost/" + - src: "/home/" + dest: "localhost/" + - src: "/var/log/" + dest: "localhost/" diff --git a/molecule/default/prepare.yml b/molecule/default/prepare.yml index cf5f0ed..7864948 100644 --- a/molecule/default/prepare.yml +++ b/molecule/default/prepare.yml @@ -2,5 +2,4 @@ - name: Prepare hosts: all roles: - # TODO: Provide a proper playbook, to test features of your role. - role: while_true_do.rpo_epel diff --git a/molecule/default/tests/test_default.py b/molecule/default/tests/test_default.py index 6d90cae..d2d7aa1 100644 --- a/molecule/default/tests/test_default.py +++ b/molecule/default/tests/test_default.py @@ -1,9 +1,7 @@ -# TODO: Edit the tests and remove this line. -# Some examples are given below. - import os import testinfra.utils.ansible_runner +import pytest testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') @@ -13,3 +11,27 @@ def test_rsnapshot_package(host): pkg = host.package('rsnapshot') assert pkg.is_installed + + +@pytest.mark.parametrize("name", [ + ("daily"), + ("weekly"), + ("monthly"), +]) +def test_rsnapshot_timer(host, name): + timer = host.service("rsnapshot-"+name+".timer") + + assert timer.is_enabled + + +def test_rsansphot_exec_timer(host): + file1 = host.file("/.snapshots/daily.0/localhost/etc/") + file2 = host.file("/.snapshots/daily.0/localhost/home/") + file3 = host.file("/.snapshots/daily.0/localhost/var/") + + with host.sudo(): + host.run("systemctl start rsnapshot@daily.service") + + assert file1.exists + assert file2.exists + assert file3.exists diff --git a/molecule/multiple/Dockerfile.j2 b/molecule/multiple/Dockerfile.j2 deleted file mode 100644 index 4d391c2..0000000 --- a/molecule/multiple/Dockerfile.j2 +++ /dev/null @@ -1,22 +0,0 @@ - -{% if item.registry is defined %} -FROM {{ item.registry.url }}/{{ item.image }} -{% else %} -FROM {{ item.image }} -{% endif %} - - - -RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get install -y python sudo bash ca-certificates && apt-get clean; \ - elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install python sudo python-devel python*-dnf bash && dnf clean all; \ - elif [ $(command -v yum) ]; then yum makecache fast && yum install -y python sudo yum-plugin-ovl bash && sed -i 's/plugins=0/plugins=1/g' /etc/yum.conf && yum clean all; \ - elif [ $(command -v zypper) ]; then zypper refresh && zypper install -y python sudo bash python-xml && zypper clean -a; \ - elif [ $(command -v apk) ]; then apk update && apk add --no-cache python sudo bash ca-certificates; \ - elif [ $(command -v xbps-install) ]; then xbps-install -Syu && xbps-install -y python sudo bash ca-certificates && xbps-remove -O; \ - fi - -ENV ANSIBLE_USER=ansible SUDO_GROUP=wheel - -RUN useradd -m ${ANSIBLE_USER} \ - && usermod -aG ${SUDO_GROUP} ${ANSIBLE_USER} \ - && sed -i "s/^%${SUDO_GROUP}.*/%${SUDO_GROUP} ALL=(ALL) NOPASSWD: ALL/g" /etc/sudoers \ No newline at end of file diff --git a/molecule/multiple/molecule.yml b/molecule/multiple/molecule.yml deleted file mode 100644 index 742b3da..0000000 --- a/molecule/multiple/molecule.yml +++ /dev/null @@ -1,60 +0,0 @@ ---- -scenario: - name: multiple - test_sequence: - - lint - - destroy - - dependency - - syntax - - create - - prepare - - converge - - idempotence - - side_effect - - verify - - destroy - -lint: - name: yamllint - -dependency: - name: galaxy - options: - role-file: requirements.yml - -driver: - name: docker - -platforms: - - name: rsnapshot-centos7-multiple - image: ${image:-centos:7} - command: /usr/sbin/init - volume_mounts: - - /sys/fs/cgroup:/sys/fs/cgroup:ro - - /tmp/wtd_testing:/mnt/files - tmpfs: - - /run - - /tmp - # privileged: true - groups: - - ${pygroup:-python} - environment: - container: docker - -provisioner: - name: ansible - lint: - name: ansible-lint - inventory: - group_vars: - all: - ansible_user: ansible - python3: - ansible_python_interpreter: /usr/bin/python3 - -verifier: - name: testinfra - options: - v: 1 - lint: - name: flake8 diff --git a/molecule/multiple/playbook.yml b/molecule/multiple/playbook.yml deleted file mode 100644 index 06cc959..0000000 --- a/molecule/multiple/playbook.yml +++ /dev/null @@ -1,38 +0,0 @@ ---- -- name: Converge - hosts: all - roles: - # TODO: Provide a proper playbook, to test features of your role. - - role: while_true_do.srv_rsnapshot - wtd_rsnapshot_configs: - - name: 'root' - retains: - - name: 'daily' - time: '*-*-* 00:35' - value: 7 - - name: 'weekly' - time: 'Sun *-*-* 00:00' - value: 4 - - name: 'monthly' - time: '*-*-1 01:00' - value: 2 - backups: - - src: /root/doc - target: localhost/root - - src: /root/picture - target: localhost/root - - src: /root/videos - target: localhost/root - execStart: "/usr/bin/rsnapshot -c /etc/rsnapshot-root.conf %i" - - name: 'user' - retains: - - name: 'daily' - value: 7 - backups: - - src: /home/user/doc - target: localhost/user - - src: /home/user/picture - target: localhost/user - - src: /home/user/videos - target: localhost/user - execStart: "/usr/bin/rsnapshot -c /etc/rsnapshot-user.conf %i" diff --git a/molecule/multiple/prepare.yml b/molecule/multiple/prepare.yml deleted file mode 100644 index 49dc42a..0000000 --- a/molecule/multiple/prepare.yml +++ /dev/null @@ -1,41 +0,0 @@ ---- -- name: Prepare - hosts: all - roles: - - role: while_true_do.rpo_epel - tasks: - - name: Create folder - become: true - file: - path: "/root/{{ item }}" - state: directory - with_items: - - doc - - picture - - videos - - - name: fill files for root - become: true - command: 'dd bs=2M count=10 if=/dev/random of=/root/{{ item }}/dummyfile-{{ item }}' - with_items: - - doc - - picture - - videos - - - name: Create user folder - become: true - file: - path: "/home/user/{{ item }}" - state: directory - with_items: - - doc - - picture - - videos - - - name: fill files for root - become: true - command: 'dd bs=2M count=10 if=/dev/random of=/home/user/{{ item }}/dummyfile-{{ item }}' - with_items: - - doc - - picture - - videos diff --git a/molecule/multiple/tests/test_default.py b/molecule/multiple/tests/test_default.py deleted file mode 100644 index 02d7adb..0000000 --- a/molecule/multiple/tests/test_default.py +++ /dev/null @@ -1,59 +0,0 @@ -# TODO: Edit the tests and remove this line. -# Some examples are given below. - -import os -import pytest -import testinfra.utils.ansible_runner - -testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( - os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') - - -@pytest.mark.parametrize("name,retain", [ - ('root', 'daily'), - ('root', 'weekly'), - ('root', 'monthly'), - ('user', 'daily'), -]) -def test_rsnapshot_config_file(host, name, retain): - file = host.file('/etc/rsnapshot-' + name + '.conf') - - assert file.exists - assert file.user == 'root' - assert file.group == 'root' - assert file.mode == 0o644 - - -@pytest.mark.parametrize("name,retain", [ - ('root', 'daily'), - ('root', 'weekly'), - ('root', 'monthly'), - ('user', 'daily'), -]) -def test_rsnapshot_systemd_timer_endabled_started(host, name, retain): - srv = host.service("rsnapshot-" + name + "-" - + retain + ".timer") - - assert srv.is_running - assert srv.is_enabled - - -def test_rsnapshot_package(host): - pkg = host.package('rsnapshot') - - assert pkg.is_installed - - -@pytest.mark.parametrize("name,retain,time,value", [ - ('root', 'daily', '*-*-* 00:35', '7'), - ('root', 'weekly', 'Sun *-*-* 00:00', '4'), - ('root', 'monthly', '*-*-1 01:00', '2'), - ('user', 'daily', '00:30', '7'), -]) -def test_rsnapshot_running_timers(host, name, retain, time, value): - cmd = host.run('systemctl list-timers') - timers = cmd.stdout - -# rsnapshot-user-daily.timer - assert "rsnapshot-" + name + "-" + retain + ".timer" in timers - assert "rsnapshot-" + name + "@" + retain + ".service" in timers diff --git a/molecule/multiple/tests/test_default.py_backup b/molecule/multiple/tests/test_default.py_backup deleted file mode 100644 index 969e1cb..0000000 --- a/molecule/multiple/tests/test_default.py_backup +++ /dev/null @@ -1,99 +0,0 @@ -# TODO: Edit the tests and remove this line. -# Some examples are given below. - -import os -import pytest -import testinfra.utils.ansible_runner - -testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( - os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') - -variablen = [{ - 'name': 'root', - 'retains': [{'name': 'daily', 'value': 7, 'time': '00:30'}, - {'name': 'weekly', 'value': 4, 'time': '00:40'}, - {'name': 'monthly', 'value': 2}], - 'backups': [{'src': '/root/doc', 'target': 'localhost/root'}, - {'src': '/root/picture', 'target': 'localhost/root'}, - {'src': '/root/videos', 'target': 'localhost/root'}], - 'execStart': '/usr/bin/rsnapshot -c /etc/rsnapshot-root.conf %i', - }, { - 'name': 'user', - 'retains': [{'name': 'daily', 'value': 7}], - 'backups': [{'src': '/home/user/doc', 'target': 'localhost/user' - }, {'src': '/home/user/picture', - 'target': 'localhost/user'}, - {'src': '/home/user/videos', - 'target': 'localhost/user'}], - 'execStart': '/usr/bin/rsnapshot -c /etc/rsnapshot-user.conf %i', - }] - - -def test_rsnapshot_config_file(host): - file = host.file('/etc/rsnapshot.conf') - - assert file.exists - assert file.user == 'root' - assert file.group == 'root' - assert file.mode == 0o644 - - -#@pytest.mark.parametrize("name,retains,backups,execstart", [ -# ("root", "[{'name': 'daily', 'value': 7, 'time': '00:30'}", -# "{'src': '/root/doc', 'target': 'localhost/root'}]", -# "/usr/bin/rsnapshot -c /etc/rsnapshot-root.conf %i" ), -# ("user", "{'name': 'daily', 'value': 7}", -# "{'src': '/home/user/doc', 'target': 'localhost/user'}", -# "/usr/bin/rsnapshot -c /etc/rsnapshot-user.conf %i") -#]) -#def test_rsnapshot_systemd_timer(host,name,retains,backups,execstart): -# for retain in retains: -# file = host.file('/etc/systemd/system/rsnapshot-' -# + name -# + '-' + retain.name + '.timer') -# assert file.exists -# assert file.user == 'root' -# assert file.group == 'root' -# assert file.mode == 0o644 -# #assert file.contains('Unit=rsnapshot-' + config.get('name') -# # + '@' + retain.get('name') + '.service') -# #if retain.get('time') is None: -# # assert file.contains('OnCalendar=00:30') -# #else: -# # assert file.contains('OnCalendar=' + retain.get('time') ) -# - -def test_rsnapshot_systemd_service_user_daily(host): - host.ansible("systemd","name=rsnapshot-user@daily.service state=started","become=true") - - snapshotfolder = host.file('/.snapshot') - assert snapshotfolder.exists - -#def test_rsnapshot_systemd_service(host): -# global variablen -# for config in variablen: -# file = host.file('/etc/systemd/system/rsnapshot-' -# + config.get('name') + '@.service') -# -# assert file.exists -# assert file.user == 'root' -# assert file.group == 'root' -# assert file.mode == 0o644 -# assert file.contains('ExecStart=/usr/bin/rsnapshot %I 2>&1') -# -# -def test_rsnapshot_systemd_timer_endabled_started(host): - global variablen - for config in variablen: - for retain in config.get('retains'): - srv = host.service("rsnapshot-" + config.get('name') + "-" - + retain.get('name') + ".timer") - - assert srv.is_running - assert srv.is_enabled - - -def test_rsnapshot_package(host): - pkg = host.package('rsnapshot') - - assert pkg.is_installed diff --git a/requirements.yml b/requirements.yml index 7662425..ecc5e85 100644 --- a/requirements.yml +++ b/requirements.yml @@ -5,6 +5,4 @@ # contact: hello@while-true-do.io # license: BSD-3-Clause -# TODO: Edit or delete this file - - src: while_true_do.rpo_epel diff --git a/tasks/main.yml b/tasks/main.yml index b636b4a..79904c7 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,51 +1,83 @@ --- # tasks/main.yml for rsnapshot -- name: "Set rsnapshot to state {{ wtd_rsnapshot_state }}" - become: true - package: - name: "{{ wtd_rsnapshot_packages }}" - state: "{{ wtd_rsnapshot_state }}" -- name: 'Include configuration for notification' - include_tasks: notify.yml - when: wtd_rsnapshot_notify - -- name: 'Set configurations for rsnapshot' +- name: Manage rsnapshot Packages + package: + name: "{{ wtd_srv_rsnapshot_package }}" + state: "{{ wtd_srv_rsnapshot_package_state }}" become: true + tags: + - rsnapshot + - backup + - package + +- name: Manage rsnapshot Configuration template: src: "rsnapshot.conf.j2" - dest: "/etc/rsnapshot-{{ item.name }}.conf" + dest: "/etc/rsnapshot.conf" + owner: "root" + group: "root" mode: 0644 - owner: root - group: root validate: "rsnapshot -c %s configtest" - with_items: - - "{{ wtd_rsnapshot_configs }}" - -- name: "Create Timers for Backup" become: true + tags: + - rsnapshot + - backup + - configuration + +# systemd scheduler +- name: Manage rsnapshot backup Service File template: - src: "rsnapshot.timer.j2" - dest: "/etc/systemd/system/rsnapshot-{{ item.0.name }}-{{ item.1.name }}.timer" - owner: root - group: root + src: "rsnapshot.service.j2" + dest: "/etc/systemd/system/rsnapshot@.service" + owner: "root" + group: "root" mode: 0644 - loop: "{{ wtd_rsnapshot_configs | subelements('retains') }}" - -- name: "Create Service for Backup" become: true + tags: + - rsnapshot + - backup + - configuration + - scheduler + - service + - systemd + - timer + +- name: Manage rsnapshot backup Timer File template: - src: "rsnapshot@.service.j2" - dest: "/etc/systemd/system/rsnapshot-{{ item.name }}@.service" - owner: root - group: root + src: "rsnapshot.timer.j2" + dest: "/etc/systemd/system/rsnapshot-{{ retain.name }}.timer" + owner: "root" + group: "root" mode: 0644 - loop: "{{ wtd_rsnapshot_configs }}" - -- name: "Enable and start service rsnapshot timer" become: true + loop: "{{ wtd_srv_rsnapshot_conf.retains }}" + loop_control: + loop_var: retain + tags: + - rsnapshot + - backup + - configuration + - scheduler + - service + - systemd + - timer + +- name: Manage rsnapshot backup Timer systemd: - name: "rsnapshot-{{ item.0.name }}-{{ item.1.name }}.timer" - state: started - enabled: yes - loop: "{{ wtd_rsnapshot_configs | subelements('retains') }}" + name: "rsnapshot-{{ retain.name }}.timer" + state: "started" + enabled: "{{ retain.enabled | default('true') }}" + daemon_reload: true + become: true + loop: "{{ wtd_srv_rsnapshot_conf.retains }}" + loop_control: + loop_var: retain + tags: + - rsnapshot + - backup + - configuration + - scheduler + - service + - systemd + - timer diff --git a/tasks/notify.yml b/tasks/notify.yml deleted file mode 100644 index debe9ac..0000000 --- a/tasks/notify.yml +++ /dev/null @@ -1,20 +0,0 @@ ---- -- name: "Get Version of rsnapshot" - # shell: "yum info rsnapshot | grep Version | awk -F : '{ print $2 }'| sed 's/ //g'" - command: "`which yum` list installed rsnapshot | grep rsnapshot | awk '{print $2}' | cut -d'-' -f1" - register: wtd_rsnapshot_notify_version - changed_when: wtd_rsnapshot_notify_version.rc == 0 - failed_when: wtd_rsnapshot_notify_version.rc != 0 - -- name: "Copy rsnapreport.pl into bin folder" - command: "cp /usr/share/doc/rsnapshot-{{ rsnapshot_version.stdout }}/utils/rsnapreport.pl /usr/local/bin/rsnapreport.pl" - register: wtd_rsnapshot_notify_copy - changed_when: wtd_rsnapshot_notify_copy.rc == 0 - failed_when: wtd_rsnapshot_notify_copy.rc != 0 - -- name: "Change Permissions of rsnapreport.pl" - file: - path: /usr/local/bin/rsnapreport.pl - owner: root - group: root - mode: 0655 diff --git a/templates/rsnapshot.conf.j2 b/templates/rsnapshot.conf.j2 index faa0c05..1e3c317 100644 --- a/templates/rsnapshot.conf.j2 +++ b/templates/rsnapshot.conf.j2 @@ -1,495 +1,169 @@ -################################################# -# rsnapshot.conf - rsnapshot configuration file # -################################################# -# # -# PLEASE BE AWARE OF THE FOLLOWING RULE: # -# # -# This file requires tabs between elements # -# # -################################################# +{{ ansible_managed | comment }} -####################### -# CONFIG FILE VERSION # -####################### - -config_version {{ item.config_version | default(wtd_rsnapshot_config_version) }} - -########################### -# SNAPSHOT ROOT DIRECTORY # -########################### - -# All snapshots will be stored under this root directory. -# -snapshot_root {{ item.snapshot_root | default(wtd_rsnapshot_config_snapshot_root) }} - -# if defined no_create_root is enabled, rsnapshot will not automatically create the -# snapshot_root directory. This is particularly useful if defined you are backing -# up to removable media, such as a FireWire or USB drive. -# -{% if item.no_ceate_root is defined or wtd_rsnapshot_config_no_ceate_root is defined %} -no_create_root {{ item.no_create_root | default(wtd_rsnapshot_config_no_ceate_root) }} -{% else %} -#no_create_root 1 -{% endif %} - -################################# -# EXTERNAL PROGRAM DEPENDENCIES # -################################# - -# LINUX USERS: Be sure to uncomment "cmd_cp". This gives you extra features. -# EVERYONE ELSE: Leave "cmd_cp" commented out for compatibility. -# -# See the README file or the man page for more details. -# -{% if item.cmd_cp is defined or wtd_rsnapshot_config_cmd_cp is defined %} -cmd_cp {{ item.cmd_cp | default(wtd_rsnapshot_config_cmd_cp) }} -{% else %} -#cmd_cp /usr/bin/cp -{% endif %} - -# uncomment this to use the rm program instead of the built-in perl routine. -# -{% if item.cmd_rm is defined or wtd_rsnapshot_config_cmd_rm is defined %} -cmd_rm {{ item.cmd_rm | default(wtd_rsnapshot_config_cmd_rm) }} -{% else %} -cmd_rm /usr/bin/rm -{% endif %} - - -# rsync must be enabled for anything to work. This is the only command that -# must be enabled. -# -{% if item.cmd_rsync is defined %} -cmd_rsync {{ item.cmd_rsync }} -{% else %} -{% if wtd_rsnapshot_config_cmd_rsync is defined %} -cmd_rsync {{ wtd_rsnapshot_config_cmd_rsync }} -{% else %} -cmd_rsync /usr/bin/rsync -{% endif %} -{% endif %} - -# Uncomment this to enable remote ssh backups over rsync. -# -{% if item.cmd_ssh is defined %} -cmd_ssh {{ item.cmd_ssh }} -{% else %} -{% if wtd_rsnapshot_config_cmd_ssh is defined %} -cmd_ssh {{ wtd_rsnapshot_config_cmd_ssh }} -{% else %} -#cmd_ssh /usr/bin/ssh -{% endif %} -{% endif %} +############################################ +# CONFIG FILE VERSION # +############################################ -# Comment this out to disable syslog support. -# -{% if item.cmd_logger is defined %} -cmd_logger {{ item.cmd_logger }} -{% else %} -{% if wtd_rsnapshot_config_cmd_logger is defined %} -cmd_logger {{ wtd_rsnapshot_config_cmd_logger }} -{% else %} -#cmd_logger /usr/bin/logger -{% endif %} -{% endif %} +config_version {{ wtd_srv_rsnapshot_conf.config_version | default('1.2') }} -# Uncomment this to specif definedy the path to "du" for disk usage checks. -# if defined you have an older version of "du", you may also want to check the -# "du_args" parameter below. -# -{% if item.cmd_du is defined %} -cmd_du {{ item.cmd_du }} -{% else %} -{% if wtd_rsnapshot_config_cmd_du is defined %} -cmd_du {{ wtd_rsnapshot_config_cmd_du }} -{% else %} -#cmd_du /usr/bin/du -{% endif %} -{% endif %} +############################################ +# SNAPSHOT ROOT DIRECTORY # +############################################ -# Uncomment this to specif definedy the path to rsnapshot-dif definedf. -# -{% if item.cmd_rsnapshot_diff is defined %} -cmd_rsnapshot_diff {{ item.cmd_rsnapshot_diff }} -{% else %} -{% if wtd_rsnapshot_config_cmd_rsnapshot_diff is defined %} -cmd_rsnapshot_diff {{ wtd_rsnapshot_config_cmd_rsnapshot_diff }} -{% else %} -#cmd_rsnapshot_diff /usr/local/bin/rsnapshot-diff -{% endif %} +snapshot_root {{ wtd_srv_rsnapshot_conf.snapshot_root | default('/.snapshots/') }} +{% if wtd_srv_rsnapshot_conf.no_create_root is defined %} +no_create_root {{ wtd_srv_rsnapshot_conf.no_create_root }} {% endif %} -# Specif definedy the path to a script (and any optional arguments) to run right -# before rsnapshot syncs files -# -{% if item.cmd_preexec is defined %} -cmd_preexec {{ item.cmd_preexec }} -{% else %} -{% if wtd_rsnapshot_config_cmd_preexec is defined %} -cmd_preexec {{ wtd_rsnapshot_config_cmd_preexec }} -{% else %} -#cmd_preexec /path/to/preexec/script -{% endif %} -{% endif %} +############################################ +# EXTERNAL PROGRAM DEPENDENCIES # +############################################ -# Specif definedy the path to a script (and any optional arguments) to run right -# after rsnapshot syncs files -# -{% if item.cmd_postexec is defined %} -cmd_postexec {{ item.cmd_postexec }} -{% else %} -{% if wtd_rsnapshot_config_cmd_postexec is defined %} -cmd_postexec {{ wtd_rsnapshot_config_cmd_postexec }} -{% else %} -#cmd_postexec /path/to/postexec/script +cmd_cp {{ wtd_srv_rsnapshot_conf.cmd_cp | default('/usr/bin/cp') }} +cmd_rm {{ wtd_srv_rsnapshot_conf.cmd_rm | default('/usr/bin/rm') }} +cmd_rsync {{ wtd_srv_rsnapshot_conf.cmd_rsync | default('/usr/bin/rsync') }} +{% if wtd_srv_rsnapshot_conf.cmd_ssh is defined %} +cmd_ssh {{ wtd_srv_rsnapshot_conf.cmd_ssh }} {% endif %} +{% if wtd_srv_rsnapshot_conf.cmd_logger is defined %} +cmd_logger {{ wtd_srv_rsnapshot_conf.cmd_logger }} {% endif %} - -# Paths to lvcreate, lvremove, mount and umount commands, for use with -# Linux LVMs. -# -{% if item.linux_lvm_cmd_lvcreate is defined %} -linux_lvm_cmd_lvcreate {{ item.linux_lvm_cmd_lvcreate }} -{% else %} -{% if wtd_rsnapshot_config_linux_lvm_cmd_lvcreate is defined %} -linux_lvm_cmd_lvcreate {{ wtd_rsnapshot_config_linux_lvm_cmd_lvcreate }} -{% else %} -#linux_lvm_cmd_lvcreate /usr/sbin/lvcreate +{% if wtd_srv_rsnapshot_conf.cmd_du is defined %} +cmd_du {{ wtd_srv_rsnapshot_conf.cmd_du }} {% endif %} +{% if wtd_srv_rsnapshot_conf.cmd_rsnapshot_diff is defined %} +cmd_rsnapshot_diff {{ wtd_srv_rsnapshot_conf.cmd_rsnapshot_diff }} {% endif %} - -{% if item.linux_lvm_cmd_lvremove is defined %} -linux_lvm_cmd_lvremove {{ item.linux_lvm_cmd_lvremove }} -{% else %} -{% if wtd_rsnapshot_config_linux_lvm_cmd_lvremove is defined %} -linux_lvm_cmd_lvremove {{ wtd_rsnapshot_config_linux_lvm_cmd_lvremove }} -{% else %} -#linux_lvm_cmd_lvremove /usr/sbin/lvremove +{% if wtd_srv_rsnapshot_conf.preexec_diff is defined %} +cmd_preexec {{ wtd_srv_rsnapshot_conf.cmd_preexec }} {% endif %} +{% if wtd_srv_rsnapshot_conf.cmd_postexec is defined %} +cmd_postexec {{ wtd_srv_rsnapshot_conf.cmd_postexec }} {% endif %} -{% if item.linux_lvm_cmd_mount is defined %} -linux_lvm_cmd_mount {{ item.linux_lvm_cmd_mount }} -{% else %} -{% if wtd_rsnapshot_config_linux_lvm_cmd_mount is defined %} -linux_lvm_cmd_mount {{ wtd_rsnapshot_config_linux_lvm_cmd_mount }} -{% else %} -#linux_lvm_cmd_mount /usr/bin/mount +{% if wtd_srv_rsnapshot_conf.linux_lvm_cmd_lvcreate is defined %} +linux_lvm_cmd_lvcreate {{ wtd_srv_rsnapshot_conf.linux_lvm_cmd_lvcreate }} {% endif %} +{% if wtd_srv_rsnapshot_conf.linux_lvm_cmd_lvremove is defined %} +linux_lvm_cmd_lvremove {{ wtd_srv_rsnapshot_conf.linux_lvm_cmd_lvremove }} {% endif %} - -{% if item.linux_lvm_cmd_umount is defined %} -linux_lvm_cmd_umount {{ item.linux_lvm_cmd_umount }} -{% else %} -{% if wtd_rsnapshot_config_linux_lvm_cmd_umount is defined %} -linux_lvm_cmd_umount {{ wtd_rsnapshot_config_linux_lvm_cmd_umount }} -{% else %} -#linux_lvm_cmd_umount /usr/bin/umount +{% if wtd_srv_rsnapshot_conf.linux_lvm_cmd_mount is defined %} +linux_lvm_cmd_mount {{ wtd_srv_rsnapshot_conf.linux_lvm_cmd_mount }} {% endif %} +{% if wtd_srv_rsnapshot_conf.linux_lvm_cmd_umount is defined %} +linux_lvm_cmd_umount {{ wtd_srv_rsnapshot_conf.linux_lvm_cmd_umount }} {% endif %} -######################################### -# BACKUP LEVELS / INTERVALS # -# Must be unique and in ascending order # -# e.g. alpha, beta, gamma, etc. # -######################################### +############################################ +# RETAINS # +############################################ -{% for e in item.retains %} -retain {{ e.name }} {{ e.value }} +{% for retain in wtd_srv_rsnapshot_conf.retains %} +retain {{ retain.name }} {{ retain.value }} {% endfor %} ############################################ # GLOBAL OPTIONS # -# All are optional, with sensible defaults # ############################################ -# Verbose level, 1 through 5. -# 1 Quiet Print fatal errors only -# 2 Default Print errors and warnings only -# 3 Verbose Show equivalent shell commands being executed -# 4 Extra Verbose Show extra verbose information -# 5 Debug mode Everything -# +# Logging -verbose {{ item.verbose | default(wtd_rsnapshot_config_verbose) }} +verbose {{ wtd_srv_rsnapshot_conf.verbose | default('2') }} +loglevel {{ wtd_srv_rsnapshot_conf.loglevel | default('3') }} +logfile {{ wtd_srv_rsnapshot_conf.logfile | default ('/var/log/rsnapshot.log') }} -# Same as "verbose" above, but controls the amount of data sent to the -# logfile, if defined one is being used. The default is 3. -# -loglevel {{ item.loglevel | default(wtd_rsnapshot_config_loglevel) }} +# Lockfile -# if defined you enable this, data will be written to the file you specif definedy. The -# amount of data written is controlled by the "loglevel" parameter. -# -{% if item.logfile is defined %} -logfile {{ item.logfile }} -{% else %} -{% if wtd_rsnapshot_config_logfile is defined %} -logfile {{ wtd_rsnapshot_config_logfile }} -{% else %} -#logfile /var/log/rsnapshot -{% endif %} +lockfile {{ wtd_srv_rsnapshot_conf.lockfile | default ('/var/run/rsnapshot.pid') }} +{% if wtd_srv_rsnapshot_conf.stop_on_stale_lockfile is defined %} +stop_on_stale_lockfile {{ wtd_srv_rsnapshot_conf.stop_on_stale_lockfile }} {% endif %} -# if defined enabled, rsnapshot will write a lockfile to prevent two instances -# from running simultaneously (and messing up the snapshot_root). -# if defined you enable this, make sure the lockfile directory is not world -# writable. Otherwise anyone can prevent the program from running. -# -lockfile /var/run/rsnapshot-{{ item.name }}.pid - -# By default, rsnapshot check lockfile, check if defined PID is running -# and if defined not, consider lockfile as stale, then start -# Enabling this stop rsnapshot if defined PID in lockfile is not running -# -{% if item.stop_on_stale_lockfile is defined or wtd_rsnapshot_config_stop_on_stale_lockfile is defined %} -stop_on_stale_lockfile {{ item.stop_on_stale_lockfile | default(wtd_rsnapshot_config_stop_on_stale_lockfile)}} -{% else %} -#stop_on_stale_lockfile 0 +{% if wtd_srv_rsnapshot_conf.rsync_short_args is defined %} +rsync_short_args {{ wtd_srv_rsnapshot_conf.rsync_short_args }} {% endif %} - -# Default rsync args. All rsync commands have at least these options set. -# -{% if item.rsync_short_args is defined or wtd_rsnapshot_config_rsync_long_args is defined %} -rsync_short_args {{ item.rsync_short_args | default(wtd_rsnapshot_config_rsync_long_args) }} -{% else %} -#rync_short_args -a +{% if wtd_srv_rsnapshot_conf.rsync_long_args is defined %} +rsync_long_args {{ wtd_srv_rsnapshot_conf.rsync_long_args }} {% endif %} - -# ssh has no args passed by default, but you can specif definedy some here. -# -{% if item.ssh_args is defined or wtd_rsnapshot_config_ssh_args is defined %} -ssh_args {{ item.ssh_args | default(wtd_rsnapshot_config_ssh_args) }} -{% else %} -#ssh_args -p 22 +{% if wtd_srv_rsnapshot_conf.ssh_args is defined %} +ssh_args {{ wtd_srv_rsnapshot_conf.ssh_args }} {% endif %} - -# Default arguments for the "du" program (for disk space reporting). -# The GNU version of "du" is preferred. See the man page for more details. -# if defined your version of "du" doesn't support the -h flag, try -k flag instead. -# -{% if item.du_args is defined or wtd_rsnapshot_config_du_args is defined %} -du_args {{ item.du_args | default(wtd_rsnapshot_config_du_args) }} -{% else %} -#du_args -csh +{% if wtd_srv_rsnapshot_conf.du_args is defined %} +du_args {{ wtd_srv_rsnapshot_conf.du_args }} {% endif %} - -# if defined this is enabled, rsync won't span filesystem partitions within a -# backup point. This essentially passes the -x option to rsync. -# The default is 0 (off). -# -{% if item.one_fs is defined or wtd_rsnapshot_config_one_fs is defined %} -one_fs {{ item.one_fs | default(wtd_rsnapshot_config_one_fs) }} -{% else %} -#one_fs 0 +{% if wtd_srv_rsnapshot_conf.one_fs is defined %} +one_fs {{ wtd_srv_rsnapshot_conf.one_fs }} {% endif %} -# The include and exclude parameters, if defined enabled, simply get passed directly -# to rsync. if defined you have multiple include/exclude patterns, put each one on a -# separate line. Please look up the --include and --exclude options in the -# rsync man page for more details on how to specif definedy file name patterns. -# -{% if item.include is defined %} -{% for e in item.include %} -include {{ e.value }} -{% endfor %} -{% else %} -{% if wtd_rsnapshot_config_include is defined %} -{% for e in wtd_rsnapshot_config_include %} -include {{ e.value }} -{% endfor %} -{% else %} -#include ??? -#include ??? -{% endif %} -{% endif %} +# Include / Exclude -{% if item.exclude is defined %} -{% for e in item.exclude %} -exclude {{ e.value }} +{% if wtd_srv_rsnapshot_conf.includes is defined %} +{% for include in wtd_srv_rsnapshot_conf.includes %} +include {{ include }} {% endfor %} -{% else %} -{% if wtd_rsnapshot_config_exclude is defined %} -{% for e in wtd_rsnapshot_config_exclude %} -exclude {{ e.value }} -{% endfor %} -{% else %} -#exclude ??? -#exclude ??? -{% endif %} {% endif %} - -# The include_file and exclude_file parameters, if defined enabled, simply get -# passed directly to rsync. Please look up the --include-from and -# --exclude-from options in the rsync man page for more details. -# -{% if item.include_file is defined %} -{% for e in item.include_file %} -include_file {{ e.value }} +{% if wtd_srv_rsnapshot_conf.excludes is defined %} +{% for exclude in wtd_srv_rsnapshot_conf.excludes %} +exclude {{ exclude }} {% endfor %} -{% else %} -{% if wtd_rsnapshot_config_include_file is defined %} -{% for e in wtd_rsnapshot_config_include_file %} -include_file {{ e.value }} -{% endfor %} -{% else %} -#include_file /path/to/include/file -{% endif %} {% endif %} - -{% if item.exclude_file is defined %} -{% for e in item.exclude_file %} -exclude_file {{ e.value }} -{% endfor %} -{% else %} -{% if wtd_rsnapshot_config_exclude_file is defined %} -{% for e in wtd_rsnapshot_config_exclude_file %} -exclude_file {{ e.value }} +{% if wtd_srv_rsnapshot_conf.include_files is defined %} +{% for include_file in wtd_srv_rsnapshot_conf.include_files %} +include_file {{ include_file }} {% endfor %} -{% else %} -#exclude_file /path/to/exclude/file {% endif %} +{% if wtd_srv_rsnapshot_conf.exclude_files is defined %} +{% for exclude_file in wtd_srv_rsnapshot_conf.exclude_files %} +exclude_file {{ exclude_file }} +{% endfor %} {% endif %} -# if defined your version of rsync supports --link-dest, consider enabling this. -# This is the best way to support special files (Fif definedOs, etc) cross-platform. -# The default is 0 (off). -# -{% if item.link_dest is defined or wtd_rsnapshot_config_link_dest is defined %} -link_dest {{ item.link_dest | default(wtd_rsnapshot_config_link_dest) }} -{% else %} -#link_dest 0 -{% endif %} - -# When sync_first is enabled, it changes the default behaviour of rsnapshot. -# Normally, when rsnapshot is called with its lowest interval -# (i.e.: "rsnapshot alpha"), it will sync files AND rotate the lowest -# intervals. With sync_first enabled, "rsnapshot sync" handles the file sync, -# and all interval calls simply rotate files. See the man page for more -# details. The default is 0 (off). -# -{% if item.sync_first is defined or wtd_rsnaphsot_config_sync_first is defined %} -sync_first {{ item.sync_first }} -{% else %} -#sync_first 0 -{% endif %} +# Behaviour -# if defined enabled, rsnapshot will move the oldest directory for each interval -# to [interval_name].delete, then it will remove the lockfile and delete -# that directory just before it exits. The default is 0 (off). -# -{% if item.use_lazy_deletes is defined %} -use_lazy_deletes {{ item.use_lazy_deleted }} -{% else %} -{% if wtd_rsnapshot_config_use_lazy_deletes is defined %} -use_lazy_deletes {{ wtd_rsnapshot_config_use_lazy_deletes }} -{% else %} -#use_lazy_deletes 0 +{% if wtd_srv_rsnapshot_conf.ink_dest is defined %} +link_dest {{ wtd_srv_rsnapshot_conf.link_dest }} {% endif %} +{% if wtd_srv_rsnapshot_conf.sync_first is defined %} +sync_first {{ wtd_srv_rsnapshot_conf.sync_first }} {% endif %} - -# Number of rsync re-tries. if defined you experience any network problems or -# network card issues that tend to cause ssh to fail with errors like -# "Corrupted MAC on input", for example, set this to a non-zero value -# to have the rsync operation re-tried. -# -{% if item.rsync_numtries is defined %} -rsync_numtries {{ item.rsync_numtries }} -{% else %} -{% if wtd_rsnapshot_config_rsync_numtries is defined %} -rsync_numtries {{ wtd_rsnapshot_config_rsync_numtries }} -{% else %} -#rsync_numtries 0 +{% if wtd_srv_rsnapshot_conf.use_lazy_deletes is defined %} +use_lazy_deletes {{ wtd_srv_rsnapshot_conf.use_lazy_deletes }} {% endif %} +{% if wtd_srv_rsnapshot_conf.rsync_numtries is defined %} +rsync_numtries {{ wtd_srv_rsnapshot_conf.rsync_numtries }} {% endif %} -# LVM parameters. Used to backup with creating lvm snapshot before backup -# and removing it after. This should ensure consistency of data in some special -# cases -# -# LVM snapshot(s) size (lvcreate --size option). -# -{% if item.linux_lvm_snapshotsize is defined %} -linux_lvm_snapshotsize {{ item.linux_lvm_snapshotsize }} -{% else %} -{% if wtd_rsnapshot_config_linux_lvm_snapshotsize is defined %} -linux_lvm_snapshotsize {{ wtd_rsnapshot_config_linux_lvm_snapshotsize }} -{% else %} -#linux_lvm_snapshotsize 100M -{% endif %} -{% endif %} +# LVM -# Name to be used when creating the LVM logical volume snapshot(s). -# -{% if item.linux_lvm_snapshotname is defined %} -linux_lvm_snapshotname {{ item.linux_lvm_snahshotname }} -{% else %} -{% if wtd_rsnapshot_config_linux_lvm_snapshotname is defined %} -linux_lvm_snapshotname {{ wtd_rsnapshot_config_linux_lvm_snapshotname }} -{% else %} -#linux_lvm_snapshotname rsnapshot +{% if wtd_srv_rsnapshot_conf.linux_lvm_snapshotsize is defined %} +linux_lvm_snapshotsize {{ wtd_srv_rsnapshot_conf.linux_lvm_snapshotsize }} {% endif %} +{% if wtd_srv_rsnapshot_conf.linux_lvm_snapshotname is defined %} +linux_lvm_snapshotname {{ wtd_srv_rsnapshot_conf.linux_lvm_snapshotname }} {% endif %} - -# Path to the LVM Volume Groups. -# -{% if item.linux_lvm_vgpath is defined %} -linux_lvm_vgpath {{ item.linux_lvm_vgpath }} -{% else %} -{% if wtd_rsnapshot_config_linux_lvm_vgpath is defined %} -linux_lvm_vgpath {{ wtd_rsnapshot_config_linux_lvm_vgpath }} -{% else %} -#linux_lvm_vgpath /dev +{% if wtd_srv_rsnapshot_conf.linux_lvm_vgpath is defined %} +linux_lvm_vgpath {{ wtd_srv_rsnapshot_conf.linux_lvm_vgpath }} {% endif %} +{% if wtd_srv_rsnapshot_conf.linux_lvm_mountpath is defined %} +linux_lvm_mountpath {{ wtd_srv_rsnapshot_conf.linux_lvm_mountpath }} {% endif %} -# Mount point to use to temporarily mount the snapshot(s). -# -{% if item.linux_lvm_mountpath is defined %} -linux_lvm_mountpath {{ item.linux_lvm_mountpath }} -{% else %} -{% if wtd_rsnapshot_config_linux_lvm_mountpath is defined %} -linux_lvm_mountpath {{ wtd_rsnapshot_config_linux_lvm_mountpath }} -{% else %} -#linux_lvm_mountpath /path/to/mount/lvm/snapshot/during/backup +############################################ +# BACKUP / SCRIPTS / EXEC # +############################################ + +{% for backup in wtd_srv_rsnapshot_conf.backups %} +{% if backup.comment is defined %} +# {{ backup.comment }} {% endif %} +{% if backup.exec is defined %} +backup_exec {{ backup.exec }} {% endif %} - -############################### -### BACKUP POINTS / SCRIPTS ### -############################### - -# LOCALHOST - -{% if item.backups is defined %} -{% for e in item.backups %} -backup {{ e.src }} {{ e.target }} -{% endfor %} -{% else %} -# LOCALHOST -backup /home/ localhost/ -backup /etc/ localhost/ -backup /usr/local/ localhost/ -#backup /var/log/rsnapshot localhost/ -#backup /etc/passwd localhost/ -#backup /home/foo/My Documents/ localhost/ -#backup /foo/bar/ localhost/ one_fs=1, rsync_short_args=-urltvpog -#backup_script /usr/local/bin/backup_pgsql.sh localhost/postgres/ -# You must set linux_lvm_* parameters below before using lvm snapshots -#backup lvm://vg0/xen-home/ lvm-vg0/xen-home/ - -# EXAMPLE.COM -#backup_exec /bin/date "+ backup of example.com started at %c" -#backup root@example.com:/home/ example.com/ +rsync_long_args=--bwlimit=16,exclude=core -#backup root@example.com:/etc/ example.com/ exclude=mtab,exclude=core -#backup_exec ssh root@example.com "mysqldump -A > /var/db/dump/mysql.sql" -#backup root@example.com:/var/db/dump/ example.com/ -#backup_exec /bin/date "+ backup of example.com ended at %c" - -# CVS.SOURCEFORGE.NET -#backup_script /usr/local/bin/backup_rsnapshot_cvsroot.sh rsnapshot.cvs.sourceforge.net/ - -# RSYNC.SAMBA.ORG -#backup rsync://rsync.samba.org/rsyncftp/ rsync.samba.org/rsyncftp/ +{% if backup.script is defined %} +backup_script {{ backup.script }} {{ backup.dest }} +{% endif %} +{% if backup.src is defined %} +backup {{ backup.src }} {{ backup.dest }} {% if backup.options is defined %}{{ backup.options }}{% endif %} {% endif %} +{% endfor %} diff --git a/templates/rsnapshot.service.j2 b/templates/rsnapshot.service.j2 new file mode 100644 index 0000000..16b8951 --- /dev/null +++ b/templates/rsnapshot.service.j2 @@ -0,0 +1,13 @@ +{{ ansible_managed | comment }} + +[Unit] +Description=rsnapshot backup service + +[Service] +Type=oneshot +Nice=19 +IOSchedulingClass=idle +ExecStart=/usr/bin/rsnapshot -c /etc/rsnapshot.conf %I + +[Install] +WantedBy=multi-user.target diff --git a/templates/rsnapshot.timer.j2 b/templates/rsnapshot.timer.j2 index 3a998a6..6d9939f 100644 --- a/templates/rsnapshot.timer.j2 +++ b/templates/rsnapshot.timer.j2 @@ -1,11 +1,12 @@ +{{ ansible_managed | comment }} + [Unit] -Description=rsnapshot {{ item.0.name }} - {{ item.1.name }} backup +Description=rsnapshot backup {{ retain.name }} timer [Timer] -# 14:30 is the clock time when to start it -OnCalendar={{ item.1.time | default(wtd_rsnapshot_timer_time) }} +OnCalendar={{ retain.time }} Persistent=true -Unit=rsnapshot-{{ item.0.name }}@{{ item.1.name }}.service +Unit=rsnapshot@{{ retain.name }}.service [Install] WantedBy=timers.target diff --git a/templates/rsnapshot@.service.j2 b/templates/rsnapshot@.service.j2 deleted file mode 100644 index 17ae0e9..0000000 --- a/templates/rsnapshot@.service.j2 +++ /dev/null @@ -1,8 +0,0 @@ -[Unit] -Description=rsnapshot (%I) backup - -[Service] -Type=oneshot -Nice=19 -IOSchedulingClass=idle -ExecStart={{ item.execStart | default(wtd_rsnapshot_service_execStart) }}