Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Backup script from hakabe (c) 2017 - betro.hakala@gmail.com
- # Free to share and modify - just keep the author within the file
- # TODO's: Parse SQL databases and move them to the corresponding folders with the webroot
- # Renice 19 makes the script consume as little CPU power as possible (we don't wanna make our webservices to lag)
- renice 19 -p $$
- DEFRETAIN=10
- LOGFILE="$BACKUP_DIR/backup.log"
- BU_FILE_COUNT=0
- TIMESTAMP=$(date +"%F")
- WEBDIR=/var/www
- BACKUP_DIR="/home/user/backup/$TIMESTAMP"
- BACKUP_SQL_DIR="$BACKUP_DIR/mysql"
- BACKUP_WWW_DIR="$BACKUP_DIR/www"
- MYSQL="/usr/bin/mysql"
- MYSQLDUMP="/usr/bin/mysqldump"
- #RSYNC_HOST=x.x.x.x
- #RSYNC_LOGIN="user"
- #REMOTE_PATH="/home/user/"
- cpath=${BACKUP_DIR}
- if [ -d $cpath ]
- then
- filler="umin"
- else
- echo Creating $cpath
- mkdir -p $cpath
- fi
- #MOVING TO WEBROOT
- cd ${WEBDIR}/
- TODAY=`date`
- BU_FILE_COUNT=0
- suffix=$(date +%m-%d-%Y)
- printf "\n\n*******************************************************\n\tFull Webroot & Database Backup Script\n\t" | tee -a $LOGFILE
- echo " $TODAY" | tee -a $LOGFILE
- printf "*******************************************************\n\n" $TODAY | tee -a $LOGFILE
- echo "see ${LOGFILE} for details"
- ## Scanning for .NO_BACKUP files that are laying inside the webfolders -- and ignoring those directories for backup
- for DIR in $(ls | grep ^[a-z.]*$)
- do
- echo $DIR
- if [ -f $DIR/.NO_BACKUP ]
- then
- printf "\t $DIR contains the ignore file - skipping it\n" | tee -a $LOGFILE
- else
- cpath=${BACKUP_DIR}/${DIR}
- #
- # Checking if the earlier defined folders exists and creating them if necessary
- #
- if [ -d $cpath ]
- then
- # It exists -- let's move on
- filler="umin"
- else
- # Giving path did not exists -- creating it
- echo Creating $cpath
- mkdir -p $cpath
- echo $DEF_RETAIN > $cpath/.RETAIN_RULE
- fi
- #
- # Backing up the all the directories in the webroot (./$DIR)
- tar -zcf ${BACKUP_DIR}/${DIR}/${DIR}_$suffix.tar.gz ./$DIR
- # Adding +1 value to BU_FILE. 10 is the max kept, then automatic delete from the oldest.
- BU_FILE_COUNT=$(( $BU_FILE_COUNT + 1 ))
- fi
- done
- printf "\n\n********************************************\n" | tee -a $LOGFILE
- echo $BU_FILE_COUNT webfolders were backed up
- printf "********************************************\n" $TODAY | tee -a $LOGFILE
- cpath=${BACKUP_SQL_DIR} #/${DIR}
- if [ -d $cpath ]
- then
- filler="umin"
- else
- echo Creating $cpath
- mkdir -p $cpath
- echo $DEF_RETAIN > $cpath/.RETAIN_RULE
- fi
- databases=`$MYSQL --defaults-file=/root/my.cnf -e "SHOW DATABASES;" | egrep -v "(Database|information_schema|performance_schema)"`
- # Do not use mysql logins and passwords in a script. Copy /etc/mysql/conf.d/my.cnf to /root/ and chmod 600 it.
- for db in $databases; do
- $MYSQLDUMP --defaults-file=/root/my.cnf --force --opt --databases $db | gzip > "$BACKUP_SQL_DIR/$db.gz"
- BU_FILE_COUNT=$(( $BU_FILE_COUNT + 1 ))
- done
- rsync -a /home/user/backup user@x.x.x.x:/home/user/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement