#!/bin/sh # ======================================================================= # USAGE: /home/hornetbzz/shells/my_sqldump.sh # Manual config snapshot from local to remote disk # Cronjob started: see /etc/crontab # Created by hornetbzz 27/07/2010 # shell file to be chmoded 0700 # shell file to run as root (NEEDS TO BE CHANGED (chroot jail) !!!) # ======================================================================= # ======================================================================= # TODO # chroot jail # ======================================================================= # ======================================================================= # changelog: # 28/09/2010. Shell release By Hornetbzz # ======================================================================= # ################### # INIT & DATA CONTROL # ################### PARAMETERS=$# [[ $PARAMETERS -ne 3 ]] && echo "Usage: my_sqldump.sh " && exit 1 # LOCAL DIRECTORY DEFAULT_LOCALDIR=/home/hornetbzz/tmp LOCAL_TARBALLS=$1 # REMOVE TRAILING "/" LOCAL_TARBALLS=${LOCAL_TARBALLS%/} # check dir depth for safety issues DEPTH=$(find $LOCAL_TARBALLS -printf '%d\n' | sort -n | tail -1) [[ ! -d $DEFAULT_LOCALDIR ]] && mkdir -p $DEFAULT_LOCALDIR [[ -z $1 ]] || [[ ! -d $1 ]] || [[ $DEPTH -lt 2 ]] && LOCAL_TARBALLS=$DEFAULT_LOCALDIR # MYSQL USER MUSER=$2 # mysql Username MPASS=$3 # mysql Password MHOST="127.0.0.1" # mysql Server Name: might need to replace by "localhost" # GZIP parameters SNAPSHOT_DATE=`date '+%d%m%y_%Hh%M'` GZIP="$(which gzip)" [[ -z $GZIP ]] && echo "gzip cmd not found, pls install and restart the shell" && exit 1 GZ_COMPRESSION_LEVEL="-9" # 1=low compression but fast, -9=high compression but slow, so u have to find the best compromise # ################ # MYSQL DUMP # ################ TARBALL="mysql" if [[ ! -z $MUSER ]] && [[ ! -z $MPASS ]] then # guess binary names MYSQL="$(which mysql)" MYSQLDUMP="$(which mysqldump)" [[ -z $MYSQL ]] || [[ -z $MYSQLDUMP ]] && echo "mysql cmd not found" && exit 1 # get all db names # NOTE : NO SPACE between -p and $MPASS DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')" # dump DBS and generate one tarball by database for db in $DBS do FILE="$LOCAL_TARBALLS/$TARBALL"_"$db-$SNAPSHOT_DATE.sql" $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db > $FILE $GZIP $GZ_COMPRESSION_LEVEL $FILE done [[ -d $LOCAL_TARBALLS/$TARBALL ]] && chown -R hornetbzz $LOCAL_TARBALLS/$TARBALL # Or generate ONE dump for all databases # FILE=$LOCAL_TARBALLS/$TARBALL/"mysql_dump".$SNAPSHOT_DATE".sql" # $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS --all-databases > $FILE && $GZIP -9 $FILE else echo "Mysql: No backup of databases have been performed (missing credentials)" fi exit 0