Advertisement
hakabe

Database and webroot backup via rsync

May 5th, 2017
901
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.52 KB | None | 0 0
  1. #!/bin/bash
  2. # Backup script from hakabe (c) 2017 - betro.hakala@gmail.com
  3. # Free to share and modify - just keep the author within the file
  4. # TODO's: Parse SQL databases and move them to the corresponding folders with the webroot
  5.  
  6. # Renice 19 makes the script consume as little CPU power as possible (we don't wanna make our webservices to lag)
  7. renice 19 -p $$
  8.  
  9. DEFRETAIN=10
  10. LOGFILE="$BACKUP_DIR/backup.log"
  11. BU_FILE_COUNT=0
  12. TIMESTAMP=$(date +"%F")
  13.  
  14. WEBDIR=/var/www
  15. BACKUP_DIR="/home/user/backup/$TIMESTAMP"
  16. BACKUP_SQL_DIR="$BACKUP_DIR/mysql"
  17. BACKUP_WWW_DIR="$BACKUP_DIR/www"
  18.  
  19. MYSQL="/usr/bin/mysql"
  20. MYSQLDUMP="/usr/bin/mysqldump"
  21. #RSYNC_HOST=x.x.x.x
  22. #RSYNC_LOGIN="user"
  23. #REMOTE_PATH="/home/user/"
  24. cpath=${BACKUP_DIR}
  25.         if [ -d $cpath ]
  26.         then
  27.                 filler="umin"
  28.         else
  29.                 echo Creating $cpath
  30.                 mkdir -p $cpath
  31.         fi
  32. #MOVING TO WEBROOT
  33. cd ${WEBDIR}/
  34.  
  35.  
  36. TODAY=`date`
  37. BU_FILE_COUNT=0
  38. suffix=$(date +%m-%d-%Y)
  39. printf "\n\n*******************************************************\n\tFull Webroot & Database Backup Script\n\t" | tee -a $LOGFILE
  40. echo "    $TODAY" | tee -a $LOGFILE
  41. printf "*******************************************************\n\n" $TODAY | tee -a $LOGFILE
  42. echo "see ${LOGFILE} for details"
  43.  
  44.  
  45. ## Scanning for .NO_BACKUP files that are laying inside the webfolders -- and ignoring those directories for backup
  46.  
  47. for DIR in $(ls | grep ^[a-z.]*$)
  48. do
  49.         echo $DIR
  50.  
  51.         if [ -f $DIR/.NO_BACKUP ]
  52.         then
  53.  
  54.                 printf "\t $DIR contains the ignore file - skipping it\n" | tee -a $LOGFILE
  55.  
  56.         else
  57.  
  58. cpath=${BACKUP_DIR}/${DIR}
  59.                 #
  60.                 # Checking if the earlier defined folders exists and creating them if necessary
  61.                 #
  62.                 if [ -d $cpath ]
  63.                 then
  64.                         # It exists -- let's move on
  65.                         filler="umin"
  66.                 else
  67.                         # Giving path did not exists -- creating it
  68.                         echo Creating $cpath
  69.                         mkdir -p $cpath
  70.                         echo $DEF_RETAIN > $cpath/.RETAIN_RULE
  71.                 fi
  72.                 #
  73.                 # Backing up the all the directories in the webroot (./$DIR)
  74.                 tar -zcf ${BACKUP_DIR}/${DIR}/${DIR}_$suffix.tar.gz ./$DIR
  75.                 # Adding +1 value to BU_FILE. 10 is the max kept, then automatic delete from the oldest.
  76.                 BU_FILE_COUNT=$(( $BU_FILE_COUNT + 1 ))
  77.         fi
  78. done
  79. printf "\n\n********************************************\n" | tee -a $LOGFILE
  80. echo $BU_FILE_COUNT webfolders were backed up
  81. printf "********************************************\n" $TODAY | tee -a $LOGFILE
  82.  
  83. cpath=${BACKUP_SQL_DIR} #/${DIR}
  84.                 if [ -d $cpath ]
  85.                 then
  86.                         filler="umin"
  87.                 else
  88.                         echo Creating $cpath
  89.                         mkdir -p $cpath
  90.                         echo $DEF_RETAIN > $cpath/.RETAIN_RULE
  91.                 fi
  92.  
  93.  
  94. databases=`$MYSQL --defaults-file=/root/my.cnf -e "SHOW DATABASES;" | egrep -v "(Database|information_schema|performance_schema)"`
  95.  
  96. # Do not use mysql logins and passwords in a script. Copy /etc/mysql/conf.d/my.cnf to /root/ and chmod 600 it.
  97. for db in $databases; do
  98.         $MYSQLDUMP --defaults-file=/root/my.cnf --force --opt --databases $db | gzip > "$BACKUP_SQL_DIR/$db.gz"
  99.         BU_FILE_COUNT=$(( $BU_FILE_COUNT + 1 ))
  100. done
  101.  
  102. rsync -a /home/user/backup user@x.x.x.x:/home/user/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement