From 6373e1f70df16d4081ebe5d0926e55dc79061378 Mon Sep 17 00:00:00 2001 From: Glenn Date: Fri, 12 Jul 2024 14:35:24 +0200 Subject: [PATCH] Initial import --- README.md | 38 +++++ criteria/variables.json | 97 ++++++++++++ defaults/main.yml | 28 ++++ files/etc.cron.d.rsnapshot | 12 ++ handlers/main.yml | 2 + meta/main.yml | 52 +++++++ tasks/_configure.yml | 23 +++ tasks/_install.yml | 7 + tasks/_validation.yml | 34 +++++ tasks/main.yml | 14 ++ templates/etc.rsnapshot.conf.j2 | 260 ++++++++++++++++++++++++++++++++ tests/inventory | 2 + tests/test.yml | 5 + vars/main.yml | 2 + 14 files changed, 576 insertions(+) create mode 100644 README.md create mode 100644 criteria/variables.json create mode 100644 defaults/main.yml create mode 100644 files/etc.cron.d.rsnapshot create mode 100644 handlers/main.yml create mode 100644 meta/main.yml create mode 100644 tasks/_configure.yml create mode 100644 tasks/_install.yml create mode 100644 tasks/_validation.yml create mode 100644 tasks/main.yml create mode 100644 templates/etc.rsnapshot.conf.j2 create mode 100644 tests/inventory create mode 100644 tests/test.yml create mode 100644 vars/main.yml diff --git a/README.md b/README.md new file mode 100644 index 0000000..225dd44 --- /dev/null +++ b/README.md @@ -0,0 +1,38 @@ +Role Name +========= + +A brief description of the role goes here. + +Requirements +------------ + +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. + +Role Variables +-------------- + +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. + +Dependencies +------------ + +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. + +Example Playbook +---------------- + +Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: + + - hosts: servers + roles: + - { role: username.rolename, x: 42 } + +License +------- + +BSD + +Author Information +------------------ + +An optional section for the role authors to include contact information, or a website (HTML is not allowed). diff --git a/criteria/variables.json b/criteria/variables.json new file mode 100644 index 0000000..c239057 --- /dev/null +++ b/criteria/variables.json @@ -0,0 +1,97 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "rsnapshot Configuration", + "type": "object", + "properties": { + "rsnapshot_snapshot_root": { + "type": "string", + "description": "Root directory where all snapshots will be stored" + }, + "rsnapshot_retains": { + "type": "object", + "description": "Backup retains intervals", + "properties": { + "hourly": { "type": "integer", "minimum": 1 }, + "daily": { "type": "integer", "minimum": 1 }, + "weekly": { "type": "integer", "minimum": 1 }, + "monthly": { "type": "integer", "minimum": 1 } + }, + "required": ["hourly", "daily", "weekly", "monthly"] + }, + "rsnapshot_verbose": { + "type": "integer", + "minimum": 1, + "maximum": 5, + "description": "Verbosity level of the output (1-5)" + }, + "rsnapshot_loglevel": { + "type": "integer", + "minimum": 1, + "maximum": 5, + "description": "Log level of the output (1-5)" + }, + "rsnapshot_logfile": { + "type": "string", + "description": "Path to the logfile" + }, + "rsnapshot_link_dest": { + "type": "integer", + "enum": [0, 1], + "description": "Whether to use --link-dest with rsync (0 or 1)" + }, + "rsnapshot_sync_first": { + "type": "integer", + "enum": [0, 1], + "description": "Whether to sync first (0 or 1)" + }, + "rsnapshot_numtries": { + "type": "integer", + "minimum": 1, + "description": "Number of rsync retries" + }, + "rsnapshot_backup_scripts": { + "type": "array", + "description": "List of backup scripts", + "items": { "type": "string" } + }, + "rsnapshot_backups": { + "type": "array", + "description": "List of backup configurations", + "items": { + "type": "object", + "properties": { + "remote": { + "type": "object", + "properties": { + "user": { "type": "string", "description": "Remote user" }, + "host": { "type": "string", "description": "Remote host" }, + "paths": { + "type": "array", + "items": { "type": "string" }, + "description": "Paths to back up on the remote host" + } + }, + "required": ["user", "host", "paths"] + }, + "local": { + "type": "string", + "description": "Local path to store backups" + } + }, + "required": ["remote", "local"] + } + } + }, + "required": [ + "rsnapshot_snapshot_root", + "rsnapshot_retains", + "rsnapshot_verbose", + "rsnapshot_loglevel", + "rsnapshot_logfile", + "rsnapshot_link_dest", + "rsnapshot_sync_first", + "rsnapshot_numtries", + "rsnapshot_backup_scripts", + "rsnapshot_backups" + ] +} diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..a2faa85 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,28 @@ +--- +# defaults file for glenux.rsnapshot +# +rsnapshot_snapshot_root: /var/lib/rsnapshot + +rsnapshot_retains: + hourly: 6 + daily: 7 + weekly: 4 + monthly: 12 + +rsnapshot_verbose: 3 + +rsnapshot_loglevel: 4 + +rsnapshot_logfile: /var/log/rsnapshot.log + +rsnapshot_link_dest: 1 + +rsnapshot_sync_first: 1 + +rsnapshot_numtries: 2 + +rsnapshot_backup_scripts: [] + +rsnapshot_backups: [] + +# diff --git a/files/etc.cron.d.rsnapshot b/files/etc.cron.d.rsnapshot new file mode 100644 index 0000000..cb30510 --- /dev/null +++ b/files/etc.cron.d.rsnapshot @@ -0,0 +1,12 @@ +# This is a sample cron file for rsnapshot. +# The values used correspond to the examples in /etc/rsnapshot.conf. +# There you can also set the backup points and many other things. +# +# To activate this cron file you have to uncomment the lines below. +# Feel free to adapt it to your needs. + +0 */4 * * * root /usr/bin/rsnapshot sync && /usr/bin/rsnapshot hourly +30 3 * * * root /usr/bin/rsnapshot daily +0 3 * * 1 root /usr/bin/rsnapshot weekly +30 2 1 * * root /usr/bin/rsnapshot monthly + diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..bbf99be --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,2 @@ +--- +# handlers file for glenux.rsnapshot diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..c572acc --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,52 @@ +galaxy_info: + author: your name + description: your role description + company: your company (optional) + + # If the issue tracker for your role is not on github, uncomment the + # next line and provide a value + # issue_tracker_url: http://example.com/issue/tracker + + # Choose a valid license ID from https://spdx.org - some suggested licenses: + # - BSD-3-Clause (default) + # - MIT + # - GPL-2.0-or-later + # - GPL-3.0-only + # - Apache-2.0 + # - CC-BY-4.0 + license: license (GPL-2.0-or-later, MIT, etc) + + min_ansible_version: 2.1 + + # If this a Container Enabled role, provide the minimum Ansible Container version. + # min_ansible_container_version: + + # + # Provide a list of supported platforms, and for each platform a list of versions. + # If you don't wish to enumerate all versions for a particular platform, use 'all'. + # To view available platforms and versions (or releases), visit: + # https://galaxy.ansible.com/api/v1/platforms/ + # + # platforms: + # - name: Fedora + # versions: + # - all + # - 25 + # - name: SomePlatform + # versions: + # - all + # - 1.0 + # - 7 + # - 99.99 + + galaxy_tags: [] + # List tags for your role here, one per line. A tag is a keyword that describes + # and categorizes the role. Users find roles by searching for tags. Be sure to + # remove the '[]' above, if you add tags to this list. + # + # NOTE: A tag is limited to a single word comprised of alphanumeric characters. + # Maximum 20 tags per role. + +dependencies: [] + # List your role dependencies here, one per line. Be sure to remove the '[]' above, + # if you add dependencies to this list. diff --git a/tasks/_configure.yml b/tasks/_configure.yml new file mode 100644 index 0000000..05dedf7 --- /dev/null +++ b/tasks/_configure.yml @@ -0,0 +1,23 @@ +--- +- name: "Create directory for backups" + ansible.builtin.file: + path: "{{rsnapshot_snapshot_root}}" + state: directory + +- name: "Deploy configuration from template" + template: + src: etc.rsnapshot.conf.j2 + dest: /etc/rsnapshot.conf + mode: 0644 + owner: root + group: root + +- name: "Deploy cronjob from file" + ansible.builtin.copy: + src: etc.cron.d.rsnapshot + dest: /etc/cron.d/rsnapshot + mode: 0644 + owner: root + group: root + +# diff --git a/tasks/_install.yml b/tasks/_install.yml new file mode 100644 index 0000000..034d63d --- /dev/null +++ b/tasks/_install.yml @@ -0,0 +1,7 @@ +--- +- name: "Install backup tools with apt" + ansible.builtin.apt: + name: + - rsnapshot + - tmux + state: present diff --git a/tasks/_validation.yml b/tasks/_validation.yml new file mode 100644 index 0000000..94d176e --- /dev/null +++ b/tasks/_validation.yml @@ -0,0 +1,34 @@ +--- +# validate varible rsnapshot_backup_dir +- name: "Validate rsnapshot_backups structure" + ansible.utils.validate: + data: + rsnapshot_snapshot_root: "{{ rsnapshot_snapshot_root }}" + rsnapshot_backups: "{{ rsnapshot_backups }}" + rsnapshot_backup_scripts: "{{ rsnapshot_backup_scripts }}" + rsnapshot_retains: "{{ rsnapshot_retains }}" + rsnapshot_verbose: "{{ rsnapshot_verbose }}" + rsnapshot_loglevel: "{{ rsnapshot_loglevel }}" + rsnapshot_logfile: "{{ rsnapshot_logfile }}" + rsnapshot_link_dest: "{{ rsnapshot_link_dest }}" + rsnapshot_sync_first: "{{ rsnapshot_sync_first }}" + rsnapshot_numtries: "{{ rsnapshot_numtries }}" + criteria: + - "{{ lookup('file', 'criteria/variables.json') | from_json }}" + engine: ansible.utils.jsonschema + ignore_errors: true + register: result + +- name: Print the items that do not satisfy the desired state + ansible.builtin.debug: + msg: "{{ item['message'] }}" + loop: "{{ result['errors'] }}" + loop_control: + label: "{{ item['data_path'] }}" + when: "'errors' in result" + +- name: Stop if errors + ansible.builtin.meta: end_host + when: "'errors' in result" + +# diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..7154814 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,14 @@ +--- +# tasks file for glenux.rsnapshot + +# include task file validation.yml +- include_tasks: '_validation.yml' + +# include task file install.yml +- include_tasks: '_install.yml' + +# include task file configure.yml +- include_tasks: '_configure.yml' + + +# diff --git a/templates/etc.rsnapshot.conf.j2 b/templates/etc.rsnapshot.conf.j2 new file mode 100644 index 0000000..f42ad5d --- /dev/null +++ b/templates/etc.rsnapshot.conf.j2 @@ -0,0 +1,260 @@ +################################################# +# rsnapshot.conf - rsnapshot configuration file # +################################################# +# # +# PLEASE BE AWARE OF THE FOLLOWING RULES: # +# # +# This file requires tabs between elements # +# # +# Directories require a trailing slash: # +# right: /home/ # +# wrong: /home # +# # +################################################# + +####################### +# CONFIG FILE VERSION # +####################### + +config_version 1.2 + +########################### +# SNAPSHOT ROOT DIRECTORY # +########################### + +# All snapshots will be stored under this root directory. +# +snapshot_root {{rsnapshot_snapshot_root}} + +# If no_create_root is enabled, rsnapshot will not automatically create the +# snapshot_root directory. This is particularly useful if you are backing +# up to removable media, such as a FireWire or USB drive. +# +#no_create_root 1 + +################################# +# 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. +# +cmd_cp /bin/cp + +# uncomment this to use the rm program instead of the built-in perl routine. +# +cmd_rm /bin/rm + +# rsync must be enabled for anything to work. This is the only command that +# must be enabled. +# +cmd_rsync /usr/bin/rsync + +# Uncomment this to enable remote ssh backups over rsync. +# +cmd_ssh /usr/bin/ssh + +# Comment this out to disable syslog support. +# +cmd_logger /usr/bin/logger + +# Uncomment this to specify the path to "du" for disk usage checks. +# If you have an older version of "du", you may also want to check the +# "du_args" parameter below. +# +#cmd_du /usr/bin/du + +# Uncomment this to specify the path to rsnapshot-diff. +# +#cmd_rsnapshot_diff /usr/bin/rsnapshot-diff + +# Specify the path to a script (and any optional arguments) to run right +# before rsnapshot syncs files +# +#cmd_preexec /path/to/preexec/script + +# Specify the path to a script (and any optional arguments) to run right +# after rsnapshot syncs files +# +#cmd_postexec /path/to/postexec/script + +# Paths to lvcreate, lvremove, mount and umount commands, for use with +# Linux LVMs. +# +#linux_lvm_cmd_lvcreate /path/to/lvcreate +#linux_lvm_cmd_lvremove /path/to/lvremove +#linux_lvm_cmd_mount /bin/mount +#linux_lvm_cmd_umount /bin/umount + +######################################### +# BACKUP INTERVALS # +# Must be unique and in ascending order # +# i.e. hourly, daily, weekly, etc. # +######################################### + +retain hourly {{ rsnapshot_retains.hourly }} +retain daily {{ rsnapshot_retains.daily }} +retain weekly {{ rsnapshot_retains.weekly }} +retain monthly {{ rsnapshot_retains.monthly }} + +############################################ +# 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 +# +verbose {{ rsnapshot_verbose }} + +# Same as "verbose" above, but controls the amount of data sent to the +# logfile, if one is being used. The default is 3. +# +loglevel {{ rsnapshot_loglevel }} + +# If you enable this, data will be written to the file you specify. The +# amount of data written is controlled by the "loglevel" parameter. +# +logfile /var/log/rsnapshot.log + +# If enabled, rsnapshot will write a lockfile to prevent two instances +# from running simultaneously (and messing up the snapshot_root). +# If you enable this, make sure the lockfile directory is not world +# writable. Otherwise anyone can prevent the program from running. +# +lockfile /var/run/rsnapshot.pid + +# By default, rsnapshot check lockfile, check if PID is running +# and if not, consider lockfile as stale, then start +# Enabling this stop rsnapshot if PID in lockfile is not running +# +#stop_on_stale_lockfile 0 + +# Default rsync args. All rsync commands have at least these options set. +# +#rsync_short_args -a +#rsync_long_args --delete --numeric-ids --relative --delete-excluded + +# ssh has no args passed by default, but you can specify some here. +# +#ssh_args -p 22 + +# 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 your version of "du" doesn't support the -h flag, try -k flag instead. +# +#du_args -csh + +# If 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). +# +#one_fs 0 + +# The include and exclude parameters, if enabled, simply get passed directly +# to rsync. If 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 specify file name patterns. +# +#include ??? +#include ??? +#exclude ??? +#exclude ??? + +# The include_file and exclude_file parameters, if 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. +# +#include_file /path/to/include/file +#exclude_file /path/to/exclude/file + +# If your version of rsync supports --link-dest, consider enable this. +# This is the best way to support special files (FIFOs, etc) cross-platform. +# The default is 0 (off). +# +link_dest 1 + +# When sync_first is enabled, it changes the default behaviour of rsnapshot. +# Normally, when rsnapshot is called with its lowest interval +# (i.e.: "rsnapshot hourly"), 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). +# +sync_first 1 + +# If 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). +# +#use_lazy_deletes 0 + +# Number of rsync re-tries. If you experience any network problems or +# network card issues that tend to cause ssh to crap-out with +# "Corrupted MAC on input" errors, for example, set this to a non-zero +# value to have the rsync operation re-tried +# +rsync_numtries 2 + +# 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). +# +#linux_lvm_snapshotsize 100M + +# Name to be used when creating the LVM logical volume snapshot(s). +# +#linux_lvm_snapshotname rsnapshot + +# Path to the LVM Volume Groups. +# +#linux_lvm_vgpath /dev + +# Mount point to use to temporarily mount the snapshot(s). +# +#linux_lvm_mountpath /path/to/mount/lvm/snapshot/during/backup + +############################### +### BACKUP POINTS / SCRIPTS ### +############################### + +# 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_script /bin/date "+ backup of example.com started at %c" unused1 +#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_script ssh root@example.com "mysqldump -A > /var/db/dump/mysql.sql" unused2 +#backup root@example.com:/var/db/dump/ example.com/ +#backup_script /bin/date "+ backup of example.com ended at %c" unused9 + +# 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/ + +{% for backup in rsnapshot_backups %} +{% for remote_path in backup.remote.paths %} +backup {{backup.remote.user}}@{{backup.remote.host}}:{{ remote_path }} {{ backup.local }} +{% endfor %} +{% endfor %} + diff --git a/tests/inventory b/tests/inventory new file mode 100644 index 0000000..878877b --- /dev/null +++ b/tests/inventory @@ -0,0 +1,2 @@ +localhost + diff --git a/tests/test.yml b/tests/test.yml new file mode 100644 index 0000000..4f3929d --- /dev/null +++ b/tests/test.yml @@ -0,0 +1,5 @@ +--- +- hosts: localhost + remote_user: root + roles: + - glenux.rsnapshot diff --git a/vars/main.yml b/vars/main.yml new file mode 100644 index 0000000..513e4a2 --- /dev/null +++ b/vars/main.yml @@ -0,0 +1,2 @@ +--- +# vars file for glenux.rsnapshot