Advertisement
Guest User

Untitled

a guest
Aug 11th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.77 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #Variables
  4.  
  5. #MySQL User and Pass
  6. MYUSER='root'
  7. MYPASS='maStaQ7k#'
  8.  
  9. # DateTime stamp format that is used in the tar file names.
  10. STAMP=$( date +%Y-%m-%d_%H%M%S )
  11.  
  12. BACKUPDATE=$( date +%Y-%m-%d )
  13.  
  14. # Backups DIR name (NOT FILE PATH)
  15. BACKUPDIR="/data/backups/mysql_test"
  16.  
  17. # Delete backups older than 'n' Days
  18. OLDBACKUPS=7
  19.  
  20. # Enable/Disable Logging (This will just echo each stage the script reaches, for debugging purposes)
  21. LOGIT=1
  22.  
  23. HOSTNAME=$( hostname | cut -d. -f1 )
  24.  
  25. # *-------------------------* SCRIPT *-------------------------*
  26. # Set todays backup dir
  27.  
  28. if [ -z "$MYUSER" ] || [ -z "$MYPASS" ]; then
  29.     echo "[LOG] Epic fail, MYUSER and MYPASS not set. Fix it."
  30.     exit 1
  31. fi
  32.  
  33. if [ $LOGIT -eq 1 ]
  34. then
  35.    echo "[LOG] Starting MySQL Backup for $( hostname )"
  36. fi
  37.  
  38. BACKUPDATE=$( date +%d-%m-%Y )
  39. FINALDIR="$BACKUPDIR/$BACKUPDATE"
  40.  
  41. if [ $LOGIT -eq 1 ]
  42. then
  43.    echo "[LOG] Checking if backup folders exist, if not then create them."
  44. fi
  45.  
  46. if [ -d $BACKUPDIR ]
  47. then
  48.    echo -n < /dev/null
  49. else
  50.    mkdir "$BACKUPDIR"
  51.  
  52.    if [ $LOGIT -eq 1 ]
  53.    then
  54.       echo "[LOG] Created Directory: $BACKUPDIR"
  55.    fi
  56.  
  57. fi
  58.  
  59. if [ -d "$FINALDIR" ]
  60. then
  61.    echo -n < /dev/null
  62. else
  63.    mkdir "$FINALDIR"
  64.    
  65.    if [ $LOGIT -eq 1 ]
  66.    then
  67.       echo "[LOG] Created Directory: $FINALDIR"
  68.    fi
  69.  
  70. fi
  71.  
  72. if [ $OLDBACKUPS -lt 0 ]
  73. then
  74.    OLDBACKUPS=3
  75. fi
  76.  
  77. # Deletes backups that are 'n' days old
  78. if [ $LOGIT -eq 1 ]
  79. then
  80.    echo "[LOG] Removing backups older than $OLDBACKUPS days."
  81. fi
  82. OLDBACKUP=$( find $BACKUPDIR -type d -mtime +$OLDBACKUPS | grep -v -x "$BACKUPDIR" | xargs rm -rf )
  83.  
  84. # Run backup.
  85. $( mysqldump -u$MYUSER -p$MYPASS --single-transaction -A > $FINALDIR/$HOSTNAME.$STAMP.sql )
  86. # Compress it
  87. $( gzip -5 $FINALDIR/$HOSTNAME.$STAMP.sql )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement