Add db-backup role
This commit is contained in:
parent
0b2e37c5fa
commit
db6703d593
23
roles/db-backup/files/mysql-backup.sh
Executable file
23
roles/db-backup/files/mysql-backup.sh
Executable 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
|
24
roles/db-backup/files/postgres-backup.sh
Executable file
24
roles/db-backup/files/postgres-backup.sh
Executable 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
|
24
roles/db-backup/files/postgres-backup2.sh
Executable file
24
roles/db-backup/files/postgres-backup2.sh
Executable 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
|
9
roles/db-backup/tasks/account.yml
Normal file
9
roles/db-backup/tasks/account.yml
Normal 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 }}"
|
23
roles/db-backup/tasks/copy-scripts.yml
Normal file
23
roles/db-backup/tasks/copy-scripts.yml
Normal 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'
|
36
roles/db-backup/tasks/install.yml
Normal file
36
roles/db-backup/tasks/install.yml
Normal 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"
|
6
roles/db-backup/tasks/main.yml
Normal file
6
roles/db-backup/tasks/main.yml
Normal file
@ -0,0 +1,6 @@
|
||||
---
|
||||
- import_tasks: account.yml
|
||||
|
||||
- import_tasks: copy-scripts.yml
|
||||
|
||||
- import_tasks: install.yml
|
5
roles/db-backup/vars/main.yml
Normal file
5
roles/db-backup/vars/main.yml
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
account:
|
||||
name: db-backup
|
||||
comment: Database Backup account
|
||||
home: /home/db-backup
|
Loading…
Reference in New Issue
Block a user