start mysqlbackup installer

master
Harold Paulson 2020-10-08 14:05:53 -07:00
commit 76818f9c1e
5 changed files with 79 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.DS_Store

2
crontab.in Normal file
View File

@ -0,0 +1,2 @@
# backup mysql just before midnight snaps
50 23 * * * root /usr/local/bin/mysqlbackup

7
dot.my.cnf Normal file
View File

@ -0,0 +1,7 @@
[client]
user=root
password=
[mysqldump]
user=root
password=

26
install.sh Executable file
View File

@ -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 <<EOF >> /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

43
mysqlbackup Executable file
View File

@ -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"