-
Notifications
You must be signed in to change notification settings - Fork 3
Add Database Backup Feature (Disabled by Default) and Use backup User for Multi-DB Support #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
NUZAT-TABASSUM
wants to merge
4
commits into
elan-ev:main
Choose a base branch
from
NUZAT-TABASSUM:database-backup-feature
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /home/tabassum/my_work/maria_db/opencast_mariadb |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,36 @@ | ||
| --- | ||
| # Yamllint configuration should be compatible with Ansible, | ||
| # see https://ansible.readthedocs.io/projects/lint/rules/yaml/#yamllint-configuration | ||
|
|
||
| extends: default | ||
|
|
||
| rules: | ||
| line-length: disable | ||
| comments: | ||
| # https://github.com/prettier/prettier/issues/6780 | ||
| min-spaces-from-content: 1 | ||
| # https://github.com/adrienverge/yamllint/issues/384 | ||
| comments-indentation: false | ||
| document-start: disable | ||
| # 160 chars was the default used by old E204 rule, but | ||
| # you can easily change it or disable in your .yamllint file. | ||
| line-length: | ||
| max: 200 | ||
| # We are adding an extra space inside braces as that's how prettier does it | ||
| # and we are trying not to fight other linters. | ||
| braces: | ||
| min-spaces-inside: 0 # yamllint defaults to 0 | ||
| max-spaces-inside: 1 # yamllint defaults to 0 | ||
| # key-duplicates: | ||
| # forbid-duplicated-merge-keys: true # not enabled by default | ||
| octal-values: | ||
| forbid-implicit-octal: true # yamllint defaults to false | ||
| forbid-explicit-octal: true # yamllint defaults to false | ||
| # quoted-strings: | ||
| # quote-type: double | ||
| # required: only-when-needed | ||
|
|
||
|
|
||
| ignore: | | ||
| venv/ | ||
| .roles/ | ||
| .cache/ | ||
| .github/ | ||
| venv/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| --- | ||
| - name: Fail if backup enabled but no output path given | ||
| ansible.builtin.fail: | ||
| msg: "database_backup_output_path must be set when database_backup_enabled = true" | ||
| when: | ||
| - database_backup_enabled | ||
| - database_backup_output_path | length == 0 | ||
|
|
||
| - name: Ensure backup OS user exists | ||
| ansible.builtin.user: | ||
| name: "{{ database_backup_owner }}" | ||
| state: present | ||
| system: true | ||
NUZAT-TABASSUM marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| when: | ||
| - database_backup_enabled | ||
| - database_backup_user != "root" | ||
|
|
||
| - name: Ensure MariaDB backup user exists for each database | ||
| community.mysql.mysql_user: | ||
| name: "{{ database_backup_user }}" | ||
| password: "{{ database_backup_user_password }}" | ||
| host: "localhost" | ||
| priv: "{{ item }}.*:SELECT,LOCK TABLES,SHOW VIEW,TRIGGER" | ||
| state: present | ||
| login_user: "{{ database_root_user }}" | ||
| login_password: "{{ database_root_password }}" | ||
| loop: "{{ database_backup_dbs }}" | ||
| when: database_backup_enabled | ||
| no_log: true | ||
|
|
||
| - name: Ensure backup output directory exists | ||
| ansible.builtin.file: | ||
| path: "{{ database_backup_output_path }}" | ||
| state: directory | ||
| owner: "{{ database_backup_owner }}" | ||
| group: "{{ database_backup_group }}" | ||
| mode: "0750" | ||
| when: database_backup_enabled | ||
|
|
||
| - name: Install backup script | ||
| ansible.builtin.template: | ||
| src: database-backup.sh.j2 | ||
| dest: "{{ database_backup_output_path }}/database-backup.sh" | ||
| owner: "{{ database_backup_owner }}" | ||
| group: "{{ database_backup_group }}" | ||
| mode: "0750" | ||
| when: database_backup_enabled | ||
|
|
||
| - name: Install systemd service unit | ||
| ansible.builtin.template: | ||
| src: database-backup.service.j2 | ||
| dest: /etc/systemd/system/database-backup.service | ||
| mode: "0644" | ||
| when: database_backup_enabled | ||
| register: database_backup_service_unit | ||
|
|
||
| - name: Install systemd timer unit | ||
| ansible.builtin.template: | ||
| src: database-backup.timer.j2 | ||
| dest: /etc/systemd/system/database-backup.timer | ||
| mode: "0644" | ||
| when: database_backup_enabled | ||
| register: database_backup_timer_unit | ||
|
|
||
| - name: Reload systemd daemon (if timers changed) | ||
| ansible.builtin.systemd: | ||
| daemon_reload: true | ||
| when: | ||
| - database_backup_enabled | ||
| - database_backup_service_unit.changed or database_backup_timer_unit.changed | ||
|
|
||
| - name: Ensure backup timer is enabled and running | ||
| ansible.builtin.systemd: | ||
| name: database-backup.timer | ||
| enabled: true | ||
| state: started | ||
| when: database_backup_enabled | ||
|
|
||
| # Restore database | ||
| - name: Install restore script | ||
| ansible.builtin.template: | ||
| src: database-restore.sh.j2 | ||
| dest: "{{ database_backup_output_path }}/database-restore.sh" | ||
| owner: "{{ database_backup_owner }}" | ||
| group: "{{ database_backup_group }}" | ||
| mode: "0750" | ||
| when: database_backup_enabled | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| [Unit] | ||
| Description=Opencast Database Backup | ||
| After=network.target | ||
| After=local-fs.target | ||
| After=remote-fs.target | ||
|
|
||
| [Service] | ||
| Type=oneshot | ||
| User={{ database_backup_owner }} | ||
| Group={{ database_backup_group }} | ||
| ExecStart={{ database_backup_output_path }}/database-backup.sh | ||
|
|
||
| [Install] | ||
| WantedBy=multi-user.target |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| DBUSER="{{ database_backup_user }}" | ||
| DBPASS="{{ database_backup_user_password }}" | ||
| OUTDIR="{{ database_backup_output_path }}" | ||
| KEEP={{ database_backup_keep }} | ||
| DBS=({{ database_backup_dbs | join(' ') }}) | ||
| TS=$(date +%Y%m%d-%H%M%S) | ||
|
|
||
| # Loop through each database name | ||
| for DB in "${DBS[@]}"; do | ||
| echo "Backing up $DB database to $OUTDIR/db-backup-${DB}-${TS}.sql.gz" | ||
|
|
||
| # Run pg_dump and compress into a .gz file | ||
| mysqldump -u "$DBUSER" -p"$DBPASS" "$DB" \ | ||
| | gzip > "${OUTDIR}/db-backup-${DB}-${TS}.sql.gz" | ||
|
|
||
| # Remove older dumps, keep only the newest $KEEP | ||
| ls -1t "${OUTDIR}/db-backup-${DB}-"*.sql.gz \ | ||
| | tail -n +$((KEEP + 1)) \ | ||
| | xargs -r rm -- | ||
| done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| [Unit] | ||
| Description=Run database backup daily | ||
|
|
||
| [Timer] | ||
| OnCalendar={{ database_backup_schedule }} | ||
| Persistent=true | ||
|
|
||
| [Install] | ||
| WantedBy=timers.target |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| #!/bin/bash | ||
|
|
||
| # Usage: ./database-restore.sh <database_name> <backup_file> | ||
|
|
||
| DB_NAME=$1 | ||
| BACKUP_FILE=$2 | ||
|
|
||
| if [ -z "$DB_NAME" ] || [ -z "$BACKUP_FILE" ]; then | ||
| echo "Usage: $0 <database_name> <backup_file>" | ||
| exit 1 | ||
| fi | ||
|
|
||
| if [ ! -f "$BACKUP_FILE" ]; then | ||
| echo "Error: Backup file $BACKUP_FILE does not exist." | ||
| exit 2 | ||
| fi | ||
|
|
||
| DB_USER="{{ database_root_user }}" | ||
| DB_PASS="{{ database_root_password }}" | ||
|
|
||
| echo "Restoring database $DB_NAME from $BACKUP_FILE..." | ||
|
|
||
| # Drop and recreate the database | ||
| mysql -u"$DB_USER" -p"$DB_PASS" -e "DROP DATABASE IF EXISTS \`$DB_NAME\`; CREATE DATABASE \`$DB_NAME\`;" | ||
|
|
||
| # Import the backup file | ||
| if [[ "$BACKUP_FILE" == *.gz ]]; then | ||
| gunzip -c "$BACKUP_FILE" | mysql -u"$DB_USER" "-p$DB_PASS" "$DB_NAME" | ||
| else | ||
| mysql -u"$DB_USER" "-p$DB_PASS" "$DB_NAME" < "$BACKUP_FILE" | ||
| fi | ||
|
|
||
|
|
||
| if [ $? -eq 0 ]; then | ||
| echo "Restore completed successfully." | ||
| else | ||
| echo "Restore failed." | ||
| exit 3 | ||
| fi |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.