Skip to content
Snippets Groups Projects
Commit c5768f2a authored by Hannes Braun's avatar Hannes Braun :upside_down:
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
# role_simple_backup
## Name
Place simple backup script and configuration file on host.
## Description
An ansible role to place a simple backup script (using rsync) and a configuration file on a host. There is no automation included. See https://git.slub-dresden.de/slub-referat-2.5/ansible/role-systemd-timer for that.
## Installation
Either refer to the role via a requirements.yml, dependecies inside a meta.yml or via cloning the repository.
**Requirements.yml - Example**
```yaml
---
roles:
- name: role_simple_backup
src: https://git.slub-dresden.de/ansible/referat25/role_simple_backup.git
scm: git
```
```bash
ansible-galaxy install -r requirements.yml
```
## Usage
Include role in a playbook and provide a valid configuration via variables.
For variables see `defaults/main.yml`.
**Example**
```yaml
---
- hosts: all
- roles:
role: role-configure-nftables
vars:
- simple_backup_config:
sources: # These directories will be backed up
- /var/log
- /home
excludes: # Files and directories matching these patterns will be excluded from the backup
- .cache
- .ssh
nfs_mount_path: /mnt/nfs/qucosa_ubl_mandant_01 # The path where the nfs will be mounted
nfs_server: "194.95.142.73" # The nfs server ip address
nfs_volume_path: /vol/qucosa_ubl_mandant_01 # The volume the nfs is placed on the nfs server
```
## License
See license file of repository.
[defaults]
roles_path = ../:
---
simple_backup_config:
sources: # These directories will be backed up
# - /var/log
# - /home
excludes: # Files and directories matching these patterns will be excluded from the backup
- .cache
- .ssh
nfs_mount_path: # The path where the nfs will be mounted
nfs_server: # The nfs server ip address
nfs_volume_path: # The volume the nfs is placed on the nfs server
#!/bin/bash
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$script_dir" || exit
set -o errexit
set -o nounset
set -o pipefail
##### READ CONFIGURATION #####
config_file="simple-backup.conf"
# Get nfs mount path
section="nfs"
key="path"
nfs_mount_path=$(awk -F= -v section="$section" -v key="$key" '
$0 ~ "\\[" section "\\]" { in_section=1; next }
$0 ~ /^\[.*\]/ { in_section=0 }
in_section && $1 ~ key { gsub(/^[ \t]+|[ \t]+$/, "", $2); print $2; exit }
' $config_file)
if test -z "$nfs_mount_path"; then
echo "No nfs volume path was provided"
exit 1
fi
# Get nfs server
section="nfs"
key="server"
nfs_server=$(awk -F= -v section="$section" -v key="$key" '
$0 ~ "\\[" section "\\]" { in_section=1; next }
$0 ~ /^\[.*\]/ { in_section=0 }
in_section && $1 ~ key { gsub(/^[ \t]+|[ \t]+$/, "", $2); print $2; exit }
' $config_file)
if test -z "$nfs_server"; then
echo "No nfs volume path was provided"
exit 1
fi
# Get nfs volume path
section="nfs"
key="volume"
nfs_volume_path=$(awk -F= -v section="$section" -v key="$key" '
$0 ~ "\\[" section "\\]" { in_section=1; next }
$0 ~ /^\[.*\]/ { in_section=0 }
in_section && $1 ~ key { gsub(/^[ \t]+|[ \t]+$/, "", $2); print $2; exit }
' $config_file)
if test -z "$nfs_volume_path"; then
echo "No nfs volume path was provided"
exit 1
fi
# Get source directories
section="files"
key="source"
source_directories=($(awk -F= -v section="$section" -v key="$key" '
$0 ~ "\\[" section "\\]" { in_section=1; next }
$0 ~ /^\[.*\]/ { in_section=0 }
in_section && $1 ~ key {
gsub(/^[ \t]+|[ \t]+$/, "", $2);
print $2
}
' $config_file))
if test -z "$source_directories"; then
echo "No source directory was provided"
exit 1
fi
# Get excluded directories
section="files"
key="exclude"
# Read the INI file and extract all values into an array
excludes=($(awk -F= -v section="$section" -v key="$key" '
$0 ~ "\\[" section "\\]" { in_section=1; next }
$0 ~ /^\[.*\]/ { in_section=0 }
in_section && $1 ~ key {
gsub(/^[ \t]+|[ \t]+$/, "", $2);
print $2
}
' $config_file))
##### MOUNTS NFS SHARE #####
if [ ! -d "$nfs_mount_path" ]; then
mkdir -p "$nfs_mount_path"
echo "Directory '$nfs_mount_path' created."
fi
mountpoint -q $nfs_mount_path || mount $nfs_server:$nfs_volume_path $nfs_mount_path
##### Backup #####
source_string=""
for source in "${source_directories[@]}"; do
source_string+="${source} "
done
exclude_string=""
for exclude in "${excludes[@]}"; do
exclude_string+="--exclude=${exclude} "
done
readonly BACKUP_DIR="${nfs_mount_path}/backups/${HOSTNAME}"
readonly DATETIME="$(date '+%Y-%m-%d_%H:%M:%S')"
readonly BACKUP_PATH="${BACKUP_DIR}/${DATETIME}"
readonly LATEST_LINK="${BACKUP_DIR}/latest"
mkdir -p "${BACKUP_DIR}"
rsync_cmd_str="rsync -avR --delete ${source_string} --link-dest ${LATEST_LINK} ${exclude_string} ${BACKUP_PATH}"
echo $rsync_cmd_str
$rsync_cmd_str
rm -rf "${LATEST_LINK}"
ln -s "${BACKUP_PATH}" "${LATEST_LINK}"
galaxy_info:
role_name: role_somple_backup
author: Hannes Braun
description: Installs and configures a simple backup scrit. No Systemd unit included
license: license (MIT)
min_ansible_version: 2.14.5
platforms:
- name: Debian
version:
- 12
galaxy_tags: []
dependencies: []
site.yml 0 → 100644
---
- name: Test role
hosts: "*"
vars:
simple_backup_config:
sources:
- /var/www/webroot
- /opt/sql-dump
excludes:
- .cache
- .ssh
nfs_mount_path:
nfs_server:
nfs_volume_path:
tasks:
- name: Execute role for test purposes
ansible.builtin.include_role:
name: role_simple_backup
---
- name: Create simple backup folder
ansible.builtin.file:
path: /opt/simple-backup
state: directory
become: true
- name: Copy simple backup script
ansible.builtin.copy:
src: files/opt/simple-backup/simple-backup.sh
dest: /opt/simple-backup/simple-backup.sh
owner: root
group: root
mode: "0644"
become: true
- name: Create simple backup config from template
ansible.builtin.template:
src: templates/opt/simple-backup/simple-backup.conf.j2
dest: /opt/simple-backup/simple-backup.conf
owner: root
group: root
mode: '0644'
become: true
[nfs]
path={{ simple_backup_config.nfs_mount_path }}
server={{ simple_backup_config.nfs_server }}
volume={{ simple_backup_config.nfs_volume_path }}
[files]
{% for item in simple_backup_config.sources %}
nfs={{ item }}
{% endfor %}
{% for item in simple_backup_config.excludes %}
exclude={{ item }}
{% endfor %}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment