Skip to content

Commit 961ab0c

Browse files
committed
Ansible Backup Scripts
0 parents  commit 961ab0c

File tree

14 files changed

+288
-0
lines changed

14 files changed

+288
-0
lines changed

.github/workflows/molecule.yml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
3+
name: ansible tests
4+
5+
on: # yamllint disable-line rule:truthy
6+
- push
7+
- pull_request
8+
9+
jobs:
10+
lint:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v3
15+
16+
- name: install dependencies
17+
run: >
18+
pip3 install
19+
ansible
20+
ansible-lint
21+
yamllint
22+
23+
- name: run yamllint
24+
run: yamllint .
25+
26+
- name: run ansible-lint
27+
run: ansible-lint

LICENSE

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
BSD 3-Clause License
2+
3+
Copyright (c) 2022, virtUOS
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
1. Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
2. Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
3. Neither the name of the copyright holder nor the names of its
17+
contributors may be used to endorse or promote products derived from
18+
this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Ansible Role for Backups with Samba Share
2+
3+
This ansible role configures autofs to mount a samba share
4+
and configure a script to run regular backups.
5+
6+
## Role Variables
7+
8+
There are three required variables you need to set:
9+
10+
* `backup_smb_user`: the user that will access the backup cloud
11+
* `backup_smb_password`: the password for this user
12+
* `backup_script`: The shell commands to run for creating a backup
13+
14+
Have a look at the [defaults](defaults/main.yml) to see all variables and how to set them.
15+
16+
17+
## Example Playbook
18+
19+
Your playbook, could look like this:
20+
21+
```yaml
22+
- hosts: all
23+
become: true
24+
- role: uos.smb_backup
25+
vars:
26+
backup_smb_share: //smb.example.com/backup
27+
backup_smb_user: samba_user
28+
backup_smb_password: samba_user_password
29+
backup_script: |
30+
FILENAME="{{ backup_mountpoint }}/$(date '+%Y%m%d-%H%M%S').sql.gz"
31+
mysqldump -u root --no-data dbname | gzip > "${FILENAME}"
32+
```
33+
34+
## License
35+
36+
[BSD-3-Clause](LICENSE)

defaults/main.yml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
3+
## Samba user credentials
4+
# backup_smb_user:
5+
# backup_smb_password:
6+
7+
## Samba share and mount location
8+
# backup_smb_share: //smb.example.com/backup
9+
backup_mountpoint: /mnt/backup
10+
11+
## This is the 'OnCalendar' variable
12+
## used in the systemd timer
13+
backup_timer_string: "*-*-* 03:30:00"
14+
15+
## Days to keep backups before deleting them again
16+
backup_keep_days: 14
17+
18+
## The user that executes the backup service
19+
backup_user: root
20+
backup_group: "{{ backup_user }}"
21+
22+
## Backup and cleanup scripts
23+
# backup_script: |
24+
# FILENAME="{{ backup_mountpoint }}/$(date '+%Y%m%d-%H%M%S').sql.gz"
25+
# mysqldump -u root --no-data dbname | gzip > "${FILENAME}"
26+
27+
backup_cleanup: |
28+
find "{{ backup_mountpoint }}" -mtime "+{{ backup_keep_days }}" -delete

handlers/main.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
- name: Restart autofs
3+
ansible.builtin.service:
4+
name: autofs.service
5+
state: restarted
6+
7+
- name: Restart backup
8+
ansible.builtin.systemd:
9+
name: backup.timer
10+
state: restarted
11+
daemon_reload: true

meta/main.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
galaxy_info:
3+
role_name: smb_backup
4+
namespace: uos
5+
author: Lars Kiesow
6+
description: Simple samba based backup
7+
company: Osnabrück University
8+
license: BSD-3-Clause
9+
min_ansible_version: '0.1'
10+
platforms:
11+
- name: EL
12+
versions:
13+
- '8'
14+
- '9'
15+
- name: Debian
16+
versions:
17+
- all
18+
- name: Ubuntu
19+
versions:
20+
- all
21+
dependencies: []

