Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #Variables
- #MySQL User and Pass
- MYUSER='root'
- MYPASS='maStaQ7k#'
- # DateTime stamp format that is used in the tar file names.
- STAMP=$( date +%Y-%m-%d_%H%M%S )
- BACKUPDATE=$( date +%Y-%m-%d )
- # Backups DIR name (NOT FILE PATH)
- BACKUPDIR="/data/backups/mysql_test"
- # Delete backups older than 'n' Days
- OLDBACKUPS=7
- # Enable/Disable Logging (This will just echo each stage the script reaches, for debugging purposes)
- LOGIT=1
- HOSTNAME=$( hostname | cut -d. -f1 )
- # *-------------------------* SCRIPT *-------------------------*
- # Set todays backup dir
- if [ -z "$MYUSER" ] || [ -z "$MYPASS" ]; then
- echo "[LOG] Epic fail, MYUSER and MYPASS not set. Fix it."
- exit 1
- fi
- if [ $LOGIT -eq 1 ]
- then
- echo "[LOG] Starting MySQL Backup for $( hostname )"
- fi
- BACKUPDATE=$( date +%d-%m-%Y )
- FINALDIR="$BACKUPDIR/$BACKUPDATE"
- if [ $LOGIT -eq 1 ]
- then
- echo "[LOG] Checking if backup folders exist, if not then create them."
- fi
- if [ -d $BACKUPDIR ]
- then
- echo -n < /dev/null
- else
- mkdir "$BACKUPDIR"
- if [ $LOGIT -eq 1 ]
- then
- echo "[LOG] Created Directory: $BACKUPDIR"
- fi
- fi
- if [ -d "$FINALDIR" ]
- then
- echo -n < /dev/null
- else
- mkdir "$FINALDIR"
- if [ $LOGIT -eq 1 ]
- then
- echo "[LOG] Created Directory: $FINALDIR"
- fi
- fi
- if [ $OLDBACKUPS -lt 0 ]
- then
- OLDBACKUPS=3
- fi
- # Deletes backups that are 'n' days old
- if [ $LOGIT -eq 1 ]
- then
- echo "[LOG] Removing backups older than $OLDBACKUPS days."
- fi
- OLDBACKUP=$( find $BACKUPDIR -type d -mtime +$OLDBACKUPS | grep -v -x "$BACKUPDIR" | xargs rm -rf )
- # Run backup.
- $( mysqldump -u$MYUSER -p$MYPASS --single-transaction -A > $FINALDIR/$HOSTNAME.$STAMP.sql )
- # Compress it
- $( gzip -5 $FINALDIR/$HOSTNAME.$STAMP.sql )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement