From 4f5466efe1d2650fe29af37816b48f0e1a6e0aa7 Mon Sep 17 00:00:00 2001 From: Nils Date: Sat, 15 Oct 2022 11:30:01 +0200 Subject: [PATCH] mydumper --- README.md | 21 ++++++++++++ mydumper.sh | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 mydumper.sh diff --git a/README.md b/README.md index 7121cf9..109a347 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,27 @@ Used in Ansible Playbook: line: "MY_DIR={{ mysql_backup_dir }}" ``` +## MyDumper + +* `mydumper.sh` - Run `mydumper` and save either in folder `[NUMBER]A` or `[NUMBER]B`. + +Used in Ansible Playbook: +```yml +- name: MyDumper - Download script + ansible.builtin.get_url: + url: https://raw.githubusercontent.com/Cyclenerd/toolbox/master/mydumper.sh + dest: /root/mydumper.sh + mode: '0755' + owner: root + group: root + +- name: MyDumper - Change MY_DIR in script + ansible.builtin.lineinfile: + path: /root/mydumper.sh + regexp: '^MY_DIR' + line: "MY_DIR={{ mysql_backup_dir }}" +``` + ## License GNU Public License version 3. diff --git a/mydumper.sh b/mydumper.sh new file mode 100644 index 0000000..f995384 --- /dev/null +++ b/mydumper.sh @@ -0,0 +1,92 @@ +#!/bin/bash + +# Run mydumper and save either in folder [NUMBER]A or [NUMBER]B. +# If successful, delete the other folder (A or B). + +# Configuration file ~/.my.conf: +# +# [mydumper] +# host=127.0.0.1 +# user=root +# password=[PASSWORD] +# +# [myloader] +# host=127.0.0.1 +# user=root +# password=[PASSWORD] + +ME=$(basename "$0") +MY_DIR="/mnt/usb_wd_4tb_crypt/backup" + +function usage { + returnCode="$1" + echo + echo -e "Usage: + $ME -d -n ] [-h] + -d Backup directory (Default: '$MY_DIR') + -n Folder number + [-h] Displays help (this message)" + echo + exit "$returnCode" +} + +while getopts "d:n:h" opt; do + case $opt in + d) + MY_DIR="$OPTARG" + ;; + n) + MY_NUMBER="$OPTARG" + ;; + h) + usage 0 + ;; + *) + usage 1 + ;; + esac +done + +command -v mydumper >/dev/null 2>&1 || { echo >&2 "!!! Error !!! mydumper it's not installed."; exit 1; } + +if ((MY_NUMBER >= 1)); then + echo "Folder number: $MY_NUMBER" +else + echo "Folder number for backup missing" + exit 3 +fi + +if [ -d "$MY_DIR" ]; then + MY_DIR=${MY_DIR%/} + echo "Backup dir: $MY_DIR" +else + echo "Backup dir missing or directory '$MY_DIR' does not exists." + exit 2 +fi + +MY_BACKUP_DIR_A="$MY_DIR""/""$MY_NUMBER""A" +MY_BACKUP_DIR_B="$MY_DIR""/""$MY_NUMBER""B" + +if [ -d "$MY_BACKUP_DIR_A" ]; then + mkdir -p "$MY_BACKUP_DIR_B" + echo "Backup to: $MY_BACKUP_DIR_B" + if mydumper --outputdir "$MY_BACKUP_DIR_B"; then + echo "Success. Delete old backup." + rm -rf "$MY_BACKUP_DIR_A" + else + echo "!!! Error !!! Delete current and errored backup." + rm -rf "$MY_BACKUP_DIR_B" + exit 9 + fi +else + mkdir -p "$MY_BACKUP_DIR_A" + echo "Backup to: $MY_BACKUP_DIR_A" + if mydumper --outputdir "$MY_BACKUP_DIR_A"; then + echo "Success. Delete old backup." + rm -rf "$MY_BACKUP_DIR_B" + else + echo "!!! Error !!! Delete current and errored backup." + rm -rf "$MY_BACKUP_DIR_A" + exit 9 + fi +fi \ No newline at end of file