tasks/main.yml

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
- name: Install dependencies
3+
ansible.builtin.package:
4+
name:
5+
- autofs
6+
- cifs-utils
7+
8+
- name: Configure autofs
9+
ansible.builtin.template:
10+
src: '{{ item }}'
11+
dest: /etc/{{ item }}
12+
mode: '0644'
13+
loop:
14+
- auto.master
15+
- auto.samba
16+
notify: Restart autofs
17+
18+
- name: Configure samba credentials
19+
ansible.builtin.template:
20+
src: .samba-credentials
21+
dest: /etc/.samba-credentials
22+
mode: '0600'
23+
owner: root
24+
group: root
25+
notify: Restart autofs
26+
27+
- name: Start and enable autofs service
28+
ansible.builtin.systemd:
29+
name: autofs.service
30+
state: started
31+
enabled: true
32+
33+
- name: Create script folder
34+
ansible.builtin.file:
35+
path: /opt/backup
36+
state: directory
37+
mode: '0755'
38+
owner: root
39+
group: root
40+
41+
- name: Create backup scripts
42+
ansible.builtin.template:
43+
src: '{{ item }}'
44+
dest: /opt/backup/{{ item }}
45+
mode: '0755'
46+
owner: root
47+
group: root
48+
loop:
49+
- backup
50+
- cleanup
51+
52+
- name: Install backup service
53+
ansible.builtin.template:
54+
src: '{{ item }}'
55+
dest: /etc/systemd/system/{{ item }}
56+
mode: '0644'
57+
loop:
58+
- backup.service
59+
- backup.timer
60+
notify: Restart backup
61+
62+
- name: Start backup timer
63+
ansible.builtin.service:
64+
name: backup.timer
65+
state: started
66+
enabled: true

templates/.samba-credentials

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
username={{ backup_smb_user }}
2+
password={{ backup_smb_password }}
3+
domain=SAMBA

templates/auto.master

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#
2+
# Sample auto.master file
3+
# This is a 'master' automounter map and it has the following format:
4+
# mount-point [map-type[,format]:]map [options]
5+
# For details of the format look at auto.master(5).
6+
#
7+
#/misc /etc/auto.misc
8+
#
9+
# NOTE: mounts done from a hosts map will be mounted with the
10+
# "nosuid" and "nodev" options unless the "suid" and "dev"
11+
# options are explicitly given.
12+
#
13+
#/net -hosts
14+
#
15+
# Include /etc/auto.master.d/*.autofs
16+
# To add an extra map using this mechanism you will need to add
17+
# two configuration items - one /etc/auto.master.d/extra.autofs file
18+
# (using the same line format as the auto.master file)
19+
# and a separate mount map (e.g. /etc/auto.extra or an auto.extra NIS map)
20+
# that is referred to by the extra.autofs file.
21+
#
22+
+dir:/etc/auto.master.d
23+
#
24+
# If you have fedfs set up and the related binaries, either
25+
# built as part of autofs or installed from another package,
26+
# uncomment this line to use the fedfs program map to access
27+
# your fedfs mounts.
28+
#/nfs4 /usr/sbin/fedfs-map-nfs4 nobind
29+
#
30+
# Include central master map if it can be found using
31+
# nsswitch sources.
32+
#
33+
# Note that if there are entries for /net or /misc (as
34+
# above) in the included master map any keys that are the
35+
# same will not be seen as the first read key seen takes
36+
# precedence.
37+
#
38+
+auto.master
39+
40+
/- /etc/auto.samba --ghost

templates/auto.samba

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ backup_mountpoint }} -fstype=cifs,vers=3.0,noserverino,credentials=/etc/.samba-credentials,dir_mode=0700,file_mode=0700 :{{ backup_smb_share }}

templates/backup

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/sh
2+
3+
set -eu
4+
5+
{{ backup_script }}

templates/backup.service

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[Unit]
2+
Description=Backup Service
3+
4+
[Service]
5+
Type=simple
6+
ExecStart=/opt/backup/backup
7+
ExecStartPost=/opt/backup/cleanup
8+
User={{ backup_user }}
9+
Group={{ backup_group }}

templates/backup.timer

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[Unit]
2+
Description=Backup Timer
3+
4+
[Timer]
5+
OnCalendar={{ backup_timer_string }}
6+
7+
[Install]
8+
WantedBy=multi-user.target

templates/cleanup

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
3+
set -eu
4+
{{ backup_cleanup }}

0 commit comments

Comments
 (0)