From 423f8ce98925e8e85095173ce5466b9fed4229ed Mon Sep 17 00:00:00 2001 From: "E. S" Date: Mon, 19 May 2025 17:20:15 +0300 Subject: [PATCH] initial --- .gitignore | 1 + README.md | 5 ++ rlmariabackup | 201 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 207 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100755 rlmariabackup diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..757fee3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.idea \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..ec056a2 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# rlmariabackup + +# License + +BSD-2-Clause \ No newline at end of file diff --git a/rlmariabackup b/rlmariabackup new file mode 100755 index 0000000..70fa7ae --- /dev/null +++ b/rlmariabackup @@ -0,0 +1,201 @@ +#!/bin/sh + +set -e + +info() { + printf "\033[36m%s\033[0m\n" "$*" +} + +warn() { + printf "\033[33m%s\033[0m\n" "$*" +} + +error() { + printf "\033[91merror: %s\033[0m\n" "$*" +} + +die() { + error "$*" >&2 + exit 1 +} + +get_timestamp() { + date "+%Y%m%d_%H%M%S" +} + +save_environment_info() { + backup_dir="$1" + info_file="$backup_dir/backup_info.txt" + + { + echo "Backup created at: $(date)" + echo "Host: $(hostname)" + echo "MariaDB version: $(mariadb --version 2>/dev/null)" + echo "Operating System: $(uname -a)" + } > "$info_file" +} + +cleanup_old_backups() { + keep_count=$((MAX_BACKUPS - 1)) + + [ $keep_count -lt 1 ] && return + + cd "$BACKUP_DIR" + ls -dt */ 2>/dev/null | tail -n +$((keep_count + 1)) | xargs -r rm -rf +} + +do_backup() { + timestamp=$(get_timestamp) + backup_dir="$BACKUP_DIR/$timestamp" + + mkdir -p "$backup_dir" + + cmd="mariabackup --backup --target-dir='$backup_dir' --user='$MARIADB_USER' --password='$MARIADB_PASSWORD'" + [ "$COMPRESS" = "1" ] && cmd="$cmd --compress" + [ -n "$PARALLEL_THREADS" ] && cmd="$cmd --parallel=$PARALLEL_THREADS" + [ -n "$EXTRA_OPTIONS" ] && cmd="$cmd $EXTRA_OPTIONS" + + cleanup_old_backups + + info "Starting backup to $backup_dir" + eval "$cmd" 2>&1 | tee "$backup_dir/backup.log" + + save_environment_info "$backup_dir" + + info "Backup completed successfully" +} + +do_prepare() { + [ -z "$1" ] && die "Backup directory must be specified" + backup_dir="$1" + [ -d "$backup_dir" ] || die "Backup directory does not exist: $backup_dir" + + info "Preparing backup in $backup_dir" + mariabackup --prepare --target-dir="$backup_dir" + info "Backup preparation completed" +} + +do_restore() { + [ -z "$1" ] && die "Backup directory must be specified" + backup_dir="$1" + [ -d "$backup_dir" ] || die "Backup directory does not exist: $backup_dir" + + cat << EOF +WARNING: Before restoring, ensure that: +1. The backup has been prepared using 'prepare' command +2. MariaDB service is stopped +3. The data directory is empty + +To restore, run the following commands: + +# Stop MariaDB service +systemctl stop mariadb # or equivalent for your system + +# Backup and prepare data directory +mv /var/lib/mysql /var/lib/mysql.bak +mkdir /var/lib/mysql + +# Restore backup +mariabackup --copy-back --target-dir='$backup_dir' --datadir=/var/lib/mysql + +# Fix permissions +chown -R mysql:mysql /var/lib/mysql + +# Start MariaDB service +systemctl start mariadb # or equivalent for your system + +# Verify the service is running +systemctl status mariadb + +Note: For FreeBSD, use 'service mysql start/stop' instead of systemctl commands +EOF +} + +do_grants() { + # Check if MARIADB_USER and MARIADB_PASSWORD are provided + [ -z "$MARIADB_USER" ] && die "MARIADB_USER is required for grants command (use -u or --user)" + [ -z "$MARIADB_PASSWORD" ] && die "MARIADB_PASSWORD is required for grants command (use -p or --password)" + + cat << EOF +-- Run these commands as root in MariaDB to set up backup user: +CREATE USER '$MARIADB_USER'@'localhost' IDENTIFIED BY '$MARIADB_PASSWORD'; +GRANT RELOAD, PROCESS, LOCK TABLES, BINLOG MONITOR ON *.* TO '$MARIADB_USER'@'localhost'; +GRANT REPLICATION CLIENT ON *.* TO '$MARIADB_USER'@'localhost'; +FLUSH PRIVILEGES; +EOF +} + +show_help() { + cat << EOF +USAGE: $0 [OPTIONS] [ARGS] + +COMMANDS: + backup Create a new backup + prepare Prepare a backup for restoration + restore Show instructions for restoring a backup + grants Show SQL commands to create backup user + help Show this help message + +OPTIONS: + -u, --user MariaDB backup user (required for backup and grants) + -p, --password MariaDB backup user password (required for backup and grants) + -d, --backup-dir Directory to store backups (required for backup) + -m, --max-backups Maximum number of backups to keep (default: 3) + -t, --threads Number of parallel threads (default: 4) + -c, --compress <0|1> Enable/disable backup compression (default: 1) + -e, --extra-options Additional options to pass to mariabackup + +EXAMPLES: + $0 -u backupuser -p secret -d /backups backup + $0 prepare /backups/20240101_120000 + $0 restore /backups/20240101_120000 + $0 -u backupuser -p secret grants +EOF +} + +# check required utilities +for cmd in mariabackup mariadb; do + command -v "$cmd" >/dev/null 2>&1 || die "$cmd not found in PATH" +done + +MAX_BACKUPS=3 +PARALLEL_THREADS=4 +COMPRESS=1 +EXTRA_OPTIONS= +WORK_DIR= +COMMAND= +while [ $# -gt 0 ]; do + case "$1" in + -u|--user) MARIADB_USER="$2"; shift ;; + -p|--password) MARIADB_PASSWORD="$2"; shift ;; + -d|--backup-dir) BACKUP_DIR="$2"; shift ;; + -m|--max-backups) MAX_BACKUPS="$2"; shift ;; + -t|--threads) PARALLEL_THREADS="$2"; shift ;; + -c|--compress) COMPRESS="$2"; shift ;; + -e|--extra-options) EXTRA_OPTIONS="$2"; shift ;; + -h|--help) COMMAND=help ;; + backup|grants|help) COMMAND="$1" ;; + prepare|restore) + COMMAND="$1" + WORK_DIR="$2" + ;; + *) + die "Unknown option: $1" + ;; + esac + shift +done + +if [ "$COMMAND" = "backup" ]; then + [ -z "$MARIADB_USER" ] && die "MARIADB_USER is required (use -u or --user)" + [ -z "$MARIADB_PASSWORD" ] && die "MARIADB_PASSWORD is required (use -p or --password)" + [ -z "$BACKUP_DIR" ] && die "BACKUP_DIR is required (use -d or --backup-dir)" +fi + +case "$COMMAND" in + backup) do_backup ;; + prepare) do_prepare "$WORK_DIR" ;; + restore) do_restore "$WORK_DIR" ;; + grants) do_grants ;; + *) show_help ;; +esac