Add db-backup role

This commit is contained in:
Przemek Grondek 2023-11-20 03:35:20 +01:00
parent 0b2e37c5fa
commit db6703d593
9 changed files with 151 additions and 0 deletions

View File

@ -26,6 +26,7 @@
- hosts: uatu.lan
roles:
- db-backup
- ssh
- ubuntu
- docker

View File

@ -0,0 +1,23 @@
#!/usr/bin/env bash
set -x
HOST=192.168.50.100
USER=root
PASS=
DEST=/srv/backup/db/mysql
DATABASES=$(mysql -h $HOST -u $USER -p$PASS -s -N -e "SHOW DATABASES;")
DIR="${DEST}/$(date +"%F")"
mkdir -p "$DIR"
for db in $DATABASES; do
FILE="${DIR}/$db.sql.gz"
echo "backing up $db to $FILE"
[ "$db" != "information_schema" ] && [ "$db" != "mysql" ] && [ "$db" != "performance_schema" ] && [ "$db" != "sys" ] || continue
# Be sure to make one backup per day
[ -f $FILE ] && continue
mysqldump --single-transaction --routines --quick -h $HOST -u $USER -p$PASS -B "$db" | gzip > "$FILE"
done

View File

@ -0,0 +1,24 @@
#!/usr/bin/env bash
set -x
HOST=192.168.50.100
PORT=5432
USER=postgres
PASS=
DEST=/srv/backup/db/postgres
DATABASES=$(PGPASSWORD="$PASS" psql -h $HOST -p $PORT -U $USER -l -t | cut -d'|' -f1 | sed -e 's/ //g' -e '/^$/d')
DIR="${DEST}/$(date +"%F")"
mkdir -p "$DIR"
for db in $DATABASES; do
FILE="${DIR}/$db.sql.gz"
echo "backing up $db to $FILE"
[ "$db" != "postgres" ] && [ "$db" != "template0" ] && [ "$db" != "template1" ] || continue
# Be sure to make one backup per day
[ -f $FILE ] && continue
PGPASSWORD="$PASS" pg_dump --username=$USER --host=$HOST --port=$PORT "$db" | gzip > "$FILE"
done

View File

@ -0,0 +1,24 @@
#!/usr/bin/env bash
set -x
HOST=192.168.50.100
PORT=5433
USER=postgres
PASS=
DEST=/srv/backup/db/postgres
DATABASES=$(PGPASSWORD="$PASS" psql -h $HOST -p $PORT -U $USER -l -t | cut -d'|' -f1 | sed -e 's/ //g' -e '/^$/d')
DIR="${DEST}/$(date +"%F")"
mkdir -p "$DIR"
for db in $DATABASES; do
FILE="${DIR}/$db.sql.gz"
echo "backing up $db to $FILE"
[ "$db" != "postgres" ] && [ "$db" != "template0" ] && [ "$db" != "template1" ] || continue
# Be sure to make one backup per day
[ -f $FILE ] && continue
PGPASSWORD="$PASS" pg_dump --username=$USER --host=$HOST --port=$PORT "$db" | gzip > "$FILE"
done

View File

@ -0,0 +1,9 @@
---
- name: create db-backup account
become: yes
user:
name: "{{ account.name }}"
comment: "{{ account.comment }}"
system: yes
password_lock: yes
home: "{{ account.home }}"

View File

@ -0,0 +1,23 @@
- name: copy mysql backup
become: yes
copy:
src: "mysql-backup.sh"
dest: "{{ account.home }}/mysql-backup.sh"
owner: "{{ account.name }}"
mode: '0755'
- name: copy postgres backup
become: yes
copy:
src: "postgres-backup.sh"
dest: "{{ account.home }}/postgres-backup.sh"
owner: "{{ account.name }}"
mode: '0755'
- name: copy postgres backup
become: yes
copy:
src: "postgres-backup2.sh"
dest: "{{ account.home }}/postgres-backup2.sh"
owner: "{{ account.name }}"
mode: '0755'

View File

@ -0,0 +1,36 @@
---
- become: yes
block:
- name: install needed tools
apt:
update_cache: yes
pkg:
- postgresql-client-common
- mysql-client-8.0
- name: Add cron task for backup mysql
become: yes
ansible.builtin.cron:
user: "{{ account.name }}"
name: "Backup mysql"
minute: "0"
hour: "4"
job: "{{ account.home }}/mysql-backup.sh"
- name: Add cron task for backup postgres
become: yes
ansible.builtin.cron:
user: "{{ account.name }}"
name: "Backup postgres"
minute: "10"
hour: "4"
job: "{{ account.home }}/postgres-backup.sh"
- name: Add cron task for backup postgres2
become: yes
ansible.builtin.cron:
user: "{{ account.name }}"
name: "Backup postgres 2"
minute: "20"
hour: "4"
job: "{{ account.home }}/postgres-backup2.sh"

View File

@ -0,0 +1,6 @@
---
- import_tasks: account.yml
- import_tasks: copy-scripts.yml
- import_tasks: install.yml

View File

@ -0,0 +1,5 @@
---
account:
name: db-backup
comment: Database Backup account
home: /home/db-backup