From 067ef1a51a54eee31e5d13ad7605f3a9ec3e3898 Mon Sep 17 00:00:00 2001 From: Teddysun Date: Thu, 24 Nov 2016 23:40:26 +0900 Subject: [PATCH 01/17] update display information --- bench.sh | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/bench.sh b/bench.sh index c7f63c9..63e7667 100644 --- a/bench.sh +++ b/bench.sh @@ -100,6 +100,7 @@ cname=$( awk -F: '/model name/ {name=$2} END {print name}' /proc/cpuinfo | sed ' cores=$( awk -F: '/model name/ {core++} END {print core}' /proc/cpuinfo ) freq=$( awk -F: '/cpu MHz/ {freq=$2} END {print freq}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//' ) tram=$( free -m | awk '/Mem/ {print $2}' ) +uram=$( free -m | awk '/Mem/ {print $3}' ) swap=$( free -m | awk '/Swap/ {print $2}' ) up=$( awk '{a=$1/86400;b=($1%86400)/3600;c=($1%3600)/60;d=$1%60} {printf("%ddays, %d:%d:%d\n",a,b,c,d)}' /proc/uptime ) load=$( w | head -1 | awk -F'load average:' '{print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//' ) @@ -111,20 +112,16 @@ kern=$( uname -r ) ipv6=$( wget -qO- -t1 -T2 ipv6.icanhazip.com ) disk_size1=($( df -ahPl | grep -wvE '\-|none|Filesystem' | awk '{print $2}' )) disk_size2=($( df -ahPl | grep -wvE '\-|none|Filesystem' | awk '{print $3}' )) -disk_size3=($( df -ahPl | grep -wvE '\-|none|Filesystem' | awk '{print $4}' )) disk_total_size=$( calc_disk $disk_size1 ) disk_used_size=$( calc_disk $disk_size2 ) -disk_avail_size=$( calc_disk $disk_size3 ) clear next echo "CPU model : $cname" echo "Number of cores : $cores" echo "CPU frequency : $freq MHz" -echo "Total size of Disk : $disk_total_size GB" -echo "Used size of Disk : $disk_used_size GB" -echo "Avail size of Disk : $disk_avail_size GB" -echo "Total amount of Mem : $tram MB" +echo "Total size of Disk : $disk_total_size GB ($disk_used_size GB Used)" +echo "Total amount of Mem : $tram MB ($uram MB Used)" echo "Total amount of Swap : $swap MB" echo "System uptime : $up" echo "Load average : $load" From e751ff8964dee29652e8b66e5e0cb11f4c43de20 Mon Sep 17 00:00:00 2001 From: Teddysun Date: Fri, 25 Nov 2016 00:46:43 +0900 Subject: [PATCH 02/17] update Disk information --- bench.sh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bench.sh b/bench.sh index 63e7667..60a6c37 100644 --- a/bench.sh +++ b/bench.sh @@ -86,10 +86,10 @@ io_test() { calc_disk() { local total_size=0 - local array=$1 + local array=$@ for size in ${array[@]} do - size_t=`echo ${size:0:${#size}-1}` + [ "${size}" == "0" ] && size_t=0 || size_t=`echo ${size:0:${#size}-1}` [ "`echo ${size:(-1)}`" == "M" ] && size=$( awk 'BEGIN{print '$size_t' / 1024}' ) || size=${size_t} total_size=$( awk 'BEGIN{print '$total_size' + '$size'}' ) done @@ -110,10 +110,10 @@ lbit=$( getconf LONG_BIT ) host=$( hostname ) kern=$( uname -r ) ipv6=$( wget -qO- -t1 -T2 ipv6.icanhazip.com ) -disk_size1=($( df -ahPl | grep -wvE '\-|none|Filesystem' | awk '{print $2}' )) -disk_size2=($( df -ahPl | grep -wvE '\-|none|Filesystem' | awk '{print $3}' )) -disk_total_size=$( calc_disk $disk_size1 ) -disk_used_size=$( calc_disk $disk_size2 ) +disk_size1=($( df -ahPl | grep -wvE '\-|none|tmpfs|Filesystem' | awk '{print $2}' )) +disk_size2=($( df -ahPl | grep -wvE '\-|none|tmpfs|Filesystem' | awk '{print $3}' )) +disk_total_size=$( calc_disk ${disk_size1[@]} ) +disk_used_size=$( calc_disk ${disk_size2[@]} ) clear next From 3495a4caa5dde92ece6f181908b5c39c9b73c890 Mon Sep 17 00:00:00 2001 From: Teddysun Date: Fri, 25 Nov 2016 00:58:42 +0900 Subject: [PATCH 03/17] update uptime --- bench.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bench.sh b/bench.sh index 60a6c37..300222a 100644 --- a/bench.sh +++ b/bench.sh @@ -102,7 +102,7 @@ freq=$( awk -F: '/cpu MHz/ {freq=$2} END {print freq}' /proc/cpuinfo | sed 's/^[ tram=$( free -m | awk '/Mem/ {print $2}' ) uram=$( free -m | awk '/Mem/ {print $3}' ) swap=$( free -m | awk '/Swap/ {print $2}' ) -up=$( awk '{a=$1/86400;b=($1%86400)/3600;c=($1%3600)/60;d=$1%60} {printf("%ddays, %d:%d:%d\n",a,b,c,d)}' /proc/uptime ) +up=$( awk '{a=$1/86400;b=($1%86400)/3600;c=($1%3600)/60} {printf("%d days, %d hour %d min\n",a,b,c)}' /proc/uptime ) load=$( w | head -1 | awk -F'load average:' '{print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//' ) opsy=$( get_opsy ) arch=$( uname -m ) From 29da548407ab5e68d5b443c79cbf0a99dcd1644f Mon Sep 17 00:00:00 2001 From: Teddysun Date: Fri, 25 Nov 2016 11:33:45 +0900 Subject: [PATCH 04/17] update Disk size (round float number) --- bench.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bench.sh b/bench.sh index 300222a..7d6f29d 100644 --- a/bench.sh +++ b/bench.sh @@ -90,8 +90,8 @@ calc_disk() { for size in ${array[@]} do [ "${size}" == "0" ] && size_t=0 || size_t=`echo ${size:0:${#size}-1}` - [ "`echo ${size:(-1)}`" == "M" ] && size=$( awk 'BEGIN{print '$size_t' / 1024}' ) || size=${size_t} - total_size=$( awk 'BEGIN{print '$total_size' + '$size'}' ) + [ "`echo ${size:(-1)}`" == "M" ] && size=$( awk 'BEGIN{printf "%.1f", '$size_t' / 1024}' ) || size=${size_t} + total_size=$( awk 'BEGIN{printf "%.1f", '$total_size' + '$size'}' ) done echo ${total_size} } From ff8139a854d7b5d2d4a8466d18f46963634dc26b Mon Sep 17 00:00:00 2001 From: Teddysun Date: Fri, 25 Nov 2016 16:24:37 +0900 Subject: [PATCH 05/17] update IO test average speed --- bench.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bench.sh b/bench.sh index 7d6f29d..b345ca0 100644 --- a/bench.sh +++ b/bench.sh @@ -142,7 +142,7 @@ ioraw2=$( echo $io2 | awk 'NR==1 {print $1}' ) ioraw3=$( echo $io3 | awk 'NR==1 {print $1}' ) [ "`echo $io3 | awk 'NR==1 {print $2}'`" == "GB/s" ] && ioraw3=$( awk 'BEGIN{print '$ioraw3' * 1024}' ) ioall=$( awk 'BEGIN{print '$ioraw1' + '$ioraw2' + '$ioraw3'}' ) -ioavg=$( awk 'BEGIN{print '$ioall'/3}' ) +ioavg=$( awk 'BEGIN{printf "%.1f", '$ioall' / 3}' ) echo "Average I/O speed : $ioavg MB/s" next echo -e "Node Name\t\t\tIPv4 address\t\tDownload Speed" From 95c2bab52aa03bb9192f267d4f9bd9d953c34f9e Mon Sep 17 00:00:00 2001 From: Teddysun Date: Sun, 27 Nov 2016 00:44:36 +0900 Subject: [PATCH 06/17] update Disk information --- bench.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bench.sh b/bench.sh index b345ca0..02e63d4 100644 --- a/bench.sh +++ b/bench.sh @@ -110,8 +110,8 @@ lbit=$( getconf LONG_BIT ) host=$( hostname ) kern=$( uname -r ) ipv6=$( wget -qO- -t1 -T2 ipv6.icanhazip.com ) -disk_size1=($( df -ahPl | grep -wvE '\-|none|tmpfs|Filesystem' | awk '{print $2}' )) -disk_size2=($( df -ahPl | grep -wvE '\-|none|tmpfs|Filesystem' | awk '{print $3}' )) +disk_size1=($( df -ahPl | grep -wvE '\-|none|tmpfs|by-uuid|Filesystem' | awk '{print $2}' )) +disk_size2=($( df -ahPl | grep -wvE '\-|none|tmpfs|by-uuid|Filesystem' | awk '{print $3}' )) disk_total_size=$( calc_disk ${disk_size1[@]} ) disk_used_size=$( calc_disk ${disk_size2[@]} ) From b96f4dc6973d15e1c995f5879f5730947bba3cf5 Mon Sep 17 00:00:00 2001 From: Teddysun Date: Sun, 27 Nov 2016 16:37:14 +0900 Subject: [PATCH 07/17] update Disk information --- bench.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bench.sh b/bench.sh index 02e63d4..9b70d94 100644 --- a/bench.sh +++ b/bench.sh @@ -90,7 +90,9 @@ calc_disk() { for size in ${array[@]} do [ "${size}" == "0" ] && size_t=0 || size_t=`echo ${size:0:${#size}-1}` - [ "`echo ${size:(-1)}`" == "M" ] && size=$( awk 'BEGIN{printf "%.1f", '$size_t' / 1024}' ) || size=${size_t} + [ "`echo ${size:(-1)}`" == "M" ] && size=$( awk 'BEGIN{printf "%.1f", '$size_t' / 1024}' ) + [ "`echo ${size:(-1)}`" == "T" ] && size=$( awk 'BEGIN{printf "%.1f", '$size_t' * 1024}' ) + [ "`echo ${size:(-1)}`" == "G" ] && size=${size_t} total_size=$( awk 'BEGIN{printf "%.1f", '$total_size' + '$size'}' ) done echo ${total_size} From b4c1c9dc3142b482c88913a6528bf61d418ac517 Mon Sep 17 00:00:00 2001 From: Teddysun Date: Tue, 29 Nov 2016 11:57:34 +0900 Subject: [PATCH 08/17] Add auto transfer backup file to FTP server --- backup.sh | 413 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 249 insertions(+), 164 deletions(-) diff --git a/backup.sh b/backup.sh index 74956e1..c76092b 100644 --- a/backup.sh +++ b/backup.sh @@ -1,18 +1,27 @@ #!/usr/bin/env bash -PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin -#==============================================================# -# Description: backup script # -# Author: Teddysun # -# Visit: https://teddysun.com # -#==============================================================# +# +# Auto backup script +# +# Copyright (C) 2016 Teddysun +# +# URL: https://teddysun.com/469.html +# +# You must to modify the config before run it!!! +# Backup MySQL/MariaDB/Percona datebases, files and directories +# Backup file is encrypted with AES256-cbc with SHA1 message-digest (option) +# Auto transfer backup file to Google Drive (need install gdrive command) (option) +# Auto transfer backup file to FTP server (option) +# Auto delete Google Drive's or FTP server's remote file (option) +# -[[ $EUID -ne 0 ]] && echo 'Error: This script must be run as root!' && exit 1 +[[ $EUID -ne 0 ]] && echo "Error: This script must be run as root!" && exit 1 -### START OF CONFIG ### -# Encrypt flag(true:encrypt, false:not encrypt) +########## START OF CONFIG ########## + +# Encrypt flag (true: encrypt, false: not encrypt) ENCRYPTFLG=true -# KEEP THE PASSWORD SAFE. +# WARNING: KEEP THE PASSWORD SAFE!!! # The password used to encrypt the backup # To decrypt backups made by this script, run the following command: # openssl enc -aes256 -in [encrypted backup] -out decrypted_backup.tgz -pass pass:[backup password] -d -md sha1 @@ -27,150 +36,174 @@ TEMPDIR="/root/backups/temp/" # File to log the outcome of backups LOGFILE="/root/backups/backup.log" -# OPTIONAL: If you want MySQL to be backed up, enter the root password below +# OPTIONAL: If you want backup MySQL database, enter the MySQL root password below MYSQL_ROOT_PASSWORD="" # Below is a list of MySQL database name that will be backed up -# if you want backup all databases, leave it blank. +# If you want backup ALL databases, leave it blank. MYSQL_DATABASE_NAME[0]="" # Below is a list of files and directories that will be backed up in the tar backup # For example: # File: /data/www/default/test.tgz -# Directory: /data/www/default/test/ -# if you want not to be backed up, leave it blank. +# Directory: /data/www/default/test BACKUP[0]="" -# Number of days to store daily local backups +# Number of days to store daily local backups (default 7 days) LOCALAGEDAILIES="7" -# Delete Googole Drive's remote file flag(true:delete, false:not delete) -DELETE_REMOTE_FILE_FLG=true +# Delete Googole Drive's & FTP server's remote file flag (true: delete, false: not delete) +DELETE_REMOTE_FILE_FLG=false + +# Upload to FTP server flag (true: upload, false: not upload) +FTP_FLG=false + +# FTP server +# OPTIONAL: If you want upload to FTP server, enter the Hostname or IP address below +FTP_HOST="" + +# FTP username +# OPTIONAL: If you want upload to FTP server, enter the FTP username below +FTP_USER="" + +# FTP password +# OPTIONAL: If you want upload to FTP server, enter the username's password below +FTP_PASS="" + +# FTP server remote folder +# OPTIONAL: If you want upload to FTP server, enter the FTP remote folder below +# For example: public_html +FTP_DIR="" + +########## END OF CONFIG ########## + + # Date & Time +DAY=$(date +%d) +MONTH=$(date +%m) +YEAR=$(date +%C%y) BACKUPDATE=$(date +%Y%m%d%H%M%S) - # Backup file name TARFILE="${LOCALDIR}""$(hostname)"_"${BACKUPDATE}".tgz - +# Encrypted backup file name +ENC_TARFILE="${TARFILE}.enc" # Backup MySQL dump file name SQLFILE="${TEMPDIR}mysql_${BACKUPDATE}.sql" -### END OF CONFIG ### - - log() { echo "$(date "+%Y-%m-%d %H:%M:%S")" "$1" echo -e "$(date "+%Y-%m-%d %H:%M:%S")" "$1" >> ${LOGFILE} } - -### START OF CHECKS ### -# Check if the backup folders exist and are writeable -if [ ! -d "${LOCALDIR}" ]; then - mkdir -p ${LOCALDIR} -fi -if [ ! -d "${TEMPDIR}" ]; then - mkdir -p ${TEMPDIR} -fi - -# This section checks for all of the binaries used in the backup -BINARIES=( cat cd du date dirname echo openssl mysql mysqldump pwd rm tar ) - -# Iterate over the list of binaries, and if one isn't found, abort -for BINARY in "${BINARIES[@]}"; do - if [ ! "$(command -v "$BINARY")" ]; then - log "$BINARY is not installed. Install it and try again" - exit 1 - fi -done -### END OF CHECKS ### - -STARTTIME=$(date +%s) -cd ${LOCALDIR} || exit -log "Backup progress start" - - -### START OF MYSQL BACKUP ### -if [ -z ${MYSQL_ROOT_PASSWORD} ]; then - log "MySQL root password not set, MySQL back up skip" -else - log "MySQL dump start" - mysql -u root -p"${MYSQL_ROOT_PASSWORD}" 2>/dev/null < "${SQLFILE}" 2>/dev/null - if [ $? -ne 0 ]; then - log "MySQL all databases backup failed" +# Check for list of mandatory binaries +check_commands() { + # This section checks for all of the binaries used in the backup + BINARIES=( cat cd du date dirname echo openssl mysql mysqldump pwd rm tar ftp ) + + # Iterate over the list of binaries, and if one isn't found, abort + for BINARY in "${BINARIES[@]}"; do + if [ ! "$(command -v "$BINARY")" ]; then + log "$BINARY is not installed. Install it and try again" exit 1 fi - log "MySQL all databases dump file name: ${SQLFILE}" - #Add MySQL backup dump file to BACKUP list - BACKUP=(${BACKUP[*]} ${SQLFILE}) + done + + # check gdrive command + GDRIVE_COMMAND=false + if [ "$(command -v "gdrive")" ]; then + GDRIVE_COMMAND=true + fi +} + +calculate_size() { + local file_name=$1 + local file_size=$(du -h $file_name 2>/dev/null | awk '{print $1}') + if [ "x${file_size}" = "x" ]; then + echo "unknown" else - for db in ${MYSQL_DATABASE_NAME[*]} - do - unset DBFILE - DBFILE="${TEMPDIR}${db}_${BACKUPDATE}.sql" - mysqldump -u root -p"${MYSQL_ROOT_PASSWORD}" ${db} > "${DBFILE}" 2>/dev/null + echo "${file_size}" + fi +} + +# Backup MySQL databases +mysql_backup() { + if [ -z ${MYSQL_ROOT_PASSWORD} ]; then + log "MySQL root password not set, MySQL backup skipped" + else + log "MySQL dump start" + mysql -u root -p"${MYSQL_ROOT_PASSWORD}" 2>/dev/null < "${SQLFILE}" 2>/dev/null if [ $? -ne 0 ]; then - log "MySQL database name [${db}] backup failed, please check database name is correct and try again" + log "MySQL all databases backup failed" exit 1 fi - log "MySQL database name [${db}] dump file name: ${DBFILE}" + log "MySQL all databases dump file name: ${SQLFILE}" #Add MySQL backup dump file to BACKUP list - BACKUP=(${BACKUP[*]} ${DBFILE}) - done + BACKUP=(${BACKUP[*]} ${SQLFILE}) + else + for db in ${MYSQL_DATABASE_NAME[*]} + do + unset DBFILE + DBFILE="${TEMPDIR}${db}_${BACKUPDATE}.sql" + mysqldump -u root -p"${MYSQL_ROOT_PASSWORD}" ${db} > "${DBFILE}" 2>/dev/null + if [ $? -ne 0 ]; then + log "MySQL database name [${db}] backup failed, please check database name is correct and try again" + exit 1 + fi + log "MySQL database name [${db}] dump file name: ${DBFILE}" + #Add MySQL backup dump file to BACKUP list + BACKUP=(${BACKUP[*]} ${DBFILE}) + done + fi + log "MySQL dump completed" + fi +} + +start_backup() { + [ "${BACKUP[*]}" == "" ] && echo "Error: You must to modify the [$(basename $0)] config before run it!" && exit 1 + + log "Tar backup file start" + tar -zcPf ${TARFILE} ${BACKUP[*]} + if [ $? -gt 1 ]; then + log "Tar backup file failed" + exit 1 + fi + log "Tar backup file completed" + + # Encrypt tar file + if ${ENCRYPTFLG}; then + log "Encrypt backup file start" + openssl enc -aes256 -in "${TARFILE}" -out "${ENC_TARFILE}" -pass pass:"${BACKUPPASS}" -md sha1 + log "Encrypt backup file completed" + + # Delete unencrypted tar + log "Delete unencrypted tar file: ${TARFILE}" + rm -f ${TARFILE} fi - log "MySQL dump completed" + # Delete MySQL temporary dump file + for sql in `ls ${TEMPDIR}*.sql` + do + log "Delete MySQL temporary dump file: ${sql}" + rm -f ${sql} + done -fi -### END OF MYSQL BACKUP ### - - -### START OF TAR BACKUP ### -log "Tar backup file start" -tar -zcPf ${TARFILE} ${BACKUP[*]} -if [ $? -ne 0 ]; then - log "Tar backup file failed" - exit 1 -fi -log "Tar backup file completed" - -# Encrypt tar file -if ${ENCRYPTFLG}; then - log "Encrypt backup file start" - openssl enc -aes256 -in "${TARFILE}" -out "${TARFILE}.enc" -pass pass:"${BACKUPPASS}" -md sha1 - log "Encrypt backup file completed" - - # Delete unencrypted tar - log "Delete unencrypted tar file: ${TARFILE}" - rm -f ${TARFILE} -fi - -# Delete MySQL temporary dump file -for sql in `ls ${TEMPDIR}*.sql` -do - log "Delete MySQL temporary dump file: ${sql}" - rm -f ${sql} -done - -log "Backup progress complete" - -if ${ENCRYPTFLG}; then - BACKUPSIZE=$(du -h ${TARFILE}.enc | cut -f1) - log "File name: ${TARFILE}.enc, File size: ${BACKUPSIZE}" -else - BACKUPSIZE=$(du -h ${TARFILE} | cut -f1) - log "File name: ${TARFILE}, File size: ${BACKUPSIZE}" -fi + if ${ENCRYPTFLG}; then + OUT_FILE="${ENC_TARFILE}" + else + OUT_FILE="${TARFILE}" + fi + log "File name: ${OUT_FILE}, File size: `calculate_size ${OUT_FILE}`" +} # Transfer backup file to Google Drive # If you want to install gdrive command, please visit website: @@ -178,33 +211,51 @@ fi # of cause, you can use below command to install it # For x86_64: wget -O /usr/bin/gdrive http://dl.lamp.sh/files/gdrive-linux-x64; chmod +x /usr/bin/gdrive # For i386: wget -O /usr/bin/gdrive http://dl.lamp.sh/files/gdrive-linux-386; chmod +x /usr/bin/gdrive - -if [ ! "$(command -v "gdrive")" ]; then - GDRIVE_COMMAND=false - log "gdrive is not installed" - log "File transfer skipped. please install it and try again" -else - GDRIVE_COMMAND=true - log "Tranferring backup file to Google Drive" - if ${ENCRYPTFLG}; then - gdrive upload --no-progress ${TARFILE}.enc >> ${LOGFILE} - else - gdrive upload --no-progress ${TARFILE} >> ${LOGFILE} +gdrive_upload() { + if ${GDRIVE_COMMAND}; then + log "Tranferring backup file to Google Drive" + gdrive upload --no-progress ${OUT_FILE} >> ${LOGFILE} + if [ $? -ne 0 ]; then + log "Error: Tranferring backup file to Google Drive failed" + exit 1 + fi + log "Tranferring backup file to Google Drive completed" fi - log "Tranfer completed" -fi -### END OF TAR BACKUP ### +} +# Tranferring backup file to FTP server +ftp_upload() { + if ${FTP_FLG}; then + [ -z ${FTP_HOST} ] && echo "Error: FTP_HOST can not be empty!" && exit 1 + [ -z ${FTP_USER} ] && echo "Error: FTP_USER can not be empty!" && exit 1 + [ -z ${FTP_PASS} ] && echo "Error: FTP_PASS can not be empty!" && exit 1 + [ -z ${FTP_DIR} ] && echo "Error: FTP_DIR can not be empty!" && exit 1 -### START OF DELETE OLD BACKUP FILE### -getFileDate() { - unset FILEYEAR FILEMONTH FILEDAY FILETIME FILEDAYS FILEAGE + local FTP_OUT_FILE=$(basename ${OUT_FILE}) + log "Tranferring backup file to FTP server" + ftp -in ${FTP_HOST} 2>&1 >> ${LOGFILE} <> ${LOGFILE} - log "Google Drive's old backup file name:${FILENAME} has been deleted" + log "Google Drive's old backup file name: ${FILENAME} has been deleted" fi fi } -AGEDAILIES=${LOCALAGEDAILIES} -DAY=$(date +%d) -MONTH=$(date +%m) -YEAR=$(date +%C%y) -#Approximate a 30-day month and 365-day year -DAYS=$(( $((10#${YEAR}*365)) + $((10#${MONTH}*30)) + $((10#${DAY})) )) +# Delete FTP server's old backup file +delete_ftp_file() { + local FILENAME=$1 + if ${DELETE_REMOTE_FILE_FLG} && ${FTP_FLG}; then + ftp -in ${FTP_HOST} 2>&1 >> ${LOGFILE} < Date: Tue, 29 Nov 2016 12:00:45 +0900 Subject: [PATCH 09/17] update Readme --- Readme.md | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/Readme.md b/Readme.md index 1cd4c7b..1c1f022 100644 --- a/Readme.md +++ b/Readme.md @@ -3,8 +3,8 @@ l2tp.sh ======= -* Description: Auto install L2TP VPN for CentOS6+/Debian7+/Ubuntu12+ -* Intro: https://teddysun.com/448.html +- Description: Auto install L2TP VPN for CentOS6+/Debian7+/Ubuntu12+ +- Intro: https://teddysun.com/448.html ```bash Usage: l2tp [-l,--list|-a,--add|-d,--del|-m,--mod|-h,--help] @@ -20,8 +20,8 @@ Usage: l2tp [-l,--list|-a,--add|-d,--del|-m,--mod|-h,--help] bench.sh ======== -* Description: Auto test download & I/O speed script -* Intro: https://teddysun.com/444.html +- Description: Auto test download & I/O speed script +- Intro: https://teddysun.com/444.html ```bash Usage: @@ -36,34 +36,36 @@ Usage: backup.sh ========= -* You need to modify the config at first -* Backup MySQL/MariaDB all datebases & files and directories -* Backups are encrypted with AES256-cbc with SHA1 message-digest -* Auto transfer backup file to Google Drive (need install `gdrive`) -* Auto delete Google Drive's remote file (option) +- You must to modify the config before run it +- Backup MySQL/MariaDB/Percona datebases, files and directories +- Backup file is encrypted with AES256-cbc with SHA1 message-digest (option) +- Auto transfer backup file to Google Drive (need install `gdrive` command) (option) +- Auto transfer backup file to FTP server (option) +- Auto delete Google Drive's or FTP server's remote file (option) +- Intro: https://teddysun.com/469.html ```bash Install gdrive step: For x86_64: -wget -O /usr/bin/gdrive http://dl.teddysun.com/files/gdrive-linux-x64 +wget -O /usr/bin/gdrive http://dl.lamp.sh/files/gdrive-linux-x64 chmod +x /usr/bin/gdrive For i386: -wget -O /usr/bin/gdrive http://dl.teddysun.com/files/gdrive-linux-386 +wget -O /usr/bin/gdrive http://dl.lamp.sh/files/gdrive-linux-386 chmod +x /usr/bin/gdrive ``` unixbench.sh ============ -* Description: Auto install unixbench and test script -* Intro: https://teddysun.com/245.html +- Description: Auto install unixbench and test script +- Intro: https://teddysun.com/245.html pptp.sh(Deprecated) =================== -* Description: Auto Install PPTP for CentOS 6 -* Intro: https://teddysun.com/134.html +- Description: Auto Install PPTP for CentOS 6 +- Intro: https://teddysun.com/134.html Copyright (C) 2013-2016 Teddysun From 563aa46665d2e10ab0ab03ea73191eea82a5a9d9 Mon Sep 17 00:00:00 2001 From: Teddysun Date: Tue, 29 Nov 2016 14:25:31 +0900 Subject: [PATCH 10/17] update Swap information --- bench.sh | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/bench.sh b/bench.sh index 9b70d94..59e1a99 100644 --- a/bench.sh +++ b/bench.sh @@ -1,10 +1,13 @@ -#!/bin/bash -#==============================================================# -# Description: bench test shell script # -# Author: Teddysun # -# Thanks: LookBack # -# Visit: https://teddysun.com # -#==============================================================# +#!/usr/bin/env bash +# +# Description: Auto test download & I/O speed script +# +# Copyright (C) 2015 - 2016 Teddysun +# +# Thanks: LookBack +# +# URL: https://teddysun.com/444.html +# if [ ! -e '/usr/bin/wget' ]; then echo "Error: wget command not found. You must be install wget command at first." @@ -104,12 +107,12 @@ freq=$( awk -F: '/cpu MHz/ {freq=$2} END {print freq}' /proc/cpuinfo | sed 's/^[ tram=$( free -m | awk '/Mem/ {print $2}' ) uram=$( free -m | awk '/Mem/ {print $3}' ) swap=$( free -m | awk '/Swap/ {print $2}' ) +uswap=$( free -m | awk '/Swap/ {print $3}' ) up=$( awk '{a=$1/86400;b=($1%86400)/3600;c=($1%3600)/60} {printf("%d days, %d hour %d min\n",a,b,c)}' /proc/uptime ) load=$( w | head -1 | awk -F'load average:' '{print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//' ) opsy=$( get_opsy ) arch=$( uname -m ) lbit=$( getconf LONG_BIT ) -host=$( hostname ) kern=$( uname -r ) ipv6=$( wget -qO- -t1 -T2 ipv6.icanhazip.com ) disk_size1=($( df -ahPl | grep -wvE '\-|none|tmpfs|by-uuid|Filesystem' | awk '{print $2}' )) @@ -124,7 +127,7 @@ echo "Number of cores : $cores" echo "CPU frequency : $freq MHz" echo "Total size of Disk : $disk_total_size GB ($disk_used_size GB Used)" echo "Total amount of Mem : $tram MB ($uram MB Used)" -echo "Total amount of Swap : $swap MB" +echo "Total amount of Swap : $swap MB ($uswap MB Used)" echo "System uptime : $up" echo "Load average : $load" echo "OS : $opsy" From 3cf0099ed6a9acc5a65f7b9f761d61d77faa9e82 Mon Sep 17 00:00:00 2001 From: Teddysun Date: Tue, 29 Nov 2016 15:52:30 +0900 Subject: [PATCH 11/17] update comment --- backup.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/backup.sh b/backup.sh index c76092b..40e5213 100644 --- a/backup.sh +++ b/backup.sh @@ -226,10 +226,10 @@ gdrive_upload() { # Tranferring backup file to FTP server ftp_upload() { if ${FTP_FLG}; then - [ -z ${FTP_HOST} ] && echo "Error: FTP_HOST can not be empty!" && exit 1 - [ -z ${FTP_USER} ] && echo "Error: FTP_USER can not be empty!" && exit 1 - [ -z ${FTP_PASS} ] && echo "Error: FTP_PASS can not be empty!" && exit 1 - [ -z ${FTP_DIR} ] && echo "Error: FTP_DIR can not be empty!" && exit 1 + [ -z ${FTP_HOST} ] && log "Error: FTP_HOST can not be empty!" && exit 1 + [ -z ${FTP_USER} ] && log "Error: FTP_USER can not be empty!" && exit 1 + [ -z ${FTP_PASS} ] && log "Error: FTP_PASS can not be empty!" && exit 1 + [ -z ${FTP_DIR} ] && log "Error: FTP_DIR can not be empty!" && exit 1 local FTP_OUT_FILE=$(basename ${OUT_FILE}) log "Tranferring backup file to FTP server" From 103958d102b033f7dd81f9795dc64bb26459b359 Mon Sep 17 00:00:00 2001 From: Teddysun Date: Tue, 29 Nov 2016 16:59:32 +0900 Subject: [PATCH 12/17] Add ftp_upload.sh --- ftp_upload.sh | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 ftp_upload.sh diff --git a/ftp_upload.sh b/ftp_upload.sh new file mode 100644 index 0000000..d2f56d8 --- /dev/null +++ b/ftp_upload.sh @@ -0,0 +1,105 @@ +#!/usr/bin/env bash +# +# Auto upload file(s) to FTP server +# +# Copyright (C) 2016 Teddysun +# +# Argument example: +# 1) ./ftp_upload.sh filename +# 2) ./ftp_upload.sh filename1 filename2 filename3 ... +# 3) ./ftp_upload.sh *.gz +# + +########## START OF CONFIG ########## + +# Local directory (current folder) +LOCALDIR=$( pwd ) + +# File to log the outcome of backups +LOGFILE="/var/log/ftp_upload.log" + +# FTP server +# Enter the Hostname or IP address below +FTP_HOST="" + +# FTP username +# Enter the FTP username below +FTP_USER="" + +# FTP password +# Enter the username's password below +FTP_PASS="" + +# FTP server remote folder +# Enter the FTP remote folder below +# For example: public_html +FTP_DIR="" + +########## END OF CONFIG ########## + + +log() { + echo "$(date "+%Y-%m-%d %H:%M:%S")" "$1" + echo -e "$(date "+%Y-%m-%d %H:%M:%S")" "$1" >> ${LOGFILE} +} + +# Check ftp command +check_commands() { + # Iterate over the list of binaries, and if one isn't found, abort + if [ ! "$(command -v "ftp")" ]; then + log "ftp command is not installed, please install it and try again" + exit 1 + fi +} + +# Tranferring backup file to FTP server +ftp_upload() { + cd ${LOCALDIR} || exit + + [ -z ${FTP_HOST} ] && log "Error: FTP_HOST can not be empty!" && exit 1 + [ -z ${FTP_USER} ] && log "Error: FTP_USER can not be empty!" && exit 1 + [ -z ${FTP_PASS} ] && log "Error: FTP_PASS can not be empty!" && exit 1 + [ -z ${FTP_DIR} ] && log "Error: FTP_DIR can not be empty!" && exit 1 + + local FTP_OUT_FILE="$1" + echo "${FTP_OUT_FILE}" | grep "*" 2>&1 > /dev/null + if [ $? -eq 0 ]; then + ls ${FTP_OUT_FILE} 2>&1 > /dev/null + [ $? -ne 0 ] && log "Error: [${FTP_OUT_FILE}] file(s) not exists!" && exit 1 + else + [ ! -f ${FTP_OUT_FILE} ] && log "Error: [${FTP_OUT_FILE}] not exists!" && exit 1 + fi + + log "Tranferring [${FTP_OUT_FILE}] to FTP server" + ftp -in ${FTP_HOST} 2>&1 >> ${LOGFILE} < Date: Tue, 29 Nov 2016 17:01:23 +0900 Subject: [PATCH 13/17] update comment --- ftp_upload.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ftp_upload.sh b/ftp_upload.sh index d2f56d8..f381459 100644 --- a/ftp_upload.sh +++ b/ftp_upload.sh @@ -44,8 +44,7 @@ log() { } # Check ftp command -check_commands() { - # Iterate over the list of binaries, and if one isn't found, abort +check_command() { if [ ! "$(command -v "ftp")" ]; then log "ftp command is not installed, please install it and try again" exit 1 @@ -90,7 +89,7 @@ STARTTIME=$(date +%s) log "Upload progress start" -check_commands +check_command for i in $@ do From 8ea5719756fd28f441213300c093976506681736 Mon Sep 17 00:00:00 2001 From: Teddysun Date: Tue, 29 Nov 2016 18:48:21 +0900 Subject: [PATCH 14/17] update comment --- ftp_upload.sh | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/ftp_upload.sh b/ftp_upload.sh index f381459..f86b288 100644 --- a/ftp_upload.sh +++ b/ftp_upload.sh @@ -1,13 +1,13 @@ #!/usr/bin/env bash # -# Auto upload file(s) to FTP server +# Upload file(s) to FTP server # # Copyright (C) 2016 Teddysun # # Argument example: # 1) ./ftp_upload.sh filename # 2) ./ftp_upload.sh filename1 filename2 filename3 ... -# 3) ./ftp_upload.sh *.gz +# 3) ./ftp_upload.sh *.extension # ########## START OF CONFIG ########## @@ -63,7 +63,7 @@ ftp_upload() { local FTP_OUT_FILE="$1" echo "${FTP_OUT_FILE}" | grep "*" 2>&1 > /dev/null if [ $? -eq 0 ]; then - ls ${FTP_OUT_FILE} 2>&1 > /dev/null + ls ${FTP_OUT_FILE} 2>/dev/null [ $? -ne 0 ] && log "Error: [${FTP_OUT_FILE}] file(s) not exists!" && exit 1 else [ ! -f ${FTP_OUT_FILE} ] && log "Error: [${FTP_OUT_FILE}] not exists!" && exit 1 @@ -87,8 +87,6 @@ STARTTIME=$(date +%s) [ $# -eq 0 ] && log "Error: argument can not be empty!" && exit 1 -log "Upload progress start" - check_command for i in $@ @@ -96,8 +94,6 @@ do ftp_upload $i done -log "Upload progress complete" - ENDTIME=$(date +%s) DURATION=$((ENDTIME - STARTTIME)) log "All done" From 5eb44424aeca4e0ce02a132230dabcabd1045593 Mon Sep 17 00:00:00 2001 From: Teddysun Date: Tue, 29 Nov 2016 19:56:59 +0900 Subject: [PATCH 15/17] update upload function --- ftp_upload.sh | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/ftp_upload.sh b/ftp_upload.sh index f86b288..f2fb4c9 100644 --- a/ftp_upload.sh +++ b/ftp_upload.sh @@ -60,25 +60,33 @@ ftp_upload() { [ -z ${FTP_PASS} ] && log "Error: FTP_PASS can not be empty!" && exit 1 [ -z ${FTP_DIR} ] && log "Error: FTP_DIR can not be empty!" && exit 1 - local FTP_OUT_FILE="$1" - echo "${FTP_OUT_FILE}" | grep "*" 2>&1 > /dev/null + echo "$@" | grep "*" > /dev/null 2>&1 if [ $? -eq 0 ]; then - ls ${FTP_OUT_FILE} 2>/dev/null - [ $? -ne 0 ] && log "Error: [${FTP_OUT_FILE}] file(s) not exists!" && exit 1 + ls $@ > /dev/null 2>&1 + [ $? -ne 0 ] && log "Error: [$@] file(s) not exists!" && exit 1 else - [ ! -f ${FTP_OUT_FILE} ] && log "Error: [${FTP_OUT_FILE}] not exists!" && exit 1 + for f in $@ + do + [ ! -f ${f} ] && log "Error: [${f}] not exists!" && exit 1 + done fi - log "Tranferring [${FTP_OUT_FILE}] to FTP server" + local FTP_OUT_FILE=("$@") + + log "Tranferring file(s) list below to FTP server:" + for file in ${FTP_OUT_FILE[@]} + do + log "$file" + done ftp -in ${FTP_HOST} 2>&1 >> ${LOGFILE} < Date: Tue, 29 Nov 2016 22:46:53 +0900 Subject: [PATCH 16/17] update comment --- ftp_upload.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ftp_upload.sh b/ftp_upload.sh index f2fb4c9..5da72ec 100644 --- a/ftp_upload.sh +++ b/ftp_upload.sh @@ -7,7 +7,8 @@ # Argument example: # 1) ./ftp_upload.sh filename # 2) ./ftp_upload.sh filename1 filename2 filename3 ... -# 3) ./ftp_upload.sh *.extension +# 3) ./ftp_upload.sh "*.extension" +# 4) ./ftp_upload.sh "*.extension1" "*.extension2" # ########## START OF CONFIG ########## From bc130a75a146590650b351752e8eb5c4f08783ed Mon Sep 17 00:00:00 2001 From: Teddysun Date: Tue, 29 Nov 2016 22:47:54 +0900 Subject: [PATCH 17/17] update Readme --- Readme.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 1c1f022..b62b87f 100644 --- a/Readme.md +++ b/Readme.md @@ -45,7 +45,7 @@ backup.sh - Intro: https://teddysun.com/469.html ```bash -Install gdrive step: +Install gdrive command step: For x86_64: wget -O /usr/bin/gdrive http://dl.lamp.sh/files/gdrive-linux-x64 @@ -56,6 +56,13 @@ wget -O /usr/bin/gdrive http://dl.lamp.sh/files/gdrive-linux-386 chmod +x /usr/bin/gdrive ``` +ftp_upload.sh +============= + +- You must to modify the config before run it +- Upload file(s) to FTP server +- Intro: https://teddysun.com/484.html + unixbench.sh ============