commit 76818f9c1e6001113f8bc3b521b5bdfdc27f3146 Author: Harold Paulson Date: Thu Oct 8 14:05:53 2020 -0700 start mysqlbackup installer diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/crontab.in b/crontab.in new file mode 100644 index 0000000..b212784 --- /dev/null +++ b/crontab.in @@ -0,0 +1,2 @@ +# backup mysql just before midnight snaps +50 23 * * * root /usr/local/bin/mysqlbackup diff --git a/dot.my.cnf b/dot.my.cnf new file mode 100644 index 0000000..1408593 --- /dev/null +++ b/dot.my.cnf @@ -0,0 +1,7 @@ +[client] +user=root +password= + +[mysqldump] +user=root +password= diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..7cf59cd --- /dev/null +++ b/install.sh @@ -0,0 +1,26 @@ +#!/bin/sh + +cp mysqlbackup /usr/local/bin/ +chmod 755 /usr/local/bin/mysqlbackup + +if [ ! -f /root/.my.cnf ]; then + read -s -p "Enter the root MySQL Password: " MYSQL_PASS; + cat <> /tmp/.my.cfg +[client] +user=root +password=${MYSQL_PASS} + +[mysqldump] +user=root +password=${MYSQL_PASS} +EOF +fi + +if [ ! -e /var/backup/mysql ]; then + mkdir -p /var/backup/mysql +fi + +if ! grep mysqlbackup /etc/crontab > /dev/null; then + cat crontab.in >> /etc/crontab +fi + diff --git a/mysqlbackup b/mysqlbackup new file mode 100755 index 0000000..a476992 --- /dev/null +++ b/mysqlbackup @@ -0,0 +1,43 @@ +#!/bin/sh + +# This script will backup all mysql databases into +# compressed file named after date, ie: /var/backup/mysql/2016-07-13.tar.gz + +# Setup variables used later + +# Create date suffix with "F"ull date format +suffix=$(date +%F) +# Retrieve all database names except information schemas. +dbs=$(/usr/local/bin/mysql --defaults-extra-file=/root/.my.cnf --batch --skip-column-names -e "SHOW DATABASES;" | grep -E -v "(information|performance)_schema") +# Create temporary directory with "-d" option +tmp=$(mktemp -d) +# Set output dir here. /var/backups/ is used by system, +# so intentionally used /var/backup/ for user backups. +outDir="/var/backup/mysql" +# Create output file name +out="$outDir/$suffix.tar.gz" + +# Actual script + +# Check if output directory exists +if [ ! -d "$outDir" ];then + # Create directory with parent ("-p" option) directories + mkdir -p "$outDir" +fi + +# Loop through all databases +for db in $dbs; do + # Dump database to temporary directory with file name same as database name + sql suffix + /usr/local/bin/mysqldump --defaults-extra-file=/root/.my.cnf --events --triggers --routines --set-gtid-purged=OFF --databases "$db" > "$tmp/$db.sql" +done + +# Go to tmp dir +cd $tmp + +# Compress all dumps with gzip, discard any output to /dev/null +tar -zcf "$out" * > "/dev/null" + +# Cleanup +cd "/tmp/" +rm -rf "$tmp" +