Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5b22e7e784 | ||
|
|
cba507b077 | ||
|
|
0974c13751 | ||
|
|
617883a9a7 | ||
|
|
71a02e0f86 | ||
|
|
c4c8d6de7f | ||
|
|
8d96529d8f |
No files matched your search
@@ -0,0 +1,14 @@
|
|||||||
|
# ansible/roles/backup/defaults/main.yml
|
||||||
|
---
|
||||||
|
## Directories to include in the backup
|
||||||
|
backup_include_paths:
|
||||||
|
- /etc
|
||||||
|
- /opt/docker
|
||||||
|
|
||||||
|
## Directories to exclude from the backup
|
||||||
|
backup_exclude_paths: [] ## Nothing to exclude
|
||||||
|
|
||||||
|
## Restic retention policy
|
||||||
|
backup_retention_daily: 7
|
||||||
|
backup_retention_weekly: 4
|
||||||
|
backup_retention_monthly: 6
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
---
|
||||||
|
- name: Install restic
|
||||||
|
ansible.builtin.apt:
|
||||||
|
name: restic
|
||||||
|
state: present
|
||||||
|
|
||||||
|
- name: Initialize restic repository
|
||||||
|
environment:
|
||||||
|
RESTIC_REPOSITORY: "{{ vault_restic_repo_url }}"
|
||||||
|
RESTIC_PASSWORD: "{{ vault_restic_password }}"
|
||||||
|
AWS_ACCESS_KEY_ID: "{{ vault_aws_access_key }}"
|
||||||
|
AWS_SECRET_ACCESS_KEY: "{{ vault_aws_secret_key }}"
|
||||||
|
block:
|
||||||
|
- name: Check if already initialized
|
||||||
|
ansible.builtin.command: restic snapshots
|
||||||
|
register: restic_check
|
||||||
|
failed_when: false
|
||||||
|
changed_when: false
|
||||||
|
|
||||||
|
- name: Initialize repository
|
||||||
|
ansible.builtin.command: restic init
|
||||||
|
when: restic_check.rc != 0
|
||||||
|
|
||||||
|
- name: Deploy restic environment file
|
||||||
|
ansible.builtin.template:
|
||||||
|
src: restic-backup.env.j2
|
||||||
|
dest: /etc/restic-backup.env
|
||||||
|
owner: root
|
||||||
|
group: root
|
||||||
|
mode: "0600" ## only root can read this file
|
||||||
|
|
||||||
|
- name: Deploy restic backup script
|
||||||
|
ansible.builtin.template:
|
||||||
|
src: restic-backup.sh.j2
|
||||||
|
dest: /usr/local/bin/restic-backup.sh
|
||||||
|
owner: root
|
||||||
|
group: root
|
||||||
|
mode: "0700" ## only root can execute the script
|
||||||
|
|
||||||
|
- name: Deploy restic systemd service
|
||||||
|
ansible.builtin.template:
|
||||||
|
src: restic-backup.service.j2
|
||||||
|
dest: /etc/systemd/system/restic-backup.service
|
||||||
|
owner: root
|
||||||
|
group: root
|
||||||
|
mode: "0644"
|
||||||
|
|
||||||
|
- name: Deploy restic systemd timer
|
||||||
|
ansible.builtin.template:
|
||||||
|
src: restic-backup.timer.j2
|
||||||
|
dest: /etc/systemd/system/restic-backup.timer
|
||||||
|
owner: root
|
||||||
|
group: root
|
||||||
|
mode: "0644"
|
||||||
|
|
||||||
|
- name: Enable and start backup timer
|
||||||
|
ansible.builtin.systemd:
|
||||||
|
name: restic-backup.timer
|
||||||
|
enabled: yes
|
||||||
|
state: started
|
||||||
|
daemon_reload: yes
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
# Ansible managed
|
||||||
|
RESTIC_REPOSITORY="{{ vault_restic_repo_url }}"
|
||||||
|
RESTIC_PASSWORD="{{ vault_restic_password }}"
|
||||||
|
AWS_ACCESS_KEY_ID="{{ vault_aws_access_key }}"
|
||||||
|
AWS_SECRET_ACCESS_KEY="{{ vault_aws_secret_key }}"
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Run Restic Backup
|
||||||
|
After=network-online.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=oneshot
|
||||||
|
EnvironmentFile=/etc/restic-backup.env
|
||||||
|
ExecStart=/usr/local/bin/restic-backup.sh
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
## Ansible managed
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
# Source environment variables if running manually/outside Systemd
|
||||||
|
if [ -f /etc/restic-backup.env ]; then
|
||||||
|
# shellcheck source=/dev/null
|
||||||
|
. /etc/restic-backup.env
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Starting Restic Backup at $(date)"
|
||||||
|
|
||||||
|
## Run the backup using paths and exclusions defined as group_vars
|
||||||
|
restic backup \
|
||||||
|
{% for path in backup_include_paths %}
|
||||||
|
"{{ path }}" \
|
||||||
|
{% endfor %}
|
||||||
|
{% for path in backup_exclude_paths %}
|
||||||
|
--exclude "{{ path }}" \
|
||||||
|
{% endfor %}
|
||||||
|
--cleanup-cache
|
||||||
|
|
||||||
|
restic forget \
|
||||||
|
--prune \
|
||||||
|
--keep-daily {{ backup_retention_daily }} \
|
||||||
|
--keep-weekly {{ backup_retention_weekly }} \
|
||||||
|
--keep-monthly {{ backup_retention_monthly }}
|
||||||
|
|
||||||
|
echo "Restic backup completed at $(date)"
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Run Restic Backup
|
||||||
|
|
||||||
|
[Timer]
|
||||||
|
OnCalendar=*-*-* 04:00:00
|
||||||
|
Persistent=true
|
||||||
|
RandomizedDelaySec=1800
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=timers.target
|
||||||
@@ -39,9 +39,10 @@
|
|||||||
enabled: true
|
enabled: true
|
||||||
|
|
||||||
- name: Configure deployment user for Docker
|
- name: Configure deployment user for Docker
|
||||||
|
when: deployment_public_ssh_key is defined
|
||||||
become: true
|
become: true
|
||||||
block:
|
block:
|
||||||
- name: Add user to docker group
|
- name: Add deployment user to docker group
|
||||||
ansible.builtin.user:
|
ansible.builtin.user:
|
||||||
name: deployment
|
name: deployment
|
||||||
groups: docker
|
groups: docker
|
||||||
@@ -64,7 +65,7 @@
|
|||||||
state: present
|
state: present
|
||||||
gid: 9999
|
gid: 9999
|
||||||
|
|
||||||
- name: Create user
|
- name: Create appuser
|
||||||
ansible.builtin.user:
|
ansible.builtin.user:
|
||||||
name: appuser
|
name: appuser
|
||||||
state: present
|
state: present
|
||||||
|
|||||||
@@ -1,3 +0,0 @@
|
|||||||
---
|
|
||||||
users_setup_sysadmin: true
|
|
||||||
users_setup_deployment: false # opt-in: enable explicitly to use
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
---
|
|
||||||
- name: Create deployment user for CI/CD
|
|
||||||
ansible.builtin.user:
|
|
||||||
name: deployment
|
|
||||||
shell: /bin/bash
|
|
||||||
create_home: yes
|
|
||||||
become: true
|
|
||||||
|
|
||||||
- name: Add SSH public key for deployment user
|
|
||||||
ansible.posix.authorized_key:
|
|
||||||
user: deployment
|
|
||||||
state: present
|
|
||||||
key: "{{ deployment_public_ssh_key }}"
|
|
||||||
become: true
|
|
||||||
+39
-11
@@ -1,14 +1,42 @@
|
|||||||
---
|
---
|
||||||
- name: Configure sysadmin user
|
- name: Set up primary sysadmin user
|
||||||
ansible.builtin.import_tasks: sysadmin.yml
|
when: sysadmin_public_ssh_key is defined
|
||||||
when: users_setup_sysadmin | bool
|
become: true
|
||||||
|
block:
|
||||||
|
- name: Create sysadmin user
|
||||||
|
ansible.builtin.user:
|
||||||
|
name: "{{ sysadmin_user }}"
|
||||||
|
groups: sudo
|
||||||
|
append: yes
|
||||||
|
shell: /bin/bash
|
||||||
|
create_home: yes
|
||||||
|
|
||||||
- name: Configure deployment user
|
- name: Add SSH public key
|
||||||
ansible.builtin.import_tasks: deployment.yml
|
ansible.posix.authorized_key:
|
||||||
when: users_setup_deployment | bool
|
user: "{{ sysadmin_user }}"
|
||||||
|
state: present
|
||||||
|
key: "{{ sysadmin_public_ssh_key }}"
|
||||||
|
|
||||||
# opt in to feature flags / tasks with:
|
- name: Allow passwordless sudo
|
||||||
# roles:
|
ansible.builtin.copy:
|
||||||
# - role: studio.ansible.users
|
dest: /etc/sudoers.d/sudo-nopasswd
|
||||||
# vars:
|
content: "%sudo ALL=(ALL) NOPASSWD: ALL"
|
||||||
# users_setup_deployment: true
|
mode: "0440"
|
||||||
|
validate: /usr/sbin/visudo -cf %s
|
||||||
|
|
||||||
|
|
||||||
|
- name: Set up deployment user for CI/CD
|
||||||
|
when: deployment_public_ssh_key is defined
|
||||||
|
become: true
|
||||||
|
block:
|
||||||
|
- name: Create deployment user
|
||||||
|
ansible.builtin.user:
|
||||||
|
name: deployment
|
||||||
|
shell: /bin/bash
|
||||||
|
create_home: yes
|
||||||
|
|
||||||
|
- name: Add SSH public key
|
||||||
|
ansible.posix.authorized_key:
|
||||||
|
user: deployment
|
||||||
|
state: present
|
||||||
|
key: "{{ deployment_public_ssh_key }}"
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
---
|
|
||||||
- name: Create primary sysadmin user
|
|
||||||
ansible.builtin.user:
|
|
||||||
name: "{{ sysadmin_user }}"
|
|
||||||
groups: sudo
|
|
||||||
append: yes
|
|
||||||
shell: /bin/bash
|
|
||||||
create_home: yes
|
|
||||||
become: true
|
|
||||||
|
|
||||||
- name: Add SSH public key for sysadmin
|
|
||||||
ansible.posix.authorized_key:
|
|
||||||
user: "{{ sysadmin_user }}"
|
|
||||||
state: present
|
|
||||||
key: "{{ sysadmin_public_ssh_key }}"
|
|
||||||
become: true
|
|
||||||
|
|
||||||
- name: Allow passwordless sudo for sudo group
|
|
||||||
ansible.builtin.copy:
|
|
||||||
dest: /etc/sudoers.d/sudo-nopasswd
|
|
||||||
content: "%sudo ALL=(ALL) NOPASSWD: ALL"
|
|
||||||
mode: "0440"
|
|
||||||
validate: /usr/sbin/visudo -cf %s
|
|
||||||
become: true
|
|
||||||
Reference in new issue
Block a user