daily pastebin goal
63%
SHARE
TWEET

Untitled

a guest Apr 21st, 2018 210 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. DBUSERNAME=root
  4. DBPASSWORD=***
  5. HOSTNAME=`/bin/hostname -s`
  6. BACKUPDIR="/var/lib/backup/mysql"
  7. KEEPDAYS=14
  8.  
  9. MESSAGE=""
  10.  
  11.   MESSAGE+="Dumping MySQL-Databases to "$BACKUPDIR"...
  12. "
  13. MESSAGE+="
  14. "`date +'%Y-%m-%d %H:%M:%S'`"
  15.  
  16. "
  17.  
  18.  
  19. if [ ! -d "$BACKUPDIR" ]; then
  20.   /bin/mkdir -p "$BACKUPDIR"
  21. fi
  22.  
  23.  
  24. if [ -d "$BACKUPDIR" ] &&  [ $KEEPDAYS != "" ]; then
  25.     OIFS=$IFS;
  26.     NOW=`date "+%Y%m%d"`
  27.    
  28.     for x in `find $BACKUPDIR -maxdepth 2 -mtime $KEEPDAYS -type d -regex '.*/[0-9]+_[0-9]+'`; do
  29.         FDATE=`echo "$x"|grep -oE "[0-9]{8}_[0-9]{6}$"`
  30.         MESSAGE+="Deleting old dump "$x"...
  31. "
  32.         if [ "$FDATE" != "" ]; then
  33.             /bin/rm $x/*.bz2
  34.             /bin/rmdir "$x"
  35.         fi
  36.        
  37.     done
  38.    
  39.     IFS=$OIFS
  40. fi
  41.  
  42. MESSAGE+="
  43. "
  44.  
  45.  
  46. DBLIST=`mktemp`
  47. /usr/bin/mysqlshow -u $DBUSERNAME -p$DBPASSWORD | awk '{print $2}' | grep -v Databases | sort >$DBLIST
  48.  
  49. for DB in `cat $DBLIST`; do
  50.     DATE=`date "+%Y%m%d_%H%M%S"`    
  51.     MESSAGE+="Dumping Database $DB...
  52. "
  53.     TBLLIST=`mktemp`
  54.     /usr/bin/mysql -u $DBUSERNAME -p$DBPASSWORD -D "$DB" -e "SHOW TABLES;" | awk '{print $1}' | grep -v "Tables_in_" | sort >$TBLLIST
  55.    
  56.    
  57.     for TBL in `cat $TBLLIST`; do          
  58.         DUMPDIR=$BACKUPDIR"/"$DB"/"$DATE;
  59.         if [ ! -d "$DUMPDIR" ]; then
  60.             /bin/mkdir -p "$DUMPDIR"
  61.         fi
  62.         DUMPFILE=$DUMPDIR"/"$DB"_"$TBL"_"$DATE".sql"
  63.         DUMPBZFILE="$DUMPFILE.bz2"
  64.         if [ -f "$DUMPBZFILE" ]; then
  65.             rm "$DUMPBZFILE"
  66.         fi
  67.         /usr/bin/mysqldump --opt -u $DBUSERNAME -p$DBPASSWORD $DB -t $TBL > "$DUMPFILE";    
  68.         /bin/bzip2 "$DUMPFILE"             
  69.     done
  70.     if [ -f "$TBLLIST" ]; then
  71.         /bin/rm "$TBLLIST";
  72.     fi
  73.        
  74.    
  75. done
  76.  
  77. if [ -f "$DBLIST" ]; then
  78.     rm "$DBLIST";
  79. fi
  80.  
  81.  
  82. MESSAGE+="
  83. "`date +'%Y-%m-%d %H:%M:%S'`"
  84.  
  85. ###########################################################################
  86.  
  87. "
  88.  
  89. echo "$MESSAGE"
  90. #echo "$MESSAGE"|/usr/bin/mail -a "From: backup" -s "Backup: $HOSTNAME" mail@email.de
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top