From 80224e09b15361f74f39da1fa7c795b28522db06 Mon Sep 17 00:00:00 2001 From: admin Date: Fri, 6 Jan 2017 10:23:35 +0000 Subject: [PATCH] =?UTF-8?q?=EC=8A=A4=ED=81=AC=EB=A6=BD=ED=8A=B8=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: svn://192.168.0.12/source@336 8346c931-da38-4b9b-9d4c-e48b93cbd075 --- compile.sh | 155 ++++++++++++++++++++++++++++ dbbackup.sh | 281 ++++++++++++++++++++++++++++++++++++++++++++++++++ dumpscript.sh | 167 ++++++++++++++++++++++++++++++ sendexec.sh | 8 ++ 4 files changed, 611 insertions(+) create mode 100644 compile.sh create mode 100644 dbbackup.sh create mode 100644 dumpscript.sh create mode 100644 sendexec.sh diff --git a/compile.sh b/compile.sh new file mode 100644 index 0000000..03a7525 --- /dev/null +++ b/compile.sh @@ -0,0 +1,155 @@ +#!/bin/bash +WORKING_DIRECTORY=$(pwd) +PATH_SOURCE="$WORKING_DIRECTORY/source" +PATH_COMPILE_PREFIX="$PATH_SOURCE/build-" +PATH_COMPILE_SUFFIX="-Desktop_Qt_5_4_0_GCC_64bit-Release" +QT_QMAKE_PATH="/home/testdb/Qt5.4.0/5.4/gcc_64/bin" +QMAKE="$QT_QMAKE_PATH/qmake" +MAKE_CLEAN= +usage() +{ + echo "Usage : compile.sh [options] directory1 [directory2...]" + echo "" + echo " -s, --source Set Qt Project parent directory" + echo " -o, --output Copy executable files to specific directory" + echo " -c, --clean make clean" + echo " -h, --help help" + echo "" + echo "Purpose : compile a Qt project in source/directory1" +} + +PATH_OUTPUT= + +qtcompile() +{ + echo "$1 : " + if [ ! -d "$PATH_SOURCE" ]; then + echo "Fatal Error : Source path does not exists" + echo "Quit compile" + exit 1 + fi + + if [ ! -d "$PATH_SOURCE/$1" ]; then + echo "Error : Project directory $PATH_SOURCE/$1 does not exists" + return 1 + fi + + path_source="$PATH_SOURCE/$1/$1"".pro" + + if [ ! -f "$path_source" ]; then + echo "Error : Project file $path_source does not exists" + return 1 + fi + + path_compile="$PATH_COMPILE_PREFIX""$1""$PATH_COMPILE_SUFFIX" + if [ ! -d "$path_compile" ]; then + mkdir $path_compile + fi + + if ! cd $path_compile; then + echo "Error : Cannot access $path_compile" + return 1 + fi + + if [ -n "$MAKE_CLEAN" ]; then + + if "$QMAKE" "$path_source" "-r" "-spec" "linux-g++" && "/usr/bin/make" "clean" 1>&/dev/null && "/usr/bin/make" 1>&/dev/null ; then + echo "Success : Make Clean $path_compile" + echo "Success : Compile complete $path_source" + echo "Success : Create File in $path_compile" + + else + echo "Error : Compile error - $path_source" + return 1 + + fi + + else + + if "$QMAKE" "$path_source" "-r" "-spec" "linux-g++" && "/usr/bin/make" 1>&/dev/null ; then + echo "Success : Compile complete $path_source" + echo "Success : Create File in $path_compile" + + else + echo "Error : Compile error - $path_source" + return 1 + + fi + + fi + + + if [ -n "$PATH_OUTPUT" ]; then + if ! cp "$path_compile/$1" "$PATH_OUTPUT/"; then + echo "Error : Copy error $path_compile/$1 to $PATH_OUTPUT/" + return 1 + else + echo "Success : Copy $path_compile/$1 to $PATH_OUTPUT/" + fi + fi + echo "" + return 0 +} + + +if [ $# -eq 0 ]; then + usage + exit 1 +fi + + +#basename +#dirname + +while [ "$1" != "" ]; do + case $1 in + -s | --source ) shift + path_source="$(dirname $1)" + if [[ $path_source =~ ^/([^/]+) ]]; then + PATH_SOURCE="$path_source" + + elif [ "$path_source" = "." ]; then + PATH_SOURCE="$WORKING_DIRECTORY/$1" + + else + PATH_SOURCE="$WORKING_DIRECTORY/$path_source" + fi + ;; + -o | --ouput ) shift + if [[ $1 =~ ^/([^/]+) ]]; then + PATH_OUTPUT="$1" + else + PATH_OUTPUT="$WORKING_DIRECTORY/$1" + fi + ;; + -c | --clean ) MAKE_CLEAN="clean" + ;; + -h | --help ) usage + exit + ;; + -* | --* ) echo "Error : Unknown Option $1" + echo " Please check option" + usage + exit 1 + ;; + + * ) dir_name=$(dirname $1) + arg=$(basename $1) + if [ ! "$dir_name" = "." ]; then + path_source="$dir_name" + + if [[ $path_source =~ ^/([^/]+) ]]; then + PATH_SOURCE="$path_source" + else + PATH_SOURCE="$WORKING_DIRECTORY/$path_source" + fi + fi + + qtcompile $arg + ;; + esac + shift +done + +exit + diff --git a/dbbackup.sh b/dbbackup.sh new file mode 100644 index 0000000..fd4c997 --- /dev/null +++ b/dbbackup.sh @@ -0,0 +1,281 @@ +#!/bin/bash +IMPORT_DAYS=(1 3 5 7 9 11 13 15 17 19 21 23 25 27 29) + +BACKUP_HOST="192.168.0.30" +BACKUP_PATH="/media/testdb/dump" +BACKUP_USER="testdb" + +DATABASE_NAME="concepters" +DATABASE_USER="admin" +DATABASE_PASSWORD="admin123" + +LOG_PATH="/home/concepters/log" +LOG_FILENAME="dbbackup.log" + +TODAY=$(date +"%Y_%m_%d") +DUMP_PATH="/home/concepters/hdd/mysqlbackup" +DUMP_FILENAME=$TODAY".dump.sql" +DUMP_TOTALPATH="$DUMP_PATH/$DUMP_FILENAME" + +MIN_DUMP=5 +MAX_DUMP=10 +DUMP_OPT="--single-transaction" +DUMP_SCRIPT="/usr/local/bin/dumpscript.sh" + +insertlogger() +{ + logger "$0 : $1" && echo "$0 : $1" 1>&2 +} +#Split new lines +IFS=$'\012' + +#Log file check +insertlog() +{ + if [ ! -d $LOG_PATH ];then + if ! mkdir $LOG_PATH; then + insertlogger "Fail to make log folder" + return 1 + fi + fi + + if [ ! -f "$LOG_PATH/$LOG_FILENAME" ]; then + if ! touch "$LOG_PATH/$LOG_FILENAME"; then + insertlogger "Fail to create logfile" + return 1 + fi + fi + + if [ ! -w "$LOG_PATH/$LOG_FILENAME" ]; then + insertlogger "Permission denied to log file" + return 1 + fi + + messages="["$(date +"%F %H:%M:%S")"] $0".$$" : $1" + echo "$messages" >> "$LOG_PATH/$LOG_FILENAME" || insertlogger $messages + + return 0 +} +#Dump day check + +isDump() +{ + dayofmonth=$(date +"%e") + for i in ${IMPORT_DAYS[@]}; do + if [ $i -eq $dayofmonth ]; then + return 0 + fi + done + return 1 +} + + +# Use carefully, this function does not check size of array + +getOldestDump() +{ + value=( ) +# echo $@ + r_count=0 + for i in $@; do + value[$r_count]=$i + r_count=$((r_count+1)) + done + + oldestFile=${value[0]}; + + for i in ${value[@]}; do + if [ $i -ot $oldestFile ]; then + oldestFile=$i + fi + done + + echo $oldestFile +} + +#Get dumpfile list +#dumpFileList=$(ls -1t "$DUMP_PATH"[0-2][0-9][0-9][0-9]_[0-1][0-9]_[0-3][0-9].dump.sql) + +insertlog "Start backup concepters Datadb" +dumpFileList=$(ls -1t "$DUMP_PATH/"*.dump.sql) + +#Get number of lists +count=0 +for i in ${dumpFileList[@]}; do + #echo $i + count=$((count+1)) +done + +insertlog "Number of dumpfile = $count" + +#remove oldest dumpfile if number of dumpfile is greater equal to MAX_DUMP + +oldestDump= + +if [ $count -ge $MAX_DUMP ]; then + oldestDump=$(getOldestDump $dumpFileList) + if rm $oldestDump; then + insertlog "Removed $oldestDump" + else + insertlog "Fail to remove $oldestDump" + fi +fi + + +# Change dumpfile name if a dumpfile already exists +suffix=1 + +while [ -f "$DUMP_TOTALPATH" ]; do + insertlog "File $DUMP_TOTALPATH already exists!!" + DUMP_FILENAME="$TODAY""_$suffix"".dump.sql" + DUMP_TOTALPATH="$DUMP_PATH/$DUMP_FILENAME" + insertlog "Dumpfile will be saved to $DUMP_TOTALPATH" + suffix=$((suffix+1)) +done + + +# Start dump + +insertlog "Start mysqldump to $DUMP_TOTALPATH" +if ! mysqldump -u"$DATABASE_USER" -p"$DATABASE_PASSWORD" "$DUMP_OPT" "$DATABASE_NAME" > "$DUMP_TOTALPATH"; then + insertlog "Mysqldump failed" + exit 1 +fi + +insertlog "Success mysqldump to $DUMP_TOTALPATH" + + +# Check dumpfile list, size and free space in the testDB PC + +testDumpFileList=$(ssh "$BACKUP_USER"@"$BACKUP_HOST" "ls -alt --block-size=k $BACKUP_PATH/*.dump.sql") +dfScreen=$(ssh "$BACKUP_USER"@"$BACKUP_HOST" "df -k") +lineofdf=$(echo "$dfScreen" | grep "$BACKUP_PATH") + +#echo $testDumpFileList + +# Extract useful infomation + +rxdisk="\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)" +rxfile="([0-9]+)K" +rxpath="\s+([0-9a-zA-Z_/. ]+\.dump.sql)$" +lsdumpfile=$(ls -alt --block-size=k $DUMP_TOTALPATH) + +dumpfilesize= +if [[ $lsdumpfile =~ $rxfile ]]; then + dumpfilesize=${BASH_REMATCH[1]} +fi + +diskfreespace= +if [[ $lineofdf =~ $rxdisk ]]; then + diskfreespace=${BASH_REMATCH[3]} +fi + +filesize=( ) +filelist=( ) +count=0 +for i in ${testDumpFileList[@]}; do + if [[ $i =~ $rxfile ]]; then + filesize[$count]=${BASH_REMATCH[1]} + fi + if [[ $i =~ $rxpath ]]; then + filelist[$count]=${BASH_REMATCH[1]} + fi + count=$((count+1)) +done + +#echo "Last file = ${filelist[$((count-1))]}" +#for i in "${testDumpFileList[@]}"; do +# echo $i +#done + +insertlog "$BACKUP_HOST has $count dump files" +insertlog "$BACKUP_HOST has $diskfreespace kbytes free space" + + +# Transfer the dumpfile if free space in the testDB PC is enough to save the dumpfile +# This script check free space, size dumpfiles in the testdb PC and a dumpfile made now +# Number of dumpfile in the testDB pc MUST BE GRATHER THAN MIN_DUMP +# If free space is not enough, you have to buy a new hdd or remove old dumpfiles in the testDB PC +# This script will remove older dumpfiles in the testDB PC as guarantee constraints above + +if [ $diskfreespace -lt $dumpfilesize ]; then + if [ $count -lt $MIN_DUMP ]; then + insertlog "$BACKUP_HOST has too small free disk space" + insertlog "Need to extend $BACKUP_HOST disk space" + insertlog "Backup is canceled" + exit 1 + + else + insertlog "Check dumpfiles to remove" + tempcount=$count + calcfreesize=$diskfreespace + + while [ $calcfreesize -lt $dumpfilesize ]; do + calcfreesize=$((calcfreesize+filesize[$((tempcount-1))])) + if [ $tempcount -lt $MIN_DUMP ]; then + insertlog "$BACKUP_HOST has too small free disk space" + insertlog "Need to extend $BACKUP_HOST disk space" + insertlog "Backup is canceled" + exit 1 + fi + tempcount=$((tempcount-1)) + done + + for ((i=tempcount-1;i&2 +} + +insertlog() +{ + if [ ! -d $LOG_PATH ];then + if ! mkdir $LOG_PATH; then + insertlogger "Fail to make log folder" + return 1 + fi + fi + + if [ ! -f "$LOG_PATH/$LOG_FILENAME" ]; then + if ! touch "$LOG_PATH/$LOG_FILENAME"; then + insertlogger "Fail to create logfile" + return 1 + fi + fi + + if [ ! -w "$LOG_PATH/$LOG_FILENAME" ]; then + insertlogger "Permission denied to log file" + return 1 + fi + + messages="["$(date +"%F %H:%M:%S")"] $0".$$" : $1" + echo "$messages" >> "$LOG_PATH/$LOG_FILENAME" || insertlogger $messages + + return 0 +} +insertlog "Start to extract dumpfile" + + +# Check parameter 1 +# If parameter 1 is empty, check BACKUP_PATH and recent dump file will be extracted + +if [ -z "$1" ]; then + if ! ls -t "$BACKUP_PATH/"*".dump.sql"; then + insertlog "Fail to find dumpfile in $BACKUP_PATH" + exit 1 + fi + + filenamelist=$(ls -t "$BACKUP_PATH/"*".dump.sql") + for i in ${filenamelist[@]}; do + BACKUP_TOTALPATH=$i + break + done +fi + +insertlog "$BACKUP_TOTALPATH" + + +# Check form of dump file is valid and extract the day when the dumpfile is created + +rx="/[0-9]{4}_[0-9]{2}_([0-9]{2})\w*\.dump\.sql" + +day= +if [[ $BACKUP_TOTALPATH =~ $rx ]]; then + day=${BASH_REMATCH[1]} +fi + + +if [ -z "$day" ]; then + insertlog "Check dumpfile name YYYY_MM_DD.dump.sql or YYYY_MM_DD_N.dump.sql" + insertlog "Fail to dump file" + exit 1 +fi + +databasename= +previousname= +# Determine where to import the dumpfile + +for i in ${DATABASE_GROUPONE[@]}; do + if [ $i -eq $day ]; then + databasename=$DATABASE_ONENAME + previousname=$DATABASE_TWONAME + fi +done + +for i in ${DATABASE_GROUPTWO[@]}; do + if [ $i -eq $day ]; then + databasename=$DATABASE_TWONAME + previousname=$DATABASE_THREENAME + fi +done + +for i in ${DATABASE_GROUPTHREE[@]}; do + if [ $i -eq $day ]; then + databasename=$DATABASE_THREENAME + previousname=$DATABASE_ONENAME + fi +done + +# If today is not dump day, extracting dump is canceled + +if [ -z "$databasename" ]; then + insertlog "Today is not dump day" + insertlog "Extracting dump is canceled" + exit 1 +fi + +#echo "databasename = $databasename" +#echo "previousname = $previousname" + +# Remove all tables in the oldest backup DB + +insertlog "Start to delete $previousname tables" + +showtables=$(mysql -u"$MYSQL_USER" -p"$MYSQL_PASSWORD" $previousname -e "show tables") + +rxtable="(\w+)" +tablelist=( ) +count=0 +if [ ! -z "$showtables" ]; then + for i in ${showtables[@]}; do + if [[ $i =~ $rxtable ]]; then + tablelist[$((count))]=${BASH_REMATCH[1]} + count=$((count+1)) + fi + done +fi + +for((i=1;i