Skip to content
Snippets Groups Projects
Verified Commit 743dd1fa authored by Evangelos Foutras's avatar Evangelos Foutras :smiley_cat:
Browse files

borg_client: refactor database related vars/tasks

Move backup-related variable defaults from the database roles into the
borg_client role. Also check group membership to guard installation of
database backup helper scripts.
parent 26808056
No related branches found
No related tags found
No related merge requests found
Pipeline #95617 passed
......@@ -76,12 +76,13 @@ or just a sub-directory:
### Mariadb
For Mariadb backups are made using mariabackup to `mysql_backup_dir`.Backups can are made and
restored using the `mariabackup` tool. See also [official MariaDB docs](https://mariadb.com/kb/en/full-backup-and-restore-with-mariabackup/).
For Mariadb backups are made using mariabackup to `backup_mysql_dir`. Backups
are made and can be restored using the `mariadb-backup` tool. See also
[official MariaDB docs](https://mariadb.com/kb/en/full-backup-and-restore-with-mariabackup/).
### PostgreSQL
For PostgreSQL backups are made using pg_dump to `postgres_backup_dir`.
For PostgreSQL backups are made using pg_dump to `backup_postgres_dir`.
Restoring backups can be done with `pg_restore`. See also [official PostgreSQL docs](https://www.postgresql.org/docs/current/app-pgrestore.html).
......
......@@ -7,3 +7,7 @@ backup_hosts:
dir: "~/backup/{{ inventory_hostname }}"
suffix: "-offsite"
borg_cmd: "/usr/bin/borg --remote-path=borg1"
backup_postgres_dir: /root/backup-postgres
backup_mysql_dir: /root/backup-mysql
backup_mysql_defaults: /root/.backup-my.cnf
......@@ -27,31 +27,26 @@
template: src=borg-backup.sh.j2 dest=/usr/local/bin/borg-backup{{ item['suffix'] }}.sh owner=root group=root mode=0755
loop: "{{ backup_hosts }}"
- name: Install postgres backup script
template: src=backup-postgres.sh.j2 dest=/usr/local/bin/backup-postgres.sh owner=root group=root mode=0755
when: postgres_backup_dir is defined
- name: Check whether postgres user exists
command: getent passwd postgres
register: check_postgres_user
ignore_errors: true
changed_when: check_postgres_user.stdout | length > 0
- name: Make postgres backup directory
file: path={{ postgres_backup_dir }} owner=root group=root mode=0755 state=directory
when: check_postgres_user is succeeded and postgres_backup_dir is defined
- name: Install mysql backup script
template: src=backup-mysql.sh.j2 dest=/usr/local/bin/backup-mysql.sh owner=root group=root mode=0755
when: mysql_backup_dir is defined
- name: Install mysql backup config
template: src=backup-my.cnf.j2 dest={{ mysql_backup_defaults }} owner=root group=root mode=0644
when: mysql_backup_defaults is defined
- name: Create mysql backup directory
file: path={{ mysql_backup_dir }} state=directory owner=root group=root mode=0755
when: mysql_backup_dir is defined
- name: Set up backup helpers for PostgreSQL databases
when: "'postgresql_servers' in group_names"
block:
- name: Install postgres backup script
template: src=backup-postgres.sh.j2 dest=/usr/local/bin/backup-postgres.sh owner=root group=root mode=0755
- name: Make postgres backup directory
file: path={{ backup_postgres_dir }} owner=root group=root mode=0755 state=directory
- name: Set up backup helpers for MariaDB databases
when: "'mysql_servers' in group_names"
block:
- name: Install mysql backup script
template: src=backup-mysql.sh.j2 dest=/usr/local/bin/backup-mysql.sh owner=root group=root mode=0755
- name: Install mysql backup config
template: src=backup-my.cnf.j2 dest={{ backup_mysql_defaults }} owner=root group=root mode=0644
- name: Create mysql backup directory
file: path={{ backup_mysql_dir }} state=directory owner=root group=root mode=0755
- name: Install systemd services for backup
template: src={{ item }}.j2 dest=/etc/systemd/system/{{ item }} owner=root group=root mode=0644
......
......@@ -2,8 +2,8 @@
set -e
mysql_opts="--defaults-file={{mysql_backup_defaults}}"
backupdir="{{mysql_backup_dir}}"
mysql_opts="--defaults-file={{ backup_mysql_defaults }}"
backupdir="{{ backup_mysql_dir }}"
[[ ! -d "$backupdir" ]] && mkdir -p "$backupdir"
rm -rf "${backupdir:?}"/*
......
......@@ -11,9 +11,9 @@ for db in "${DBLIST[@]}"; do
if [[ $db =~ template[01] ]]; then
continue;
fi
echo "Dumping $db to {{ postgres_backup_dir }}";
sudo -u postgres pg_dump --serializable-deferrable -Fc "$db" > "{{ postgres_backup_dir }}/$db.dump"
echo "Dumping $db to {{ backup_postgres_dir }}";
sudo -u postgres pg_dump --serializable-deferrable -Fc "$db" > "{{ backup_postgres_dir }}/$db.dump"
done
echo "Dumping globals to {{ postgres_backup_dir }}"
sudo -u postgres pg_dumpall --globals-only > "{{ postgres_backup_dir }}/globals.sql.dump"
echo "Dumping globals to {{ backup_postgres_dir }}"
sudo -u postgres pg_dumpall --globals-only > "{{ backup_postgres_dir }}/globals.sql.dump"
......@@ -23,6 +23,3 @@ mariadb_innodb_flush_log_at_trx_commit: '1'
mariadb_innodb_stats_sample_pages: '32'
mariadb_innodb_thread_concurrency: '8'
mariadb_innodb_file_per_table: true
mysql_backup_dir: '/root/backup-mysql'
mysql_backup_defaults: '/root/.backup-my.cnf'
......@@ -18,5 +18,3 @@ postgres_hosts4: []
postgres_hosts6: []
postgres_ssl_hosts4: []
postgres_ssl_hosts6: []
postgres_backup_dir: '/root/backup-postgres'
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment