├── issue_template.md ├── MD5SUM ├── docker-compose.yml ├── Dockerfile ├── LICENSE ├── CHANGLOG.md ├── docker-entrypoint.sh ├── cron_backup.sh ├── butgg.sh ├── cron_backup.bash ├── butgg.bash └── README.md /issue_template.md: -------------------------------------------------------------------------------- 1 | ##### Your OS version: 2 | ##### Command: `` 3 | ##### Detail log: 4 | ``` 5 | ``` 6 | -------------------------------------------------------------------------------- /MD5SUM: -------------------------------------------------------------------------------- 1 | 347fd1bb7ceddefa6beea882a33fce19 butgg.bash 2 | 20677905c2ac692c667491e10e1cae16 cron_backup.bash 3 | b92bd91b4fe47600dfdfa43719c5a2d2 butgg.sh 4 | 45d5555ce301448e8f85176dd6e3db06 cron_backup.sh 5 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | 3 | services: 4 | butgg: 5 | image: mbrother2/backuptogoogle 6 | container_name: butgg 7 | environment: 8 | - GG_CLIENT_ID=xxxxxx 9 | - GG_CLIENT_SECRET=xxxxxx 10 | - TAR_BEFORE_UPLOAD=No 11 | - SYNC_FILE=No 12 | - DAY_REMOVE=7 13 | - GDRIVE_ID=None 14 | - EMAIL_USER=None 15 | - EMAIL_PASS=None 16 | - EMAIL_TO=None 17 | command: cron 18 | volumes: 19 | - /root/gdrive:/root/bin 20 | - /root/conf:/root/.gdrive 21 | - /your_dir_you_want_to_backup_on_host_machine:/root/backup 22 | restart: always 23 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1.12.5-alpine 2 | 3 | LABEL maintainer="mbrother2 " 4 | 5 | ENV GITHUB_LINK https://raw.githubusercontent.com/mbrother2/backuptogoogle/master 6 | ENV GDRIVE_DIR /root/.gdrive 7 | 8 | USER root 9 | 10 | RUN set -x; \ 11 | mkdir ${GDRIVE_DIR}; \ 12 | # Download scripts from Github 13 | apk add --no-cache curl; \ 14 | curl -o /usr/local/bin/cron_backup.bash ${GITHUB_LINK}/cron_backup.bash; \ 15 | chmod 755 /usr/local/bin/cron_backup.bash; \ 16 | # Clone gdrive projert from Github 17 | cd /root; \ 18 | apk add --no-cache git bash; \ 19 | git clone https://github.com/gdrive-org/gdrive.git 20 | 21 | VOLUME /root/.gdrive 22 | VOLUME /root/bin 23 | 24 | COPY docker-entrypoint.sh /usr/local/bin/ 25 | 26 | RUN chmod 755 /usr/local/bin/docker-entrypoint.sh; \ 27 | ln -s usr/local/bin/docker-entrypoint.sh / # backwards compat 28 | 29 | ENTRYPOINT ["docker-entrypoint.sh"] 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 rootorchild 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /CHANGLOG.md: -------------------------------------------------------------------------------- 1 | #### butgg 1.5.1 (12/05/2020) Support docker & docker-compose (beta) 2 | - [BETA] Support docker & docker-compose 3 | 4 | #### butgg 1.5.1 (17/02/2020) Improvement 5 | - [Improvement] Improve check Google Drive folder ID 6 | 7 | #### butgg 1.5.0 (05/12/2019) New feature & Improvement 8 | - [New feature] Compress directory before upload option 9 | - [Improvement] Show help if run butgg.bash with not support option 10 | 11 | #### butgg 1.4.0 (03/12/2019) New feature & Fix bug 12 | - [New feature] Only sync data from local to Google Drive option 13 | - [Fix bug] Run upload normaly if file exclude.list does not exist 14 | 15 | #### butgg 1.3.0 (02/12/2019) New feature 16 | - [New feature] Exclude file/directory in cron backup 17 | 18 | #### butgg 1.2.2 (27/11/2019) Improvement 19 | - [Improvement] Improve check network to connect to Github & Google 20 | 21 | #### butgg 1.2.1 (25/11/2019) Add support 22 | - [Add support] Support Debian 23 | 24 | #### butgg 1.2.0 (23/11/2019) New feature 25 | - [New feature] Send error email if upload to Google Drive fail 26 | 27 | #### butgg 1.1.1 (20/11/2019) Improvement 28 | - [Improvement] Improve check Google folder ID 29 | - [Improvement] Improve detect Google folder upload ID 30 | 31 | #### butgg 1.1.0 (19/11/2019) New feature 32 | - [New feature] Add support upload to specific diectory on Google Drive 33 | 34 | #### butgg 1.0.1 (19/11/2019) Add support 35 | - [Add support] Support SUSE Linux Enterprise 36 | 37 | #### butgg 1.0.0 (17/11/2019) First Release 38 | - [Release] Release butgg 1.0 stable 39 | -------------------------------------------------------------------------------- /docker-entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | DF_BACKUP_DIR="/root/backup" 4 | DF_TAR_BEFORE_UPLOAD="No" 5 | DF_SYNC_FILE="No" 6 | DF_LOG_FILE="/root/.gdrive/butgg.log" 7 | DF_DAY_REMOVE="7" 8 | DF_GDRIVE_ID="None" 9 | DF_EMAIL_USER="None" 10 | DF_EMAIL_PASS="None" 11 | DF_EMAIL_TO="None" 12 | BUTGG_CONF="/root/.gdrive/butgg.conf" 13 | LOG_FILE="/root/.gdrive/butgg.log" 14 | GDRIVE_BIN="/root/bin/gdrive" 15 | 16 | set -e 17 | 18 | OPTION=$1 19 | 20 | # Color variables 21 | GREEN='\e[32m' 22 | RED='\e[31m' 23 | YELLOW='\e[33m' 24 | REMOVE='\e[0m' 25 | 26 | # Change color of words 27 | change_color(){ 28 | case $1 in 29 | green) echo -e "${GREEN}$2${REMOVE}";; 30 | red) echo -e "${RED}$2${REMOVE}";; 31 | yellow) echo -e "${YELLOW}$2${REMOVE}";; 32 | *) echo "$2";; 33 | esac 34 | } 35 | 36 | # Write log 37 | show_write_log(){ 38 | echo "`date "+[ %d/%m/%Y %H:%M:%S ]"` $1" | tee -a ${LOG_FILE} 39 | } 40 | # Write config 41 | write_config(){ 42 | if [ "$3" == "" ] 43 | then 44 | VAR=$1 45 | eval "$VAR"="$2" 46 | if [ -f ${BUTGG_CONF} ] 47 | then 48 | sed -i "/^$1/d" ${BUTGG_CONF} 49 | fi 50 | echo "$1=$2" >> ${BUTGG_CONF} 51 | else 52 | VAR=$1 53 | eval "$VAR"="$3" 54 | if [ -f ${BUTGG_CONF} ] 55 | then 56 | sed -i "/^$1/d" ${BUTGG_CONF} 57 | fi 58 | echo "$1=$3" >> ${BUTGG_CONF} 59 | fi 60 | } 61 | 62 | # Build gdrive 63 | build_gdrive(){ 64 | if [ ! -f ${GDRIVE_BIN} ] 65 | then 66 | cd /root/gdrive 67 | sed -i "s#^const ClientId =.*#const ClientId = \"${GG_CLIENT_ID}\"#g" handlers_drive.go 68 | sed -i "s#^const ClientSecret =.*#const ClientSecret = \"${GG_CLIENT_SECRET}\"#g" handlers_drive.go 69 | go get github.com/prasmussen/gdrive 70 | go build -ldflags '-w -s' 71 | mv /root/gdrive/gdrive ${GDRIVE_BIN} 72 | chmod 755 ${GDRIVE_BIN} 73 | show_write_log "Build gdrive successful." 74 | fi 75 | } 76 | 77 | # Setup cron 78 | setup_cron(){ 79 | echo "0 0 * * * /usr/local/bin/cron_backup.bash" >> /root/cron.txt 80 | crontab /root/cron.txt 81 | rm -f /root/cron.txt 82 | crond -f -L /dev/stdout 83 | } 84 | 85 | # Setup gdrive credential 86 | setup_credential(){ 87 | ${GDRIVE_BIN} about 88 | show_write_log "Setup gdrive credential successful." 89 | } 90 | 91 | # Set up config file 92 | setup_config(){ 93 | show_write_log "Setting up config file..." 94 | echo "" 95 | if [ -z ${DAY_REMOVE} ] 96 | then 97 | read -p " How many days do you want to keep backup on Google Drive?(default ${DF_DAY_REMOVE}): " DAY_REMOVE 98 | fi 99 | if [ -z ${SYNC_FILE} ] 100 | then 101 | echo "" 102 | echo "Read more https://github.com/mbrother2/backuptogoogle/wiki/What-is-the-option-SYNC_FILE%3F" 103 | read -p " Do you want only sync file(default no)(y/n): " SYNC_FILE 104 | fi 105 | if [ -z ${GDRIVE_ID} ] 106 | then 107 | echo "" 108 | echo "Read more https://github.com/mbrother2/backuptogoogle/wiki/Get-Google-folder-ID" 109 | if [ "${SYNC_FILE}" == "y" ] 110 | then 111 | echo "Because you choose sync file method, so you must enter exactly Google folder ID here!" 112 | fi 113 | read -p " Your Google folder ID(default ${DF_GDRIVE_ID}): " GDRIVE_ID 114 | fi 115 | if [ -z ${TAR_BEFORE_UPLOAD} ] 116 | then 117 | if [ "${SYNC_FILE}" == "y" ] 118 | then 119 | TAR_BEFORE_UPLOAD=${DF_TAR_BEFORE_UPLOAD} 120 | else 121 | read -p " Do you want compress directory before upload?(default no)(y/n): " TAR_BEFORE_UPLOAD 122 | fi 123 | fi 124 | if [[ -z ${EMAIL_USER} ]] && [[ -z ${EMAIL_PASS} ]] && [[ -z ${EMAIL_TO} ]] 125 | then 126 | echo "" 127 | echo "Read more https://github.com/mbrother2/backuptogoogle/wiki/Turn-on-2-Step-Verification-&-create-app's-password-for-Google-email" 128 | read -p " Do you want to send email if upload error(default no)(y/n): " SEND_EMAIL 129 | if [ "${SEND_EMAIL}" == "y" ] 130 | then 131 | read -p " Your Google email user name: " EMAIL_USER 132 | read -p " Your Google email password: " EMAIL_PASS 133 | read -p " Which email will be receive notify?: " EMAIL_TO 134 | fi 135 | fi 136 | if [ "${SYNC_FILE}" == "y" ] 137 | then 138 | SYNC_FILE="Yes" 139 | else 140 | SYNC_FILE="No" 141 | fi 142 | if [ "${TAR_BEFORE_UPLOAD}" == "y" ] 143 | then 144 | TAR_BEFORE_UPLOAD="Yes" 145 | else 146 | TAR_BEFORE_UPLOAD=${DF_TAR_BEFORE_UPLOAD} 147 | fi 148 | echo "LOG_FILE=${LOG_FILE}" > ${BUTGG_CONF} 149 | write_config BACKUP_DIR "${DF_BACKUP_DIR}" "${DF_BACKUP_DIR}" 150 | write_config DAY_REMOVE "${DF_DAY_REMOVE}" "${DAY_REMOVE}" 151 | write_config GDRIVE_ID "${DF_GDRIVE_ID}" "${GDRIVE_ID}" 152 | write_config EMAIL_USER "${DF_EMAIL_USER}" "${EMAIL_USER}" 153 | write_config EMAIL_PASS "${DF_EMAIL_PASS}" "${EMAIL_PASS}" 154 | write_config EMAIL_TO "${DF_EMAIL_TO}" "${EMAIL_TO}" 155 | write_config SYNC_FILE "${DF_SYNC_FILE}" "${SYNC_FILE}" 156 | write_config TAR_BEFORE_UPLOAD "${DF_TAR_BEFORE_UPLOAD}" "${TAR_BEFORE_UPLOAD}" 157 | if [ ! -d ${BACKUP_DIR} ] 158 | then 159 | show_write_log "`change_color yellow [WARNING]` You does not mount your backup dir in host machine to ${BACKUP_DIR} on container! Please recreate container and ensure your backup dir has mounted to container." 160 | exit 1 161 | fi 162 | show_write_log "Setup config file successful" 163 | } 164 | 165 | show_write_log "---" 166 | 167 | case "${OPTION}" in 168 | build) 169 | build_gdrive 170 | ;; 171 | credential) 172 | build_gdrive 173 | setup_credential 174 | ;; 175 | config) 176 | setup_config 177 | ;; 178 | setup) 179 | build_gdrive 180 | setup_credential 181 | setup_config 182 | ;; 183 | cron) 184 | build_gdrive 185 | setup_credential 186 | setup_config 187 | setup_cron 188 | ;; 189 | esac 190 | -------------------------------------------------------------------------------- /cron_backup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Setup variables 4 | BUTGG_CONF="${HOME}/.gdrive/butgg.conf" 5 | BUTGG_DEBUG="${HOME}/.gdrive/detail.log" 6 | BUTGG_EXCLUDE="${HOME}/.gdrive/exclude.list" 7 | GDRIVE_BIN="${HOME}/bin/gdrive" 8 | DF_BACKUP_DIR="${HOME}/backup" 9 | DF_SYNC_FILE="No" 10 | DF_LOG_FILE="${HOME}/.gdrive/butgg.log" 11 | DF_DAY_REMOVE="7" 12 | DF_GDRIVE_ID="None" 13 | DF_EMAIL_USER="None" 14 | DF_EMAIL_PASS="None" 15 | DF_EMAIL_TO="None" 16 | FIRST_OPTION=$1 17 | 18 | # Date variables 19 | TODAY=`date +"%d_%m_%Y"` 20 | 21 | # Color variables 22 | GREEN='\e[32m' 23 | RED='\e[31m' 24 | YELLOW='\e[33m' 25 | REMOVE='\e[0m' 26 | 27 | # Change color of words 28 | change_color(){ 29 | case $1 in 30 | green) echo -e "${GREEN}$2${REMOVE}";; 31 | red) echo -e "${RED}$2${REMOVE}";; 32 | yellow) echo -e "${YELLOW}$2${REMOVE}";; 33 | *) echo "$2";; 34 | esac 35 | } 36 | 37 | # Show processing and write log 38 | show_write_log(){ 39 | if [ "${FIRST_OPTION}" == "-v" ] 40 | then 41 | echo `date "+[ %d/%m/%Y %H:%M:%S ]"` $1 42 | fi 43 | echo `date "+[ %d/%m/%Y %H:%M:%S ]"` $1 >> ${LOG_FILE} 44 | } 45 | 46 | # Check file type 47 | check_file_type(){ 48 | if [ -d $1 ] 49 | then 50 | FILE_TYPE="directory" 51 | elif [ -f $1 ] 52 | then 53 | FILE_TYPE="file" 54 | else 55 | show_write_log "`change_color red [CHECKS][FAIL]` Can not detect file type for $1. Exit" 56 | exit 1 57 | fi 58 | } 59 | 60 | # Detect OS 61 | detect_os(){ 62 | show_write_log "Checking OS..." 63 | if [ -f /etc/freebsd-update.conf ] 64 | then 65 | OS="FreeBSD" 66 | else 67 | show_write_log "Sorry! We do not support your OS. Exit" 68 | exit 1 69 | fi 70 | show_write_log "OS supported" 71 | } 72 | 73 | # Write config 74 | check_config(){ 75 | if [ "$3" == "" ] 76 | then 77 | VAR=$1 78 | eval "$VAR"="$2" 79 | if [ $1 == LOG_FILE ] 80 | then 81 | show_write_log "---" 82 | fi 83 | show_write_log "`change_color yellow [WARNING]` $1 does not exist. Use default config" 84 | if [ -f ${BUTGG_CONF} ] 85 | then 86 | sed -i ".${TODAY}" "/^$1/d" ${BUTGG_CONF} 87 | fi 88 | echo "$1=$2" >> ${BUTGG_CONF} 89 | else 90 | VAR=$1 91 | eval "$VAR"="$3" 92 | if [ $1 == LOG_FILE ] 93 | then 94 | show_write_log "---" 95 | fi 96 | fi 97 | } 98 | 99 | # Get config 100 | get_config(){ 101 | if [ ! -f ${BUTGG_CONF} ] 102 | then 103 | check_config LOG_FILE ${DF_LOG_FILE} 104 | check_config BACKUP_DIR ${DF_BACKUP_DIR} 105 | check_config DAY_REMOVE ${DF_DAY_REMOVE} 106 | check_config GDRIVE_ID ${DF_GDRIVE_ID} 107 | check_config EMAIL_USER ${DF_EMAIL_USER} 108 | check_config EMAIL_PASS ${DF_EMAIL_PASS} 109 | check_config EMAIL_TO ${DF_EMAIL_TO} 110 | check_config SYNC_FILE ${DF_SYNC_FILE} 111 | else 112 | LOG_FILE=`cat ${BUTGG_CONF} | grep "^LOG_FILE" | cut -d"=" -f2 | sed 's/"//g' | sed "s/'//g"` 113 | check_config LOG_FILE ${DF_LOG_FILE} ${LOG_FILE} 114 | BACKUP_DIR=`cat ${BUTGG_CONF} | grep "^BACKUP_DIR" | cut -d"=" -f2 | sed 's/"//g' | sed "s/'//g"` 115 | check_config BACKUP_DIR ${DF_BACKUP_DIR} ${BACKUP_DIR} 116 | DAY_REMOVE=`cat ${BUTGG_CONF} | grep "^DAY_REMOVE" | cut -d"=" -f2 | sed 's/"//g' | sed "s/'//g"` 117 | check_config DAY_REMOVE ${DF_DAY_REMOVE} ${DAY_REMOVE} 118 | GDRIVE_ID=`cat ${BUTGG_CONF} | grep "^GDRIVE_ID" | cut -d"=" -f2 | sed 's/"//g' | sed "s/'//g"` 119 | check_config GDRIVE_ID ${DF_GDRIVE_ID} ${GDRIVE_ID} 120 | EMAIL_USER=`cat ${BUTGG_CONF} | grep "^EMAIL_USER" | cut -d"=" -f2 | sed 's/"//g' | sed "s/'//g"` 121 | check_config EMAIL_USER ${DF_EMAIL_USER} ${EMAIL_USER} 122 | EMAIL_PASS=`cat ${BUTGG_CONF} | grep "^EMAIL_PASS" | cut -d"=" -f2 | sed 's/"//g' | sed "s/'//g"` 123 | check_config EMAIL_PASS ${DF_EMAIL_PASS} ${EMAIL_PASS} 124 | EMAIL_TO=`cat ${BUTGG_CONF} | grep "^EMAIL_TO" | cut -d"=" -f2 | sed 's/"//g' | sed "s/'//g"` 125 | check_config EMAIL_TO ${DF_EMAIL_TO} ${EMAIL_TO} 126 | SYNC_FILE=`cat ${BUTGG_CONF} | grep "^SYNC_FILE" | cut -d"=" -f2 | sed 's/"//g' | sed "s/'//g"` 127 | check_config SYNC_FILE ${DF_SYNC_FILE} ${SYNC_FILE} 128 | fi 129 | } 130 | 131 | # Check infomations before upload to Google Drive 132 | check_info(){ 133 | if [ ! -d "${BACKUP_DIR}" ] 134 | then 135 | show_write_log "`change_color red [CHECKS][FAIL]` Directory ${BACKUP_DIR} does not exist. Exit" 136 | send_error_email "butgg [CHECKS][FAIL]" "Directory ${BACKUP_DIR} does not exist" 137 | exit 1 138 | fi 139 | if [ ! -f ${HOME}/.gdrive/token_v2.json ] 140 | then 141 | show_write_log "`change_color red [CHECKS][FAIL]` File ${HOME}/.gdrive/token_v2.json does not exist. Exit" 142 | show_write_log "Please run command: '${GDRIVE_BIN} about' to create your Google token for gdrive" 143 | send_error_email "butgg [CHECKS][FAIL]" "File ${HOME}/.gdrive/token_v2.json does not exist" 144 | exit 1 145 | else 146 | echo "\n" | ${GDRIVE_BIN} list >/dev/null 147 | if [ $? -ne 0 ] 148 | then 149 | echo "" 150 | show_write_log "`change_color red [CHECKS][FAIL]` File ${HOME}/.gdrive/token_v2.json exists but can not verify Google token for gdrive. Exit" 151 | show_write_log "Please run command: '${GDRIVE_BIN} about' to recreate your Google token for gdrive" 152 | show_write_log "Please run command: 'butgg.bash --setup credential' to recreate your Google token for gdrive" 153 | send_error_email "butgg [CHECKS][FAIL]" "File ${HOME}/.gdrive/token_v2.json exists but can not verify Google token for gdrive" 154 | exit 1 155 | fi 156 | fi 157 | } 158 | 159 | # Send error email 160 | send_error_email(){ 161 | if [ "${EMAIL_USER}" == "None" ] 162 | then 163 | show_write_log "`change_color yellow [WARNING]` Email not config, do not send error email" 164 | else 165 | show_write_log "Sending error email..." 166 | curl -s --url "smtp://smtp.gmail.com:587" --ssl-reqd --mail-from "${EMAIL_USER}" --mail-rcpt "${EMAIL_TO}" --user "${EMAIL_USER}:${EMAIL_PASS}" -T <(echo -e "From: ${EMAIL_USER}\nTo: ${EMAIL_TO}\nSubject: $1\n\n $2") 167 | if [ $? -ne 0 ] 168 | then 169 | echo `date "+[ %d/%m/%Y %H:%M:%S ]"` "---" >> ${BUTGG_DEBUG} 170 | curl -v --url "smtp://smtp.gmail.com:587" --ssl-reqd --mail-from "${EMAIL_USER}" --mail-rcpt "${EMAIL_TO}" --user "${EMAIL_USER}:${EMAIL_PASS}" -T <(echo -e "From: ${EMAIL_USER}\nTo: ${EMAIL_TO}\nSubject: $1\n\n $2") --stderr ${BUTGG_DEBUG}_${TODAY} 171 | cat ${BUTGG_DEBUG}_${TODAY} >> ${BUTGG_DEBUG} 172 | rm -f ${BUTGG_DEBUG}_${TODAY} 173 | show_write_log "`change_color red [EMAIL][FAIL]` Can not send error email. See ${BUTGG_DEBUG} for more detail" 174 | else 175 | show_write_log "Send error email successful" 176 | fi 177 | fi 178 | } 179 | 180 | 181 | # Run upload to Google Drive 182 | run_upload(){ 183 | show_write_log "Start upload to Google Drive..." 184 | if [ "${GDRIVE_ID}" == "None" ] 185 | then 186 | CHECK_BACKUP_DIR=`${GDRIVE_BIN} list -m 100000 --name-width 0 | grep -c "${TODAY}"` 187 | else 188 | show_write_log "Checking Google folder ID..." 189 | CHECK_GDRIVE_ID=`${GDRIVE_BIN} list -m 100000 --name-width 0 | grep " dir " | awk '{print $1}' | grep -c "^${GDRIVE_ID}$"` 190 | if [ $? -ne 0 ] 191 | then 192 | show_write_log "`change_color yellow [CHECKS][FAIL]` Can not find Google folder ID ${GDRIVE_ID} . Exit" 193 | send_error_email "butgg [CHECKS][FAIL]" "Can not find Google folder ID ${GDRIVE_ID}" 194 | exit 1 195 | else 196 | show_write_log "Check Google folder ID successful" 197 | fi 198 | CHECK_BACKUP_DIR=`${GDRIVE_BIN} list --query "\"${GDRIVE_ID}\" in parents trashed = false" -m 100000 --name-width 0 | grep -c "${TODAY}"` 199 | fi 200 | if [ ${CHECK_BACKUP_DIR} -eq 0 ] 201 | then 202 | show_write_log "Directory ${TODAY} does not exist. Creating..." 203 | if [ "${GDRIVE_ID}" == "None" ] 204 | then 205 | ID_DIR=`${GDRIVE_BIN} mkdir ${TODAY} | awk '{print $2}'` 206 | else 207 | ID_DIR=`${GDRIVE_BIN} mkdir -p ${GDRIVE_ID} ${TODAY} | awk '{print $2}'` 208 | fi 209 | else 210 | show_write_log "Directory ${TODAY} existed. Skipping..." 211 | if [ "${GDRIVE_ID}" == "None" ] 212 | then 213 | ID_DIR=`${GDRIVE_BIN} list -m 100000 --name-width 0 | grep "${TODAY}" | head -1 | awk '{print $1}'` 214 | else 215 | ID_DIR=`${GDRIVE_BIN} list --query "\"${GDRIVE_ID}\" in parents and trashed = false" -m 100000 --name-width 0 | grep "${TODAY}" | head -1 | awk '{print $1}'` 216 | fi 217 | fi 218 | if [ ${#ID_DIR} -ne 33 ] 219 | then 220 | show_write_log "`change_color red [CREATE][FAIL]` Can not create directory ${TODAY}" 221 | send_error_email "butgg [CREATE][FAIL]" "Can not create directory ${TODAY}" 222 | exit 1 223 | elif [ ${CHECK_BACKUP_DIR} -eq 0 ] 224 | then 225 | show_write_log "`change_color green [CREATE]` Created directory ${TODAY} with ID ${ID_DIR}" 226 | else 227 | : 228 | fi 229 | BACKUP_DIR=`realpath ${BACKUP_DIR}` 230 | for i in $(ls -1 ${BACKUP_DIR}) 231 | do 232 | check_file_type "${BACKUP_DIR}/$i" 233 | if [ -f "${BUTGG_EXCLUDE}" ] 234 | then 235 | CHECK_EXCLUDE=`cat "${BUTGG_EXCLUDE}" | grep -c "^$i$"` 236 | if [ ${CHECK_EXCLUDE} -ne 0 ] 237 | then 238 | show_write_log "`change_color green [INFO]` ${FILE_TYPE^} $i in list exclude. Skip upload" 239 | continue 240 | fi 241 | fi 242 | show_write_log "Uploading ${FILE_TYPE} ${BACKUP_DIR}/$i to directory ${TODAY}..." 243 | UPLOAD_FILE=`${GDRIVE_BIN} upload -p ${ID_DIR} --recursive ${BACKUP_DIR}/$i` 244 | if [[ "${UPLOAD_FILE}" == *"Error"* ]] || [[ "${UPLOAD_FILE}" == *"Fail"* ]] 245 | then 246 | show_write_log "`change_color red [UPLOAD][FAIL]` Can not upload backup file! ${UPLOAD_FILE}. Exit" 247 | send_error_email "butgg [UPLOAD][FAIL]" "Can not upload backup file! ${UPLOAD_FILE}" 248 | exit 249 | else 250 | show_write_log "`change_color green [UPLOAD]` Uploaded ${FILE_TYPE} ${BACKUP_DIR}/$i to directory ${TODAY}" 251 | fi 252 | done 253 | show_write_log "Finish! All files and directories in ${BACKUP_DIR} are uploaded to Google Drive in directory ${TODAY}" 254 | } 255 | 256 | remove_old_dir(){ 257 | OLD_BACKUP_DAY=`date -v-${DAY_REMOVE}d +%d_%m_%Y` 258 | if [ "${GDRIVE_ID}" == "None" ] 259 | then 260 | OLD_BACKUP_ID=`${GDRIVE_BIN} list -m 100000 --name-width 0 | grep "${OLD_BACKUP_DAY}" | awk '{print $1}'` 261 | else 262 | OLD_BACKUP_ID=`${GDRIVE_BIN} list --query "\"${GDRIVE_ID}\" in parents and trashed = false" -m 100000 --name-width 0 | grep "${OLD_BACKUP_DAY}" | awk '{print $1}'` 263 | fi 264 | if [ "${OLD_BACKUP_ID}" != "" ] 265 | then 266 | ${GDRIVE_BIN} delete -r ${OLD_BACKUP_ID} 267 | if [ "${GDRIVE_ID}" == "None" ] 268 | then 269 | OLD_BACKUP_ID=`${GDRIVE_BIN} list -m 100000 --name-width 0 | grep "${OLD_BACKUP_DAY}" | awk '{print $1}'` 270 | else 271 | OLD_BACKUP_ID=`${GDRIVE_BIN} list --query "\"${GDRIVE_ID}\" in parents and trashed = false" -m 100000 --name-width 0 | grep "${OLD_BACKUP_DAY}" | awk '{print $1}'` 272 | fi 273 | if [ "${OLD_BACKUP_ID}" == "" ] 274 | then 275 | show_write_log "`change_color green [REMOVE]` Removed directory ${OLD_BACKUP_DAY}" 276 | else 277 | show_write_log "`change_color red [REMOVE][FAIL]` Directory ${OLD_BACKUP_DAY} exists but can not remove!" 278 | send_error_email "butgg [REMOVE][FAIL]" "Directory ${OLD_BACKUP_DAY} exists but can not remove!" 279 | fi 280 | else 281 | show_write_log "Directory ${OLD_BACKUP_DAY} does not exist. Nothing need remove!" 282 | fi 283 | } 284 | 285 | run_sync(){ 286 | show_write_log "Checking Google folder ID..." 287 | if [ "${GDRIVE_ID}" == "None" ] 288 | then 289 | show_write_log "`change_color yellow [CHECKS][FAIL]` Google folder ID not config. Exit" 290 | send_error_email "butgg [CHECKS][FAIL]" "Google folder ID not config" 291 | exit 1 292 | else 293 | CHECK_GDRIVE_ID=`${GDRIVE_BIN} list -m 100000 --name-width 0 | grep " dir " | awk '{print $1}' | grep -c "^${GDRIVE_ID}$"` 294 | if [ $? -ne 0 ] 295 | then 296 | show_write_log "`change_color yellow [CHECKS][FAIL]` Can not find Google folder ID ${GDRIVE_ID} . Exit" 297 | send_error_email "butgg [CHECKS][FAIL]" "Can not find Google folder ID ${GDRIVE_ID}" 298 | exit 1 299 | else 300 | show_write_log "Check Google folder ID successful" 301 | fi 302 | fi 303 | show_write_log "Start sync to Google Drive..." 304 | ${GDRIVE_BIN} sync upload --keep-local --delete-extraneous "${BACKUP_DIR}" "${GDRIVE_ID}" | while IFS= read -r line; do printf '[ %s ] %s\n' "$(date '+%d/%m/%Y %H:%M:%S')" "$line" | tee -a ${LOG_FILE}; done 305 | if [ $? -eq 0 ] 306 | then 307 | show_write_log "Finish! All files and directories in ${BACKUP_DIR} are synced to Google Drive" 308 | else 309 | echo `date "+[ %d/%m/%Y %H:%M:%S ]"` "---" >> ${BUTGG_DEBUG} 310 | ${GDRIVE_BIN} sync upload "${BACKUP_DIR}" "${GDRIVE_ID}" >> ${BUTGG_DEBUG} 311 | show_write_log "`change_color red [SYNC][FAIL]` Can not sync file!. See ${BUTGG_DEBUG} for more detail. Exit" 312 | send_error_email "butgg [SYNC][FAIL]" "Can not sync file" 313 | exit 1 314 | fi 315 | } 316 | 317 | # Main functions 318 | get_config 319 | detect_os 320 | check_info 321 | if [ "${SYNC_FILE}" == "No" ] 322 | then 323 | run_upload 324 | remove_old_dir 325 | elif [ "${SYNC_FILE}" == "Yes" ] 326 | then 327 | run_sync 328 | else 329 | show_write_log "`change_color yellow [CHECKS][FAIL]` Option SYNC_FILE=${SYNC_FILE} not support. Only Yes or No. Exit" 330 | send_error_email "butgg [CHECKS][FAIL]" "Option SYNC_FILE=${SYNC_FILE} not support" 331 | exit 1 332 | fi 333 | -------------------------------------------------------------------------------- /butgg.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Set variables 4 | GITHUB_LINK="https://raw.githubusercontent.com/mbrother2/backuptogoogle/master" 5 | GO_FILE="go1.12.5.freebsd-amd64" 6 | BUTGG_CONF="${HOME}/.gdrive/butgg.conf" 7 | DF_BACKUP_DIR="${HOME}/backup" 8 | DF_SYNC_FILE="No" 9 | DF_LOG_FILE="${HOME}/.gdrive/butgg.log" 10 | DF_DAY_REMOVE="7" 11 | DF_GDRIVE_ID="None" 12 | DF_EMAIL_USER="None" 13 | DF_EMAIL_PASS="None" 14 | DF_EMAIL_TO="None" 15 | GDRIVE_BIN="${HOME}/bin/gdrive" 16 | GDRIVE_TOKEN="${HOME}/.gdrive/token_v2.json" 17 | CRON_BACKUP="${HOME}/bin/cron_backup.sh" 18 | SETUP_FILE="${HOME}/bin/butgg.sh" 19 | CRON_TEMP="${HOME}/.gdrive/old_cron" 20 | SECOND_OPTION=$2 21 | 22 | # Date variables 23 | TODAY=`date +"%d_%m_%Y"` 24 | 25 | # Color variables 26 | GREEN='\e[32m' 27 | RED='\e[31m' 28 | YELLOW='\e[33m' 29 | REMOVE='\e[0m' 30 | 31 | # Change color of words 32 | change_color(){ 33 | case $1 in 34 | green) echo -e "${GREEN}$2${REMOVE}";; 35 | red) echo -e "${RED}$2${REMOVE}";; 36 | yellow) echo -e "${YELLOW}$2${REMOVE}";; 37 | *) echo "$2";; 38 | esac 39 | } 40 | 41 | # Check MD5 of downloaded file 42 | check_md5sum(){ 43 | curl -o $2 ${GITHUB_LINK}/$1 44 | ORIGIN_MD5=`curl -s ${GITHUB_LINK}/MD5SUM | grep $1 | awk '{print $1}'` 45 | LOCAL_MD5=`md5 $2 | awk '{print $4}'` 46 | if [ "${ORIGIN_MD5}" == "${LOCAL_MD5}" ] 47 | then 48 | show_write_log "Check md5sum for file $1 successful" 49 | else 50 | show_write_log "`change_color red [CHECKS][FAIL]` Can not verify md5 for file $1. Exit!" 51 | exit 1 52 | fi 53 | } 54 | 55 | # Check log file 56 | check_log_file(){ 57 | if [ ! -f ${BUTGG_CONF} ] 58 | then 59 | LOG_FILE=${DF_LOG_FILE} 60 | else 61 | LOG_FILE=`cat ${BUTGG_CONF} | grep "^LOG_FILE" | cut -d"=" -f2 | sed 's/"//g' | sed "s/'//g"` 62 | if [ "${LOG_FILE}" == "" ] 63 | then 64 | LOG_FILE=${DF_LOG_FILE} 65 | fi 66 | fi 67 | create_dir .gdrive 68 | create_dir bin 69 | } 70 | 71 | # Write log 72 | show_write_log(){ 73 | echo "`date "+[ %d/%m/%Y %H:%M:%S ]"` $1" | tee -a ${LOG_FILE} 74 | } 75 | 76 | # Create necessary directory 77 | create_dir(){ 78 | if [ ! -d ${HOME}/$1 ] 79 | then 80 | mkdir -p ${HOME}/$1 81 | if [ ! -d ${HOME}/$1 ] 82 | then 83 | echo "Can not create directory ${HOME}/$1. Exit" 84 | exit 1 85 | else 86 | if [ "$1" == ".gdrive" ] 87 | then 88 | show_write_log "---" 89 | show_write_log "Creating necessary directory..." 90 | fi 91 | show_write_log "Create directory ${HOME}/$1 successful" 92 | fi 93 | else 94 | if [ "$1" == ".gdrive" ] 95 | then 96 | show_write_log "---" 97 | show_write_log "Creating necessary directory..." 98 | fi 99 | show_write_log "Directory ${HOME}/$1 existed. Skip" 100 | fi 101 | echo 1 >> ${HOME}/$1/test.txt 102 | if [ $? -ne 0 ] 103 | then 104 | echo "Can not write to ${HOME}/$1. Exit" 105 | exit 1 106 | else 107 | show_write_log "Check write to ${HOME}/$1 successful" 108 | fi 109 | rm -f ${HOME}/$1/test.txt 110 | } 111 | 112 | check_package(){ 113 | which $1 114 | if [ $? -ne 0 ] 115 | then 116 | show_write_log "Command $1 not found. Trying to install $1..." 117 | sleep 3 118 | ${INSTALL_CM} install -y $1 119 | which $1 120 | if [ $? -ne 0 ] 121 | then 122 | show_write_log "Can not install $1 package. Please install $1 manually." 123 | exit 1 124 | fi 125 | fi 126 | show_write_log "Package $1 is installed" 127 | } 128 | 129 | # Write config 130 | write_config(){ 131 | if [ "$3" == "" ] 132 | then 133 | VAR=$1 134 | eval "$VAR"="$2" 135 | if [ -f ${BUTGG_CONF} ] 136 | then 137 | sed -i "/^$1/d" ${BUTGG_CONF} 138 | fi 139 | echo "$1=$2" >> ${BUTGG_CONF} 140 | else 141 | VAR=$1 142 | eval "$VAR"="$3" 143 | if [ -f ${BUTGG_CONF} ] 144 | then 145 | sed -i "/^$1/d" ${BUTGG_CONF} 146 | fi 147 | echo "$1=$3" >> ${BUTGG_CONF} 148 | fi 149 | } 150 | 151 | # Check network 152 | check_network(){ 153 | show_write_log "Cheking network..." 154 | curl -sI raw.githubusercontent.com >/dev/null 155 | if [ $? -eq 0 ] 156 | then 157 | show_write_log "Connect Github successful" 158 | else 159 | show_write_log "`change_color red [CHECKS][FAIL]` Can not connect to Github file, please check your network. Exit" 160 | exit 1 161 | fi 162 | curl -sI dl.google.com >/dev/null 163 | if [ $? -eq 0 ] 164 | then 165 | show_write_log "Connect Google successful" 166 | else 167 | show_write_log "`change_color red [CHECKS][FAIL]` Can not connect to Google file, please check your network. Exit" 168 | exit 1 169 | fi 170 | } 171 | 172 | # Detect OS 173 | detect_os(){ 174 | show_write_log "Checking OS..." 175 | if [ -f /etc/freebsd-update.conf ] 176 | then 177 | SYS="BSD" 178 | OS="FreeBSD" 179 | INSTALL_CM="pkg" 180 | else 181 | show_write_log "Sorry! We do not support your OS. Exit" 182 | exit 1 183 | fi 184 | show_write_log "OS supported" 185 | show_write_log "Checking necessary package..." 186 | check_package curl 187 | if [ "${FIRST_OPTION}" != "--update" ] 188 | then 189 | check_package git 190 | fi 191 | } 192 | 193 | # Download file from Github 194 | download_file(){ 195 | show_write_log "Downloading script cron file from github..." 196 | check_md5sum cron_backup.sh "${CRON_BACKUP}" 197 | show_write_log "Downloading setup file from github..." 198 | check_md5sum butgg.sh "${SETUP_FILE}" 199 | chmod 755 ${CRON_BACKUP} ${SETUP_FILE} 200 | } 201 | 202 | # Build GDRIVE_BIN 203 | build_gdrive(){ 204 | cd $HOME/bin 205 | show_write_log "Downloading go from Google..." 206 | curl -o ${GO_FILE}.tar.gz https://dl.google.com/go/${GO_FILE}.tar.gz 207 | show_write_log "Extracting go lang..." 208 | tar -xf ${GO_FILE}.tar.gz 209 | show_write_log "Cloning gdrive project from Github..." 210 | rm -rf gdrive 211 | git clone https://github.com/gdrive-org/gdrive.git 212 | show_write_log "Build your own gdrive!" 213 | echo "" 214 | echo "Read more https://github.com/mbrother2/backuptogoogle/wiki/Create-own-Google-credential-step-by-step" 215 | read -p " Your Google API client_id: " gg_client_id 216 | read -p " Your Google API client_secret: " gg_client_secret 217 | sed -i ".${TODAY}" "s#^const ClientId =.*#const ClientId = \"${gg_client_id}\"#g" $HOME/bin/gdrive/handlers_drive.go 218 | sed -i ".${TODAY}" "s#^const ClientSecret =.*#const ClientSecret = \"${gg_client_secret}\"#g" $HOME/bin/gdrive/handlers_drive.go 219 | echo "" 220 | show_write_log "Building gdrive..." 221 | cd $HOME/bin/gdrive 222 | $HOME/bin/go/bin/go get github.com/prasmussen/gdrive 223 | $HOME/bin/go/bin/go build -ldflags '-w -s' 224 | if [ $? -ne 0 ] 225 | then 226 | show_write_log "`change_color red [ERROR]` Can not build gdrive. Exit" 227 | exit 1 228 | else 229 | show_write_log "Build gdrive successful. Gdrive bin locate here ${GDRIVE_BIN} " 230 | fi 231 | mv $HOME/bin/gdrive/gdrive $HOME/bin/gdrive.bin 232 | chmod 755 $HOME/bin/gdrive.bin 233 | rm -f $HOME/bin/${GO_FILE}.tar.gz 234 | rm -rf $HOME/bin/go 235 | rm -rf $HOME/bin/gdrive 236 | mv $HOME/bin/gdrive.bin $HOME/bin/gdrive 237 | } 238 | 239 | # Setup gdrive credential 240 | setup_credential(){ 241 | show_write_log "Setting up gdrive credential..." 242 | if [ "${SECOND_OPTION}" == "credential" ] 243 | then 244 | if [ -f ${GDRIVE_TOKEN} ] 245 | then 246 | rm -f ${GDRIVE_TOKEN} 247 | fi 248 | fi 249 | ${GDRIVE_BIN} about 250 | if [ $? -ne 0 ] 251 | then 252 | show_write_log "`change_color yellow [WARNING]` Can not create gdrive credential. Please run \"${GDRIVE_BIN} about\" to create it after" 253 | sleep 3 254 | else 255 | show_write_log "Setup gdrive credential successful" 256 | fi 257 | } 258 | 259 | # Set up config file 260 | setup_config(){ 261 | show_write_log "Setting up config file..." 262 | echo "" 263 | read -p " Which directory on your server do you want to upload to Google Drive?(default ${DF_BACKUP_DIR}): " BACKUP_DIR 264 | read -p " How many days do you want to keep backup on Google Drive?(default ${DF_DAY_REMOVE}): " DAY_REMOVE 265 | echo "" 266 | echo "Read more https://github.com/mbrother2/backuptogoogle/wiki/What-is-the-option-SYNC_FILE%3F" 267 | read -p " Do you want only sync file(default no)(y/n): " SYNC_FILE 268 | echo "" 269 | echo "Read more: https://github.com/mbrother2/backuptogoogle/wiki/Get-Google-folder-ID)" 270 | read -p " Your Google folder ID(default ${DF_GDRIVE_ID}): " GDRIVE_ID 271 | echo "" 272 | echo "Read more https://github.com/mbrother2/backuptogoogle/wiki/Turn-on-2-Step-Verification-&-create-app's-password-for-Google-email" 273 | read -p " Do you want to send email if upload error(default no)(y/n): " SEND_EMAIL 274 | if [ "${SEND_EMAIL}" == "y" ] 275 | then 276 | read -p " Your Google email user name: " EMAIL_USER 277 | read -p " Your Google email password: " EMAIL_PASS 278 | read -p " Which email will be receive notify?: " EMAIL_TO 279 | fi 280 | if [ "${SYNC_FILE}" == "y" ] 281 | then 282 | SYNC_FILE="Yes" 283 | else 284 | SYNC_FILE="No" 285 | fi 286 | echo "" 287 | echo "LOG_FILE=${LOG_FILE}" > ${BUTGG_CONF} 288 | write_config BACKUP_DIR "${DF_BACKUP_DIR}" "${BACKUP_DIR}" 289 | write_config DAY_REMOVE "${DF_DAY_REMOVE}" "${DAY_REMOVE}" 290 | write_config GDRIVE_ID "${DF_GDRIVE_ID}" "${GDRIVE_ID}" 291 | write_config EMAIL_USER "${DF_EMAIL_USER}" "${EMAIL_USER}" 292 | write_config EMAIL_PASS "${DF_EMAIL_PASS}" "${EMAIL_PASS}" 293 | write_config EMAIL_TO "${DF_EMAIL_TO}" "${EMAIL_TO}" 294 | write_config SYNC_FILE "${DF_SYNC_FILE}" "${SYNC_FILE}" 295 | if [ $? -ne 0 ] 296 | then 297 | show_write_log "`change_color red [ERROR]` Can not write config to file ${BUTGG_CONF}. Please check permission of this file. Exit" 298 | exit 1 299 | else 300 | if [ ! -d ${BACKUP_DIR} ] 301 | then 302 | show_write_log "`change_color yellow [WARNING]` Directory ${BACKUP_DIR} does not exist! Ensure you will be create it after." 303 | sleep 3 304 | fi 305 | show_write_log "Setup config file successful" 306 | fi 307 | } 308 | 309 | # Set up cron backup 310 | setup_cron(){ 311 | show_write_log "Setting up cron backup..." 312 | crontab -l > ${CRON_TEMP} 313 | CHECK_CRON=`cat ${CRON_TEMP} | grep -c "cron_backup.sh"` 314 | if [ ${CHECK_CRON} -eq 0 ] 315 | then 316 | echo "PATH=$PATH" >> ${CRON_TEMP} 317 | echo "0 0 * * * sh ${CRON_BACKUP} >/dev/null 2>&1" >> ${CRON_TEMP} 318 | crontab ${CRON_TEMP} 319 | if [ $? -ne 0 ] 320 | then 321 | show_write_log "Can not setup cronjob to backup! Please check again" 322 | SHOW_CRON="`change_color yellow [WARNING]` Can not setup cronjob to backup" 323 | else 324 | show_write_log "Setup cronjob to backup successful" 325 | SHOW_CRON="0 0 * * * sh ${CRON_BACKUP} >/dev/null 2>&1" 326 | fi 327 | else 328 | show_write_log "Cron backup existed. Skip" 329 | SHOW_CRON=`cat ${CRON_TEMP} | grep "cron_backup.sh"` 330 | fi 331 | rm -f ${CRON_TEMP} 332 | } 333 | 334 | # Show information 335 | show_info(){ 336 | echo "" 337 | if [ "${SECOND_OPTION}" == config ] 338 | then 339 | show_write_log "+-----" 340 | show_write_log "| SUCESSFUL! Your information:" 341 | show_write_log "| Backup dir : ${BACKUP_DIR}" 342 | show_write_log "| Keep backup : ${DAY_REMOVE} days" 343 | show_write_log "| Google folder ID: ${GDRIVE_ID}" 344 | show_write_log "| Your email : ${EMAIL_USER}" 345 | show_write_log "| Email password : ${EMAIL_PASS}" 346 | show_write_log "| Email notify : ${EMAIL_TO}" 347 | show_write_log "| Config file : ${BUTGG_CONF}" 348 | show_write_log "+-----" 349 | else 350 | show_write_log "+-----" 351 | show_write_log "| SUCESSFUL! Your information:" 352 | show_write_log "| Backup dir : ${BACKUP_DIR}" 353 | show_write_log "| Config file : ${BUTGG_CONF}" 354 | show_write_log "| Log file : ${LOG_FILE}" 355 | show_write_log "| Keep backup : ${DAY_REMOVE} days" 356 | show_write_log "| Google folder ID: ${GDRIVE_ID}" 357 | show_write_log "| Your email : ${EMAIL_USER}" 358 | show_write_log "| Email password : ${EMAIL_PASS}" 359 | show_write_log "| Email notify : ${EMAIL_TO}" 360 | show_write_log "| butgg.sh file : ${SETUP_FILE}" 361 | show_write_log "| Cron backup file: ${CRON_BACKUP}" 362 | show_write_log "| Gdrive bin file : ${GDRIVE_BIN}" 363 | show_write_log "| Cron backup : ${SHOW_CRON}" 364 | show_write_log "| Google token : ${GDRIVE_TOKEN}" 365 | show_write_log "+-----" 366 | 367 | echo "" 368 | echo " If you get trouble when use butgg.sh please report here:" 369 | echo " https://github.com/mbrother2/backuptogoogle/issues" 370 | fi 371 | } 372 | 373 | _setup(){ 374 | check_log_file 375 | if [ -z "${SECOND_OPTION}" ] 376 | then 377 | detect_os 378 | check_network 379 | download_file 380 | build_gdrive 381 | setup_credential 382 | setup_config 383 | setup_cron 384 | show_info 385 | else 386 | case ${SECOND_OPTION} in 387 | config) 388 | setup_config 389 | show_info 390 | ;; 391 | credential) 392 | setup_credential 393 | ;; 394 | only-build) 395 | detect_os 396 | check_network 397 | build_gdrive 398 | ;; 399 | no-build) 400 | detect_os 401 | check_network 402 | download_file 403 | setup_credential 404 | setup_config 405 | setup_cron 406 | show_info 407 | ;; 408 | no-update) 409 | detect_os 410 | check_network 411 | build_gdrive 412 | setup_credential 413 | setup_config 414 | setup_cron 415 | show_info 416 | ;; 417 | *) 418 | show_write_log "No such command: ${SECOND_OPTION}. Please use butgg.sh --help" 419 | ;; 420 | esac 421 | fi 422 | } 423 | 424 | _update(){ 425 | check_log_file 426 | detect_os 427 | check_network 428 | download_file 429 | } 430 | 431 | _uninstall(){ 432 | check_log_file 433 | show_write_log "Removing all butgg.sh scripts..." 434 | rm -f ${GDRIVE_BIN} ${CRON_BACKUP} ${SETUP_FILE} 435 | if [ $? -ne 0 ] 436 | then 437 | show_write_log "Can not remove all butgg.sh scripts. Please check permission of these files" 438 | else 439 | show_write_log "Remove all butgg.sh scripts successful" 440 | fi 441 | read -p " Do you want remove ${HOME}/.gdrive directory?(y/n) " REMOVE_GDRIVE_DIR 442 | if [ "${REMOVE_GDRIVE_DIR}" == "y" ] || [ "${REMOVE_GDRIVE_DIR}" == "Y" ] 443 | then 444 | rm -rf ${HOME}/.gdrive 445 | if [ $? -ne 0 ] 446 | then 447 | show_write_log "Can not remove directory ${HOME}/.gdrive. Please check permission of this directory" 448 | else 449 | echo "Remove directory ${HOME}/.gdrive successful" 450 | fi 451 | else 452 | show_write_log "Skip remove ${HOME}/.gdrive directory" 453 | fi 454 | } 455 | 456 | _help(){ 457 | echo "butgg.sh - Backup to Google Drive solution" 458 | echo "" 459 | echo "Usage: butgg.sh [options] [command]" 460 | echo "" 461 | echo "Options:" 462 | echo " --help show this help message and exit" 463 | echo " --setup setup or reset all scripts & config file" 464 | echo " config only setup config" 465 | echo " credential only setup credential" 466 | echo " only-build only build gdrive bin" 467 | echo " no-build setup butgg without build gdrive" 468 | echo " no-update setup butgg without update script" 469 | echo " --update update to latest version" 470 | echo " --uninstall remove all butgg scripts and .gdrive directory" 471 | } 472 | 473 | # Main functions 474 | case $1 in 475 | --help) _help ;; 476 | --setup) _setup ;; 477 | --update) _update ;; 478 | --uninstall) _uninstall ;; 479 | *) echo "No such option: $1. Please use butgg.sh --help" ;; 480 | esac 481 | -------------------------------------------------------------------------------- /cron_backup.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Setup variables 4 | BUTGG_CONF="${HOME}/.gdrive/butgg.conf" 5 | BUTGG_DEBUG="${HOME}/.gdrive/detail.log" 6 | BUTGG_EXCLUDE="${HOME}/.gdrive/exclude.list" 7 | GDRIVE_BIN="${HOME}/bin/gdrive" 8 | DF_BACKUP_DIR="${HOME}/backup" 9 | DF_TAR_BEFORE_UPLOAD="No" 10 | DF_SYNC_FILE="No" 11 | DF_LOG_FILE="${HOME}/.gdrive/butgg.log" 12 | DF_DAY_REMOVE="7" 13 | DF_GDRIVE_ID="None" 14 | DF_EMAIL_USER="None" 15 | DF_EMAIL_PASS="None" 16 | DF_EMAIL_TO="None" 17 | FIRST_OPTION=$1 18 | 19 | # Date variables 20 | TODAY=`date +"%d_%m_%Y"` 21 | RANDOM_STRING=`date +%s | sha256sum | base64 | head -c 16` 22 | 23 | # Color variables 24 | GREEN='\e[32m' 25 | RED='\e[31m' 26 | YELLOW='\e[33m' 27 | REMOVE='\e[0m' 28 | 29 | # Change color of words 30 | change_color(){ 31 | case $1 in 32 | green) echo -e "${GREEN}$2${REMOVE}";; 33 | red) echo -e "${RED}$2${REMOVE}";; 34 | yellow) echo -e "${YELLOW}$2${REMOVE}";; 35 | *) echo "$2";; 36 | esac 37 | } 38 | 39 | # Show processing and write log 40 | show_write_log(){ 41 | if [ "${FIRST_OPTION}" == "-v" ] 42 | then 43 | echo `date "+[ %d/%m/%Y %H:%M:%S ]"` $1 44 | fi 45 | echo `date "+[ %d/%m/%Y %H:%M:%S ]"` $1 >> ${LOG_FILE} 46 | } 47 | 48 | # Check file type 49 | check_file_type(){ 50 | if [ -d $1 ] 51 | then 52 | FILE_TYPE="directory" 53 | elif [ -f $1 ] 54 | then 55 | FILE_TYPE="file" 56 | else 57 | show_write_log "`change_color red [CHECKS][FAIL]` Can not detect file type for $1. Exit" 58 | exit 1 59 | fi 60 | } 61 | 62 | # Detect OS 63 | detect_os(){ 64 | show_write_log "Checking OS..." 65 | if [ -f /etc/os-release ] 66 | then 67 | OS=`cat /etc/os-release | grep "^NAME=" | cut -d'"' -f2 | awk '{print $1}'` 68 | show_write_log "OS supported" 69 | else 70 | show_write_log "Sorry! We do not support your OS. Exit" 71 | exit 1 72 | fi 73 | } 74 | 75 | # Write config 76 | check_config(){ 77 | if [ "$3" == "" ] 78 | then 79 | VAR=$1 80 | eval "$VAR"="$2" 81 | if [ $1 == LOG_FILE ] 82 | then 83 | show_write_log "---" 84 | fi 85 | show_write_log "`change_color yellow [WARNING]` $1 does not exist. Use default config" 86 | if [ -f ${BUTGG_CONF} ] 87 | then 88 | sed -i "/^$1/d" ${BUTGG_CONF} 89 | fi 90 | echo "$1=$2" >> ${BUTGG_CONF} 91 | else 92 | VAR=$1 93 | eval "$VAR"="$3" 94 | if [ $1 == LOG_FILE ] 95 | then 96 | show_write_log "---" 97 | fi 98 | fi 99 | } 100 | 101 | # Get config 102 | get_config(){ 103 | if [ ! -f ${BUTGG_CONF} ] 104 | then 105 | check_config LOG_FILE ${DF_LOG_FILE} 106 | check_config BACKUP_DIR ${DF_BACKUP_DIR} 107 | check_config DAY_REMOVE ${DF_DAY_REMOVE} 108 | check_config GDRIVE_ID ${DF_GDRIVE_ID} 109 | check_config EMAIL_USER ${DF_EMAIL_USER} 110 | check_config EMAIL_PASS ${DF_EMAIL_PASS} 111 | check_config EMAIL_TO ${DF_EMAIL_TO} 112 | check_config SYNC_FILE ${DF_SYNC_FILE} 113 | check_config TAR_BEFORE_UPLOAD ${DF_TAR_BEFORE_UPLOAD} 114 | else 115 | LOG_FILE=`cat ${BUTGG_CONF} | grep "^LOG_FILE" | cut -d"=" -f2 | sed 's/"//g' | sed "s/'//g"` 116 | check_config LOG_FILE ${DF_LOG_FILE} ${LOG_FILE} 117 | BACKUP_DIR=`cat ${BUTGG_CONF} | grep "^BACKUP_DIR" | cut -d"=" -f2 | sed 's/"//g' | sed "s/'//g"` 118 | check_config BACKUP_DIR ${DF_BACKUP_DIR} ${BACKUP_DIR} 119 | DAY_REMOVE=`cat ${BUTGG_CONF} | grep "^DAY_REMOVE" | cut -d"=" -f2 | sed 's/"//g' | sed "s/'//g"` 120 | check_config DAY_REMOVE ${DF_DAY_REMOVE} ${DAY_REMOVE} 121 | GDRIVE_ID=`cat ${BUTGG_CONF} | grep "^GDRIVE_ID" | cut -d"=" -f2 | sed 's/"//g' | sed "s/'//g"` 122 | check_config GDRIVE_ID ${DF_GDRIVE_ID} ${GDRIVE_ID} 123 | EMAIL_USER=`cat ${BUTGG_CONF} | grep "^EMAIL_USER" | cut -d"=" -f2 | sed 's/"//g' | sed "s/'//g"` 124 | check_config EMAIL_USER ${DF_EMAIL_USER} ${EMAIL_USER} 125 | EMAIL_PASS=`cat ${BUTGG_CONF} | grep "^EMAIL_PASS" | cut -d"=" -f2 | sed 's/"//g' | sed "s/'//g"` 126 | check_config EMAIL_PASS ${DF_EMAIL_PASS} ${EMAIL_PASS} 127 | EMAIL_TO=`cat ${BUTGG_CONF} | grep "^EMAIL_TO" | cut -d"=" -f2 | sed 's/"//g' | sed "s/'//g"` 128 | check_config EMAIL_TO ${DF_EMAIL_TO} ${EMAIL_TO} 129 | SYNC_FILE=`cat ${BUTGG_CONF} | grep "^SYNC_FILE" | cut -d"=" -f2 | sed 's/"//g' | sed "s/'//g"` 130 | check_config SYNC_FILE ${DF_SYNC_FILE} ${SYNC_FILE} 131 | TAR_BEFORE_UPLOAD=`cat ${BUTGG_CONF} | grep "^TAR_BEFORE_UPLOAD" | cut -d"=" -f2 | sed 's/"//g' | sed "s/'//g"` 132 | check_config TAR_BEFORE_UPLOAD ${DF_TAR_BEFORE_UPLOAD} ${TAR_BEFORE_UPLOAD} 133 | fi 134 | } 135 | 136 | # Check infomations before upload to Google Drive 137 | check_info(){ 138 | if [ ! -d "${BACKUP_DIR}" ] 139 | then 140 | show_write_log "`change_color red [CHECKS][FAIL]` Directory ${BACKUP_DIR} does not exist. Exit" 141 | send_error_email "butgg [CHECKS][FAIL]" "Directory ${BACKUP_DIR} does not exist" 142 | exit 1 143 | fi 144 | if [ ! -f ${HOME}/.gdrive/token_v2.json ] 145 | then 146 | show_write_log "`change_color red [CHECKS][FAIL]` File ${HOME}/.gdrive/token_v2.json does not exist. Exit" 147 | show_write_log "Please run command: '${GDRIVE_BIN} about' to create your Google token for gdrive" 148 | send_error_email "butgg [CHECKS][FAIL]" "File ${HOME}/.gdrive/token_v2.json does not exist" 149 | exit 1 150 | else 151 | echo "\n" | ${GDRIVE_BIN} list >/dev/null 152 | if [ $? -ne 0 ] 153 | then 154 | echo "" 155 | show_write_log "`change_color red [CHECKS][FAIL]` File ${HOME}/.gdrive/token_v2.json exists but can not verify Google token for gdrive. Exit" 156 | show_write_log "Please run command: 'butgg.bash --setup credential' to recreate your Google token for gdrive" 157 | send_error_email "butgg [CHECKS][FAIL]" "File ${HOME}/.gdrive/token_v2.json exists but can not verify Google token for gdrive" 158 | exit 1 159 | fi 160 | fi 161 | } 162 | 163 | # Send error email 164 | send_error_email(){ 165 | if [ "${EMAIL_USER}" == "None" ] 166 | then 167 | show_write_log "`change_color yellow [WARNING]` Email not config, do not send error email" 168 | else 169 | show_write_log "Sending error email..." 170 | curl -s --url "smtp://smtp.gmail.com:587" --ssl-reqd --mail-from "${EMAIL_USER}" --mail-rcpt "${EMAIL_TO}" --user "${EMAIL_USER}:${EMAIL_PASS}" -T <(echo -e "From: ${EMAIL_USER}\nTo: ${EMAIL_TO}\nSubject: $1\n\n $2") 171 | if [ $? -ne 0 ] 172 | then 173 | echo "" >> ${BUTGG_DEBUG} 174 | echo `date "+[ %d/%m/%Y %H:%M:%S ]"` "---" >> ${BUTGG_DEBUG} 175 | curl -v --url "smtp://smtp.gmail.com:587" --ssl-reqd --mail-from "${EMAIL_USER}" --mail-rcpt "${EMAIL_TO}" --user "${EMAIL_USER}:${EMAIL_PASS}" -T <(echo -e "From: ${EMAIL_USER}\nTo: ${EMAIL_TO}\nSubject: $1\n\n $2") --stderr ${BUTGG_DEBUG}_${TODAY} 176 | cat ${BUTGG_DEBUG}_${TODAY} >> ${BUTGG_DEBUG} 177 | rm -f ${BUTGG_DEBUG}_${TODAY} 178 | show_write_log "`change_color red [EMAIL][FAIL]` Can not send error email. See ${BUTGG_DEBUG} for more detail" 179 | else 180 | show_write_log "Send error email successful" 181 | fi 182 | fi 183 | } 184 | 185 | # Run upload to Google Drive 186 | run_upload(){ 187 | show_write_log "Start upload to Google Drive..." 188 | if [ "${GDRIVE_ID}" == "None" ] 189 | then 190 | CHECK_BACKUP_DIR=`${GDRIVE_BIN} list --query "'root' in parents and trashed = false" -m 100000 --name-width 0 | grep -c "${TODAY}"` 191 | else 192 | show_write_log "Checking Google folder ID..." 193 | CHECK_GDRIVE_ID=`${GDRIVE_BIN} info "${GDRIVE_ID}" | grep -c "Error 404: File not found:"` 194 | if [ ${CHECK_GDRIVE_ID} -ne 0 ] 195 | then 196 | show_write_log "`change_color yellow [CHECKS][FAIL]` Can not find Google folder ID ${GDRIVE_ID} . Exit" 197 | send_error_email "butgg [CHECKS][FAIL]" "Can not find Google folder ID ${GDRIVE_ID}" 198 | exit 1 199 | else 200 | show_write_log "Check Google folder ID successful" 201 | fi 202 | CHECK_BACKUP_DIR=`${GDRIVE_BIN} list --query "\"${GDRIVE_ID}\" in parents and trashed = false" -m 100000 --name-width 0 | grep -c "${TODAY}"` 203 | fi 204 | if [ ${CHECK_BACKUP_DIR} -eq 0 ] 205 | then 206 | show_write_log "Directory ${TODAY} does not exist. Creating..." 207 | if [ "${GDRIVE_ID}" == "None" ] 208 | then 209 | ID_DIR=`${GDRIVE_BIN} mkdir ${TODAY} | awk '{print $2}'` 210 | else 211 | ID_DIR=`${GDRIVE_BIN} mkdir -p ${GDRIVE_ID} ${TODAY} | awk '{print $2}'` 212 | fi 213 | else 214 | show_write_log "Directory ${TODAY} existed. Skipping..." 215 | if [ "${GDRIVE_ID}" == "None" ] 216 | then 217 | ID_DIR=`${GDRIVE_BIN} list --query "'root' in parents and trashed = false" -m 100000 --name-width 0 | grep "${TODAY}" | head -1 | awk '{print $1}'` 218 | else 219 | ID_DIR=`${GDRIVE_BIN} list --query "\"${GDRIVE_ID}\" in parents and trashed = false" -m 100000 --name-width 0 | grep "${TODAY}" | head -1 | awk '{print $1}'` 220 | fi 221 | fi 222 | if [ ${#ID_DIR} -ne 33 ] 223 | then 224 | show_write_log "`change_color red [CREATE][FAIL]` Can not create directory ${TODAY}" 225 | send_error_email "butgg [CREATE][FAIL]" "Can not create directory ${TODAY}" 226 | exit 1 227 | elif [ ${CHECK_BACKUP_DIR} -eq 0 ] 228 | then 229 | show_write_log "`change_color green [CREATE]` Created directory ${TODAY} with ID ${ID_DIR}" 230 | else 231 | : 232 | fi 233 | if [ "${TAR_BEFORE_UPLOAD}" == "Yes" ] 234 | then 235 | show_write_log "Compressing backup directory..." 236 | cd ${BACKUP_DIR} 237 | BACKUP_DIR_NAME=`basename ${BACKUP_DIR}` 238 | tar -zcf ${BACKUP_DIR_NAME}_${RANDOM_STRING}.tar.gz * 239 | if [ $? -ne 0 ] 240 | then 241 | show_write_log "`change_color red [COMPRESS][FAIL]` Can not compress ${BACKUP_DIR}. Exit" 242 | send_error_email "butgg [COMPRESS][FAIL]" "Can not compress ${BACKUP_DIR}" 243 | exit 1 244 | else 245 | show_write_log "`change_color green [COMPRESS]` Compress ${BACKUP_DIR} successful" 246 | fi 247 | show_write_log "Uploading ${BACKUP_DIR_NAME}_${RANDOM_STRING}.tar.gz to directory ${TODAY}..." 248 | UPLOAD_FILE=`${GDRIVE_BIN} upload -p ${ID_DIR} ${BACKUP_DIR_NAME}_${RANDOM_STRING}.tar.gz` 249 | if [[ "${UPLOAD_FILE}" == *"Error"* ]] || [[ "${UPLOAD_FILE}" == *"Fail"* ]] 250 | then 251 | show_write_log "`change_color red [UPLOAD][FAIL]` Can not upload backup file ${BACKUP_DIR_NAME}_${RANDOM_STRING}.tar.gz. Exit" 252 | send_error_email "butgg [UPLOAD][FAIL]" "Can not upload backup file ${BACKUP_DIR_NAME}_${RANDOM_STRING}.tar.gz" 253 | exit 1 254 | else 255 | show_write_log "`change_color green [UPLOAD]` Uploaded ${BACKUP_DIR_NAME}_${RANDOM_STRING}.tar.gz to directory ${TODAY}" 256 | fi 257 | show_write_log "Removing ${BACKUP_DIR_NAME}_${RANDOM_STRING}.tar.gz after upload..." 258 | rm -f ${BACKUP_DIR_NAME}_${RANDOM_STRING}.tar.gz 259 | if [ $? -ne 0 ] 260 | then 261 | show_write_log "`change_color red [REMOVE][FAIL]` Can not remove ${BACKUP_DIR_NAME}_${RANDOM_STRING}.tar.gz. You must delete it manually" 262 | send_error_email "butgg [REMOVE][FAIL]" "Can not remove ${BACKUP_DIR_NAME}_${RANDOM_STRING}.tar.gz. You must delete it manually" 263 | else 264 | show_write_log "`change_color green [REMOVE]` Remove ${BACKUP_DIR_NAME}_${RANDOM_STRING}.tar.gz successful" 265 | fi 266 | elif [ "${TAR_BEFORE_UPLOAD}" == "No" ] 267 | then 268 | show_write_log "`change_color green [INFO]` You do not compress directory before upload" 269 | BACKUP_DIR=`realpath ${BACKUP_DIR}` 270 | for i in $(ls -1 ${BACKUP_DIR}) 271 | do 272 | check_file_type "${BACKUP_DIR}/$i" 273 | if [ -f "${BUTGG_EXCLUDE}" ] 274 | then 275 | CHECK_EXCLUDE=`cat "${BUTGG_EXCLUDE}" | grep -c "^$i$"` 276 | if [ ${CHECK_EXCLUDE} -ne 0 ] 277 | then 278 | show_write_log "`change_color green [INFO]` ${FILE_TYPE^} $i in list exclude. Skip upload" 279 | continue 280 | fi 281 | fi 282 | show_write_log "Uploading ${FILE_TYPE} ${BACKUP_DIR}/$i to directory ${TODAY}..." 283 | UPLOAD_FILE=`${GDRIVE_BIN} upload -p ${ID_DIR} --recursive ${BACKUP_DIR}/$i` 284 | if [[ "${UPLOAD_FILE}" == *"Error"* ]] || [[ "${UPLOAD_FILE}" == *"Fail"* ]] 285 | then 286 | show_write_log "`change_color red [UPLOAD][FAIL]` Can not upload backup file ${UPLOAD_FILE}. Exit" 287 | send_error_email "butgg [UPLOAD][FAIL]" "Can not upload backup file! ${UPLOAD_FILE}" 288 | exit 1 289 | else 290 | show_write_log "`change_color green [UPLOAD]` Uploaded ${FILE_TYPE} ${BACKUP_DIR}/$i to directory ${TODAY}" 291 | fi 292 | done 293 | show_write_log "Finish! All files and directories in ${BACKUP_DIR} are uploaded to Google Drive in directory ${TODAY}" 294 | else 295 | show_write_log "`change_color yellow [CHECKS][FAIL]` Option TAR_BEFORE_UPLOAD=${TAR_BEFORE_UPLOAD} not support. Only Yes or No. Exit" 296 | send_error_email "butgg [CHECKS][FAIL]" "Option TAR_BEFORE_UPLOAD=${TAR_BEFORE_UPLOAD} not support" 297 | exit 1 298 | fi 299 | } 300 | 301 | remove_old_dir(){ 302 | OLD_BACKUP_DAY=`date +%d_%m_%Y -d "-${DAY_REMOVE} day"` 303 | if [ "${GDRIVE_ID}" == "None" ] 304 | then 305 | OLD_BACKUP_ID=`${GDRIVE_BIN} list --query "'root' in parents and trashed = false" -m 100000 --name-width 0 | grep "${OLD_BACKUP_DAY}" | awk '{print $1}'` 306 | else 307 | OLD_BACKUP_ID=`${GDRIVE_BIN} list --query "\"${GDRIVE_ID}\" in parents and trashed = false" -m 100000 --name-width 0 | grep "${OLD_BACKUP_DAY}" | awk '{print $1}'` 308 | fi 309 | if [ "${OLD_BACKUP_ID}" != "" ] 310 | then 311 | ${GDRIVE_BIN} delete -r ${OLD_BACKUP_ID} 312 | if [ "${GDRIVE_ID}" == "None" ] 313 | then 314 | OLD_BACKUP_ID=`${GDRIVE_BIN} list --query "'root' in parents and trashed = false" -m 100000 --name-width 0 | grep "${OLD_BACKUP_DAY}" | awk '{print $1}'` 315 | else 316 | OLD_BACKUP_ID=`${GDRIVE_BIN} list --query "\"${GDRIVE_ID}\" in parents and trashed = false" -m 100000 --name-width 0 | grep "${OLD_BACKUP_DAY}" | awk '{print $1}'` 317 | fi 318 | if [ "${OLD_BACKUP_ID}" == "" ] 319 | then 320 | show_write_log "`change_color green [REMOVE]` Removed directory ${OLD_BACKUP_DAY}" 321 | else 322 | show_write_log "`change_color red [REMOVE][FAIL]` Directory ${OLD_BACKUP_DAY} exists but can not remove!" 323 | send_error_email "butgg [REMOVE][FAIL]" "Directory ${OLD_BACKUP_DAY} exists but can not remove!" 324 | exit 1 325 | fi 326 | else 327 | show_write_log "Directory ${OLD_BACKUP_DAY} does not exist. Nothing need remove!" 328 | fi 329 | } 330 | 331 | run_sync(){ 332 | show_write_log "Checking Google folder ID..." 333 | if [ "${GDRIVE_ID}" == "None" ] 334 | then 335 | show_write_log "`change_color yellow [CHECKS][FAIL]` Google folder ID not config. Exit" 336 | send_error_email "butgg [CHECKS][FAIL]" "Google folder ID not config" 337 | exit 1 338 | else 339 | CHECK_GDRIVE_ID=`${GDRIVE_BIN} info "${GDRIVE_ID}" | grep -c "Error 404: File not found:"` 340 | if [ ${CHECK_GDRIVE_ID} -ne 0 ] 341 | then 342 | show_write_log "`change_color yellow [CHECKS][FAIL]` Can not find Google folder ID ${GDRIVE_ID} . Exit" 343 | send_error_email "butgg [CHECKS][FAIL]" "Can not find Google folder ID ${GDRIVE_ID}" 344 | exit 1 345 | else 346 | show_write_log "Check Google folder ID successful" 347 | fi 348 | fi 349 | show_write_log "Start sync to Google Drive..." 350 | ${GDRIVE_BIN} sync upload --no-progress --keep-local --delete-extraneous "${BACKUP_DIR}" "${GDRIVE_ID}" | while IFS= read -r line; do printf '[ %s ] %s\n' "$(date '+%d/%m/%Y %H:%M:%S')" "$line" | tee -a ${HOME}/${RANDOM_STRING}.tmp; done 351 | CHECK_SYNC=`cat ${HOME}/${RANDOM_STRING}.tmp | grep -c "Root directory is not empty"` 352 | if [ ${CHECK_SYNC} -eq 0 ] 353 | then 354 | cat ${HOME}/${RANDOM_STRING}.tmp >> ${LOG_FILE} 355 | rm -f ${HOME}/${RANDOM_STRING}.tmp 356 | show_write_log "Finish! All files and directories in ${BACKUP_DIR} are synced to Google Drive" 357 | else 358 | echo "" >> ${BUTGG_DEBUG} 359 | echo `date "+[ %d/%m/%Y %H:%M:%S ]"` "---" >> ${BUTGG_DEBUG} 360 | cat ${HOME}/${RANDOM_STRING}.tmp >> ${BUTGG_DEBUG} 361 | rm -f ${HOME}/${RANDOM_STRING}.tmp 362 | show_write_log "`change_color red [SYNC][FAIL]` Can not sync file!. See ${BUTGG_DEBUG} for more detail. Exit" 363 | send_error_email "butgg [SYNC][FAIL]" "Can not sync file" 364 | exit 1 365 | fi 366 | } 367 | 368 | # Main functions 369 | get_config 370 | detect_os 371 | check_info 372 | if [ "${SYNC_FILE}" == "No" ] 373 | then 374 | run_upload 375 | remove_old_dir 376 | elif [ "${SYNC_FILE}" == "Yes" ] 377 | then 378 | run_sync 379 | else 380 | show_write_log "`change_color yellow [CHECKS][FAIL]` Option SYNC_FILE=${SYNC_FILE} not support. Only Yes or No. Exit" 381 | send_error_email "butgg [CHECKS][FAIL]" "Option SYNC_FILE=${SYNC_FILE} not support" 382 | exit 1 383 | fi 384 | -------------------------------------------------------------------------------- /butgg.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Setup variables 4 | GITHUB_LINK="https://raw.githubusercontent.com/mbrother2/backuptogoogle/master" 5 | GO_FILE="go1.12.5.linux-amd64" 6 | BUTGG_CONF="${HOME}/.gdrive/butgg.conf" 7 | DF_BACKUP_DIR="${HOME}/backup" 8 | DF_TAR_BEFORE_UPLOAD="No" 9 | DF_SYNC_FILE="No" 10 | DF_LOG_FILE="${HOME}/.gdrive/butgg.log" 11 | DF_DAY_REMOVE="7" 12 | DF_GDRIVE_ID="None" 13 | DF_EMAIL_USER="None" 14 | DF_EMAIL_PASS="None" 15 | DF_EMAIL_TO="None" 16 | GDRIVE_BIN="${HOME}/bin/gdrive" 17 | GDRIVE_TOKEN="${HOME}/.gdrive/token_v2.json" 18 | CRON_BACKUP="${HOME}/bin/cron_backup.bash" 19 | SETUP_FILE="${HOME}/bin/butgg.bash" 20 | CRON_TEMP="${HOME}/.gdrive/old_cron" 21 | FIRST_OPTION=$1 22 | SECOND_OPTION=$2 23 | 24 | # Date variables 25 | TODAY=`date +"%d_%m_%Y"` 26 | 27 | # Color variables 28 | GREEN='\e[32m' 29 | RED='\e[31m' 30 | YELLOW='\e[33m' 31 | REMOVE='\e[0m' 32 | 33 | # Change color of words 34 | change_color(){ 35 | case $1 in 36 | green) echo -e "${GREEN}$2${REMOVE}";; 37 | red) echo -e "${RED}$2${REMOVE}";; 38 | yellow) echo -e "${YELLOW}$2${REMOVE}";; 39 | *) echo "$2";; 40 | esac 41 | } 42 | 43 | # Check MD5 of downloaded file 44 | check_md5sum(){ 45 | curl -o $2 ${GITHUB_LINK}/$1 46 | ORIGIN_MD5=`curl -s ${GITHUB_LINK}/MD5SUM | grep $1 | awk '{print $1}'` 47 | LOCAL_MD5=`md5sum $2 | awk '{print $1}'` 48 | if [ "${ORIGIN_MD5}" == "${LOCAL_MD5}" ] 49 | then 50 | show_write_log "Check md5sum for file $1 successful" 51 | else 52 | show_write_log "`change_color red [CHECKS][FAIL]` Can not verify md5 for file $1. Exit!" 53 | exit 1 54 | fi 55 | } 56 | 57 | # Check log file 58 | check_log_file(){ 59 | if [ ! -f ${BUTGG_CONF} ] 60 | then 61 | LOG_FILE=${DF_LOG_FILE} 62 | else 63 | LOG_FILE=`cat ${BUTGG_CONF} | grep "^LOG_FILE" | cut -d"=" -f2 | sed 's/"//g' | sed "s/'//g"` 64 | if [ "${LOG_FILE}" == "" ] 65 | then 66 | LOG_FILE=${DF_LOG_FILE} 67 | fi 68 | fi 69 | create_dir .gdrive 70 | create_dir bin 71 | } 72 | 73 | # Write log 74 | show_write_log(){ 75 | echo "`date "+[ %d/%m/%Y %H:%M:%S ]"` $1" | tee -a ${LOG_FILE} 76 | } 77 | 78 | # Create necessary directory 79 | create_dir(){ 80 | if [ ! -d ${HOME}/$1 ] 81 | then 82 | mkdir -p ${HOME}/$1 83 | if [ ! -d ${HOME}/$1 ] 84 | then 85 | echo "Can not create directory ${HOME}/$1. Exit" 86 | exit 1 87 | else 88 | if [ "$1" == ".gdrive" ] 89 | then 90 | show_write_log "---" 91 | show_write_log "Creating necessary directory..." 92 | fi 93 | show_write_log "Create directory ${HOME}/$1 successful" 94 | fi 95 | else 96 | if [ "$1" == ".gdrive" ] 97 | then 98 | show_write_log "---" 99 | show_write_log "Creating necessary directory..." 100 | fi 101 | show_write_log "Directory ${HOME}/$1 existed. Skip" 102 | fi 103 | echo 1 >> ${HOME}/$1/test.txt 104 | if [ $? -ne 0 ] 105 | then 106 | echo "Can not write to ${HOME}/$1. Exit" 107 | exit 1 108 | else 109 | show_write_log "Check write to ${HOME}/$1 successful" 110 | fi 111 | rm -f ${HOME}/$1/test.txt 112 | } 113 | 114 | check_package(){ 115 | which $1 116 | if [ $? -ne 0 ] 117 | then 118 | show_write_log "Command $1 not found. Trying to install $1..." 119 | sleep 3 120 | ${INSTALL_CM} install -y $1 121 | which $1 122 | if [ $? -ne 0 ] 123 | then 124 | show_write_log "Can not install $1 package. Please install $1 manually." 125 | exit 1 126 | fi 127 | fi 128 | show_write_log "Package $1 is installed" 129 | } 130 | 131 | # Write config 132 | write_config(){ 133 | if [ "$3" == "" ] 134 | then 135 | VAR=$1 136 | eval "$VAR"="$2" 137 | if [ -f ${BUTGG_CONF} ] 138 | then 139 | sed -i "/^$1/d" ${BUTGG_CONF} 140 | fi 141 | echo "$1=$2" >> ${BUTGG_CONF} 142 | else 143 | VAR=$1 144 | eval "$VAR"="$3" 145 | if [ -f ${BUTGG_CONF} ] 146 | then 147 | sed -i "/^$1/d" ${BUTGG_CONF} 148 | fi 149 | echo "$1=$3" >> ${BUTGG_CONF} 150 | fi 151 | } 152 | 153 | # Detect OS 154 | detect_os(){ 155 | show_write_log "Checking OS..." 156 | if [ -f /etc/os-release ] 157 | then 158 | OS=`cat /etc/os-release | grep "^NAME=" | cut -d'"' -f2 | awk '{print $1}'` 159 | if [[ "${OS}" == "CentOS" ]] || [[ "${OS}" == "CloudLinux" ]] 160 | then 161 | INSTALL_CM="yum" 162 | elif [[ "${OS}" == "Ubuntu" ]] || [[ "${OS}" == "Debian" ]] 163 | then 164 | INSTALL_CM="apt" 165 | elif [[ "${OS}" == "openSUSE" ]] || [[ "${OS}" == "SLES" ]] 166 | then 167 | INSTALL_CM="zypper" 168 | else 169 | show_write_log "Sorry! We do not support your OS. Exit" 170 | exit 1 171 | fi 172 | else 173 | show_write_log "Sorry! We do not support your OS. Exit" 174 | exit 1 175 | fi 176 | show_write_log "OS supported" 177 | show_write_log "Checking necessary package..." 178 | check_package curl 179 | check_package realpath 180 | if [ "${FIRST_OPTION}" != "--update" ] 181 | then 182 | check_package git 183 | fi 184 | } 185 | 186 | # Check network 187 | check_network(){ 188 | show_write_log "Cheking network..." 189 | curl -sI raw.githubusercontent.com >/dev/null 190 | if [ $? -eq 0 ] 191 | then 192 | show_write_log "Connect Github successful" 193 | else 194 | show_write_log "`change_color red [CHECKS][FAIL]` Can not connect to Github file, please check your network. Exit" 195 | exit 1 196 | fi 197 | if [ "${FIRST_OPTION}" != "--update" ] 198 | then 199 | curl -sI dl.google.com >/dev/null 200 | if [ $? -eq 0 ] 201 | then 202 | show_write_log "Connect Google successful" 203 | else 204 | show_write_log "`change_color red [CHECKS][FAIL]` Can not connect to Google, please check your network. Exit" 205 | exit 1 206 | fi 207 | fi 208 | } 209 | 210 | # Download file from Github 211 | download_file(){ 212 | show_write_log "Downloading script cron file from github..." 213 | check_md5sum cron_backup.bash "${CRON_BACKUP}" 214 | show_write_log "Downloading setup file from github..." 215 | check_md5sum butgg.bash "${SETUP_FILE}" 216 | chmod 755 ${CRON_BACKUP} ${SETUP_FILE} 217 | } 218 | 219 | # Build GDRIVE_BIN 220 | build_gdrive(){ 221 | cd $HOME/bin 222 | show_write_log "Downloading go from Google..." 223 | curl -o ${GO_FILE}.tar.gz https://dl.google.com/go/${GO_FILE}.tar.gz 224 | show_write_log "Extracting go lang..." 225 | tar -xf ${GO_FILE}.tar.gz 226 | show_write_log "Cloning gdrive project from Github..." 227 | rm -rf gdrive 228 | git clone https://github.com/gdrive-org/gdrive.git 229 | show_write_log "Build your own gdrive!" 230 | echo "" 231 | echo "Read more: https://github.com/mbrother2/backuptogoogle/wiki/Create-own-Google-credential-step-by-step" 232 | read -p " Your Google API client_id: " gg_client_id 233 | read -p " Your Google API client_secret: " gg_client_secret 234 | sed -i "s#^const ClientId =.*#const ClientId = \"${gg_client_id}\"#g" $HOME/bin/gdrive/handlers_drive.go 235 | sed -i "s#^const ClientSecret =.*#const ClientSecret = \"${gg_client_secret}\"#g" $HOME/bin/gdrive/handlers_drive.go 236 | echo "" 237 | show_write_log "Building gdrive..." 238 | cd $HOME/bin/gdrive 239 | $HOME/bin/go/bin/go get github.com/prasmussen/gdrive 240 | $HOME/bin/go/bin/go build -ldflags '-w -s' 241 | if [ $? -ne 0 ] 242 | then 243 | show_write_log "`change_color red [ERROR]` Can not build gdrive. Exit" 244 | exit 1 245 | else 246 | show_write_log "Build gdrive successful. Gdrive bin locate here ${GDRIVE_BIN} " 247 | fi 248 | mv $HOME/bin/gdrive/gdrive $HOME/bin/gdrive.bin 249 | chmod 755 $HOME/bin/gdrive.bin 250 | rm -f $HOME/bin/${GO_FILE}.tar.gz 251 | rm -rf $HOME/bin/go 252 | rm -rf $HOME/bin/gdrive 253 | mv $HOME/bin/gdrive.bin $HOME/bin/gdrive 254 | } 255 | 256 | # Setup gdrive credential 257 | setup_credential(){ 258 | show_write_log "Setting up gdrive credential..." 259 | if [ "${SECOND_OPTION}" == "credential" ] 260 | then 261 | if [ -f ${GDRIVE_TOKEN} ] 262 | then 263 | rm -f ${GDRIVE_TOKEN} 264 | fi 265 | fi 266 | ${GDRIVE_BIN} about 267 | if [ $? -ne 0 ] 268 | then 269 | show_write_log "`change_color yellow [WARNING]` Can not create gdrive credential. Please run \"${GDRIVE_BIN} about\" to create it after" 270 | sleep 3 271 | else 272 | show_write_log "Setup gdrive credential successful" 273 | fi 274 | } 275 | 276 | # Set up config file 277 | setup_config(){ 278 | show_write_log "Setting up config file..." 279 | echo "" 280 | read -p " Which directory on your server do you want to upload to Google Drive?(default ${DF_BACKUP_DIR}): " BACKUP_DIR 281 | read -p " How many days do you want to keep backup on Google Drive?(default ${DF_DAY_REMOVE}): " DAY_REMOVE 282 | echo "" 283 | echo "Read more https://github.com/mbrother2/backuptogoogle/wiki/What-is-the-option-SYNC_FILE%3F" 284 | read -p " Do you want only sync file(default no)(y/n): " SYNC_FILE 285 | echo "" 286 | echo "Read more https://github.com/mbrother2/backuptogoogle/wiki/Get-Google-folder-ID" 287 | if [ "${SYNC_FILE}" == "y" ] 288 | then 289 | echo "Because you choose sync file method, so you must enter exactly Google folder ID here!" 290 | fi 291 | read -p " Your Google folder ID(default ${DF_GDRIVE_ID}): " GDRIVE_ID 292 | if [ "${SYNC_FILE}" == "y" ] 293 | then 294 | TAR_BEFORE_UPLOAD=${DF_TAR_BEFORE_UPLOAD} 295 | else 296 | read -p " Do you want compress directory before upload?(default no)(y/n): " TAR_BEFORE_UPLOAD 297 | fi 298 | echo "" 299 | echo "Read more https://github.com/mbrother2/backuptogoogle/wiki/Turn-on-2-Step-Verification-&-create-app's-password-for-Google-email" 300 | read -p " Do you want to send email if upload error(default no)(y/n): " SEND_EMAIL 301 | if [ "${SEND_EMAIL}" == "y" ] 302 | then 303 | read -p " Your Google email user name: " EMAIL_USER 304 | read -p " Your Google email password: " EMAIL_PASS 305 | read -p " Which email will be receive notify?: " EMAIL_TO 306 | fi 307 | if [ "${SYNC_FILE}" == "y" ] 308 | then 309 | SYNC_FILE="Yes" 310 | else 311 | SYNC_FILE="No" 312 | fi 313 | if [ "${TAR_BEFORE_UPLOAD}" == "y" ] 314 | then 315 | TAR_BEFORE_UPLOAD="Yes" 316 | else 317 | TAR_BEFORE_UPLOAD=${DF_TAR_BEFORE_UPLOAD} 318 | fi 319 | echo "LOG_FILE=${LOG_FILE}" > ${BUTGG_CONF} 320 | write_config BACKUP_DIR "${DF_BACKUP_DIR}" "${BACKUP_DIR}" 321 | write_config DAY_REMOVE "${DF_DAY_REMOVE}" "${DAY_REMOVE}" 322 | write_config GDRIVE_ID "${DF_GDRIVE_ID}" "${GDRIVE_ID}" 323 | write_config EMAIL_USER "${DF_EMAIL_USER}" "${EMAIL_USER}" 324 | write_config EMAIL_PASS "${DF_EMAIL_PASS}" "${EMAIL_PASS}" 325 | write_config EMAIL_TO "${DF_EMAIL_TO}" "${EMAIL_TO}" 326 | write_config SYNC_FILE "${DF_SYNC_FILE}" "${SYNC_FILE}" 327 | write_config TAR_BEFORE_UPLOAD "${DF_TAR_BEFORE_UPLOAD}" "${TAR_BEFORE_UPLOAD}" 328 | if [ $? -ne 0 ] 329 | then 330 | show_write_log "`change_color red [ERROR]` Can not write config to file ${BUTGG_CONF}. Please check permission of this file. Exit" 331 | exit 1 332 | else 333 | if [ ! -d ${BACKUP_DIR} ] 334 | then 335 | show_write_log "`change_color yellow [WARNING]` Directory ${BACKUP_DIR} does not exist! Ensure you will be create it after." 336 | sleep 3 337 | fi 338 | show_write_log "Setup config file successful" 339 | fi 340 | } 341 | 342 | # Set up cron backup 343 | setup_cron(){ 344 | echo "" 345 | show_write_log "Setting up cron backup..." 346 | CHECK_BIN=`echo $PATH | grep -c "${HOME}/bin"` 347 | if [ ${CHECK_BIN} -eq 0 ] 348 | then 349 | echo "PATH=$PATH:$HOME/bin" >> ${HOME}/.profile 350 | echo "export PATH" >> ${HOME}/.profile 351 | source ${HOME}/.profile 352 | fi 353 | crontab -l > ${CRON_TEMP} 354 | CHECK_CRON=`cat ${CRON_TEMP} | grep -c "cron_backup.bash"` 355 | if [ ${CHECK_CRON} -eq 0 ] 356 | then 357 | echo "PATH=$PATH" >> ${CRON_TEMP} 358 | echo "0 0 * * * bash ${CRON_BACKUP} >/dev/null 2>&1" >> ${CRON_TEMP} 359 | crontab ${CRON_TEMP} 360 | if [ $? -ne 0 ] 361 | then 362 | show_write_log "Can not setup cronjob to backup! Please check again" 363 | SHOW_CRON="`change_color yellow [WARNING]` Can not setup cronjob to backup" 364 | else 365 | show_write_log "Setup cronjob to backup successful" 366 | SHOW_CRON="0 0 * * * bash ${CRON_BACKUP} >/dev/null 2>&1" 367 | fi 368 | else 369 | show_write_log "Cron backup existed. Skip" 370 | SHOW_CRON=`cat ${CRON_TEMP} | grep "cron_backup.bash"` 371 | fi 372 | rm -f ${CRON_TEMP} 373 | } 374 | 375 | # Show information 376 | show_info(){ 377 | echo "" 378 | if [ "${SECOND_OPTION}" == config ] 379 | then 380 | show_write_log "+-----" 381 | show_write_log "| SUCESSFUL! Your information:" 382 | show_write_log "| Backup dir : ${BACKUP_DIR}" 383 | show_write_log "| Keep backup : ${DAY_REMOVE} days" 384 | show_write_log "| Google folder ID: ${GDRIVE_ID}" 385 | show_write_log "| Your email : ${EMAIL_USER}" 386 | show_write_log "| Email password : ${EMAIL_PASS}" 387 | show_write_log "| Email notify : ${EMAIL_TO}" 388 | show_write_log "| Config file : ${BUTGG_CONF}" 389 | show_write_log "+-----" 390 | else 391 | show_write_log "+-----" 392 | show_write_log "| SUCESSFUL! Your information:" 393 | show_write_log "| Backup dir : ${BACKUP_DIR}" 394 | show_write_log "| Config file : ${BUTGG_CONF}" 395 | show_write_log "| Log file : ${LOG_FILE}" 396 | show_write_log "| Keep backup : ${DAY_REMOVE} days" 397 | show_write_log "| Google folder ID: ${GDRIVE_ID}" 398 | show_write_log "| Your email : ${EMAIL_USER}" 399 | show_write_log "| Email password : ${EMAIL_PASS}" 400 | show_write_log "| Email notify : ${EMAIL_TO}" 401 | show_write_log "| butgg.bash file : ${SETUP_FILE}" 402 | show_write_log "| Cron backup file: ${CRON_BACKUP}" 403 | show_write_log "| Gdrive bin file : ${GDRIVE_BIN}" 404 | show_write_log "| Cron backup : ${SHOW_CRON}" 405 | show_write_log "| Google token : ${GDRIVE_TOKEN}" 406 | show_write_log "+-----" 407 | 408 | echo "" 409 | if [[ "${OS}" == "Ubuntu" ]] || [[ "${OS}" == "Debian" ]] 410 | then 411 | echo "IMPORTANT: Please run command to use butgg: source ${HOME}/.profile " 412 | fi 413 | echo "If you get trouble when use butgg.bash please report here:" 414 | echo "https://github.com/mbrother2/backuptogoogle/issues" 415 | fi 416 | } 417 | 418 | _setup(){ 419 | check_log_file 420 | if [ -z "${SECOND_OPTION}" ] 421 | then 422 | detect_os 423 | check_network 424 | download_file 425 | build_gdrive 426 | setup_credential 427 | setup_config 428 | setup_cron 429 | show_info 430 | else 431 | case ${SECOND_OPTION} in 432 | config) 433 | setup_config 434 | show_info 435 | ;; 436 | credential) 437 | setup_credential 438 | ;; 439 | only-build) 440 | detect_os 441 | check_network 442 | build_gdrive 443 | ;; 444 | no-build) 445 | detect_os 446 | check_network 447 | download_file 448 | setup_credential 449 | setup_config 450 | setup_cron 451 | show_info 452 | ;; 453 | no-update) 454 | detect_os 455 | check_network 456 | build_gdrive 457 | setup_credential 458 | setup_config 459 | setup_cron 460 | show_info 461 | ;; 462 | *) 463 | show_write_log "No such command: ${SECOND_OPTION}" 464 | _help 465 | ;; 466 | esac 467 | fi 468 | } 469 | 470 | _update(){ 471 | check_log_file 472 | detect_os 473 | check_network 474 | download_file 475 | } 476 | 477 | _uninstall(){ 478 | check_log_file 479 | show_write_log "Removing all butgg.bash scripts..." 480 | rm -f ${GDRIVE_BIN} ${CRON_BACKUP} ${SETUP_FILE} 481 | if [ $? -ne 0 ] 482 | then 483 | show_write_log "Can not remove all butgg.bash scripts. Please check permission of these files" 484 | else 485 | show_write_log "Remove all butgg.bash scripts successful" 486 | fi 487 | read -p " Do you want remove ${HOME}/.gdrive directory?(y/n) " REMOVE_GDRIVE_DIR 488 | if [[ "${REMOVE_GDRIVE_DIR}" == "y" ]] || [[ "${REMOVE_GDRIVE_DIR}" == "Y" ]] 489 | then 490 | rm -rf ${HOME}/.gdrive 491 | if [ $? -ne 0 ] 492 | then 493 | show_write_log "Can not remove directory ${HOME}/.gdrive. Please check permission of this directory" 494 | else 495 | echo "Remove directory ${HOME}/.gdrive successful" 496 | fi 497 | else 498 | show_write_log "Skip remove ${HOME}/.gdrive directory" 499 | fi 500 | } 501 | 502 | _help(){ 503 | echo "butgg.bash - Backup to Google Drive solution" 504 | echo "" 505 | echo "Usage: butgg.bash [options] [command]" 506 | echo "" 507 | echo "Options:" 508 | echo " --help show this help message and exit" 509 | echo " --setup setup or reset all scripts & config file" 510 | echo " config only setup config" 511 | echo " credential only setup credential" 512 | echo " only-build only build gdrive bin" 513 | echo " no-build setup butgg without build gdrive" 514 | echo " no-update setup butgg without update script" 515 | echo " --update update to latest version" 516 | echo " --uninstall remove all butgg scripts and .gdrive directory" 517 | } 518 | 519 | # Main functions 520 | case ${FIRST_OPTION} in 521 | --help) _help ;; 522 | --setup) _setup ;; 523 | --update) _update ;; 524 | --uninstall) _uninstall ;; 525 | *) echo "No such option: ${FIRST_OPTION}"; _help ;; 526 | esac 527 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # backuptogoogle (Linux: butgg.bash - BSD: butgg.sh) 2 | 3 | ### Tired of gdrive? Gdrive project dead? I wrote some scripts to use [rclone](https://github.com/rclone/rclone) for backuping from your server to Google Drive. Please try here: 4 | ### https://github.com/mbrother2/butdr 5 | 6 | # What can this script do? 7 | - Complie gdrive (https://github.com/gdrive-org/gdrive) on your server with your Google credential 8 | - Create cron auto backup 9 | - Exclude file/directory when run cron backup 10 | - Sync backup directory from local to Google Drive 11 | - Compress backup directory before upload 12 | - Send error email if upload to Google Drive fail 13 | - Auto remove old backup on Google Drive 14 | - Run upload from your backup directory to Google Drive whenever you want 15 | - Detail log 16 | # Structure 17 | ``` 18 | $HOME (/root or /home/$USER) 19 | ├── bin 20 | │   ├── butgg.bash (or butgg.sh on BSD system) 21 | │ ├── cron_backup.bash (or cron_backup.sh on BSD system) 22 | │   └── gdrive 23 | └── .gdrive 24 | ├── butgg.conf 25 | ├── butgg.log 26 | ├── exclude.list (exist if you create) 27 | ├── detail.log (debug log if run fail) 28 | └── token_v2.json 29 | ``` 30 | # OS support(x86_64): 31 | - **Linux:** CentOS, Debian, Ubuntu, openSUSE 32 | - **BSD:** FreeBSD 33 | - **Commercial:** CloudLinux, SUSE Linux Enterprise 34 | - **Windows:** Build gdrive only 35 | # Environment 36 | - Server, VPS, shared hosting 37 | 38 | --- 39 | # [BETA] Support docker & docker-compose 40 | For more informations: https://hub.docker.com/r/mbrother2/backuptogoogle 41 | 42 | --- 43 | # How to use 44 | **On Linux system:** 45 | ``` 46 | curl -o butgg.bash https://raw.githubusercontent.com/mbrother2/backuptogoogle/master/butgg.bash 47 | bash butgg.bash --setup 48 | ``` 49 | **On BSD system:** 50 | ``` 51 | curl -o butgg.sh https://raw.githubusercontent.com/mbrother2/backuptogoogle/master/butgg.sh 52 | sh butgg.sh --setup 53 | ``` 54 | **On Windows system:** 55 | 1. Install git for windows https://git-scm.com/download/win 56 | 2. Build gdrive on Git bash terminal: 57 | ``` 58 | curl -o build_gdrive_windows.bash https://raw.githubusercontent.com/mbrother2/backuptogoogle/master/build_gdrive_windows.bash 59 | bash build_gdrive_windows.bash 60 | ``` 61 | 62 | # Wiki 63 | ##### [Create own Google credential step by step](https://github.com/mbrother2/backuptogoogle/wiki/Create-own-Google-credential-step-by-step) 64 | ##### [Get Google folder ID](https://github.com/mbrother2/backuptogoogle/wiki/Get-Google-folder-ID) 65 | ##### [Turn on 2 Step Verification & create app's password for Google email](https://github.com/mbrother2/backuptogoogle/wiki/Turn-on-2-Step-Verification-&-create-app's-password-for-Google-email) 66 | ##### [What is the option SYNC_FILE?](https://github.com/mbrother2/backuptogoogle/wiki/What-is-the-option-SYNC_FILE%3F) 67 | 68 | # Change log 69 | https://github.com/mbrother2/backuptogoogle/blob/master/CHANGLOG.md 70 | # Options 71 | Run command `bash butgg.bash --help`(or `sh butgg.sh --help` on BSD system) to show all options( After install you only need run `butgg.bash --help`(or `butgg.sh --help` on BSD system)) 72 | ``` 73 | butgg.bash - Backup to Google Drive solution 74 | 75 | Usage: butgg.bash [options] [command] 76 | 77 | Options: 78 | --help show this help message and exit 79 | --setup setup or reset all scripts & config file 80 | config only setup config 81 | credential only setup credential 82 | only-build only build gdrive bin 83 | no-build setup butgg without build gdrive 84 | no-update setup butgg without update script 85 | --update update to latest version 86 | --uninstall remove all butgg scripts and .gdrive directory 87 | ``` 88 | # Command 89 | ###### 1. Help 90 | `butgg.bash --help` (or `butgg.sh --help` on BSD system) 91 | Show help message and exit 92 | ##### Example 93 | ``` 94 | [thanh1@centos7 .gdrive]$ butgg.bash --help 95 | butgg.bash - Backup to Google Drive solution 96 | 97 | Usage: butgg.bash [options] [command] 98 | 99 | Options: 100 | --help show this help message and exit 101 | --setup setup or reset all scripts & config file 102 | config only setup config 103 | credential only setup credential 104 | only-build only build gdrive bin 105 | no-build setup butgg without build gdrive 106 | no-update setup butgg without update script 107 | --update update to latest version 108 | --uninstall remove all butgg scripts and .gdrive directory 109 | ``` 110 | ###### 2. Setup 111 | `butgg.bash --setup` (or `butgg.sh --setup` on BSD system) 112 | Set up or reset all scripts & config file 113 | ##### Example 114 | ``` 115 | [thanh1@centos7 ~]$ butgg.bash --setup 116 | [ 14/11/2019 10:54:25 ] Cheking network... 117 | [ 14/11/2019 10:54:25 ] Connect Github successful 118 | [ 14/11/2019 10:54:26 ] Connect Google successful 119 | [ 14/11/2019 10:54:26 ] Checking OS... 120 | [ 14/11/2019 10:54:26 ] OS supported 121 | [ 14/11/2019 10:54:26 ] Downloading script cron file from github... 122 | % Total % Received % Xferd Average Speed Time Time Time Current 123 | Dload Upload Total Spent Left Speed 124 | 100 5808 100 5808 0 0 7944 0 --:--:-- --:--:-- --:--:-- 7956 125 | [ 14/11/2019 10:54:27 ] Check md5sum for file cron_backup.bash successful 126 | [ 14/11/2019 10:54:27 ] Downloading setup file from github... 127 | % Total % Received % Xferd Average Speed Time Time Time Current 128 | Dload Upload Total Spent Left Speed 129 | 100 11151 100 11151 0 0 26427 0 --:--:-- --:--:-- --:--:-- 26424 130 | [ 14/11/2019 10:54:28 ] Check md5sum for file butgg.bash successful 131 | /usr/bin/git 132 | [ 14/11/2019 10:54:28 ] Downloading go from Google... 133 | % Total % Received % Xferd Average Speed Time Time Time Current 134 | Dload Upload Total Spent Left Speed 135 | 100 122M 100 122M 0 0 10.1M 0 0:00:12 0:00:12 --:--:-- 11.0M 136 | [ 14/11/2019 10:54:40 ] Extracting go lang... 137 | [ 14/11/2019 10:54:45 ] Cloning gdrive project from Github... 138 | Cloning into 'gdrive'... 139 | remote: Enumerating objects: 1458, done. 140 | remote: Total 1458 (delta 0), reused 0 (delta 0), pack-reused 1458 141 | Receiving objects: 100% (1458/1458), 465.06 KiB | 228.00 KiB/s, done. 142 | Resolving deltas: 100% (873/873), done. 143 | [ 14/11/2019 10:54:49 ] Build your own gdrive! 144 | Please go to URL to create your own Google credential: 145 | https://github.com/mbrother2/backuptogoogle/wiki/Create-own-Google-credential-step-by-step 146 | Your Google API client_id: 782896115405-qs2evi3rqlnkjm2vond8onilq9xxxxxx.apps.googleusercontent.com 147 | Your Google API client_secret: g7p_kcdNEq_ULsfxrTxxxxxx 148 | [ 14/11/2019 10:55:02 ] Building gdrive... 149 | [ 14/11/2019 10:55:03 ] Build gdrive successful 150 | [ 14/11/2019 10:55:04 ] Setting up gdrive credential... 151 | Authentication needed 152 | Go to the following url in your browser: 153 | https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=782896115405-qs2evi3rqlnkjm2vond8onilq9xxxxxx.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&state=state 154 | 155 | Enter verification code: 4/tQEqtaOpkPsX1keXGuZWQsPMB5AF0mZ7a_FiiSheYnYuSBejxxxxxx 156 | User: mbr other, backupxxxxxx@gmail.com 157 | Used: 158 | Free: 16.1 GB 159 | Total: 16.1 GB 160 | Max upload size: 5.2 TB 161 | [ 14/11/2019 10:55:29 ] Setup gdrive credential successful 162 | [ 14/11/2019 10:55:29 ] Setting up config file... 163 | Which directory do you want to upload to Google Drive?(default /home/thanh1/backup): /home/thanh1/backup2/backup 164 | How many days you want to keep backup on Google Drive?(default 7): 30 165 | [ 14/11/2019 10:55:47 ] [WARNING] Directory /home/thanh1/backup2/backup does not exist! Ensure you will be create it after. 166 | [ 14/11/2019 10:55:50 ] Setup config file successful 167 | [ 14/11/2019 10:55:50 ] Setting up cron backup... 168 | [ 14/11/2019 10:55:50 ] Cron backup existed. Skip 169 | 170 | [ 14/11/2019 10:55:50 ] +----- 171 | [ 14/11/2019 10:55:50 ] | SUCESSFUL! Your information: 172 | [ 14/11/2019 10:55:50 ] | Backup dir : /home/thanh1/backup2/backup 173 | [ 14/11/2019 10:55:50 ] | Config file : /home/thanh1/.gdrive/butgg.conf 174 | [ 14/11/2019 10:55:50 ] | Log file : /home/thanh1/.gdrive/butgg.log 175 | [ 14/11/2019 10:55:50 ] | Keep backup : 30 days 176 | [ 14/11/2019 10:55:50 ] | butgg.sh file : /home/thanh1/bin/butgg.bash 177 | [ 14/11/2019 10:55:50 ] | Cron backup file: /home/thanh1/bin/cron_backup.bash 178 | [ 14/11/2019 10:55:50 ] | Gdrive bin file : /home/thanh1/bin/gdrive 179 | [ 14/11/2019 10:55:50 ] | Cron backup : 0 0 * * * bash /home/thanh1/bin/cron_backup.bash >/dev/null 2>&1 180 | [ 14/11/2019 10:55:50 ] | Google token : /home/thanh1/.gdrive/token_v2.json 181 | [ 14/11/2019 10:55:50 ] +----- 182 | 183 | If you get trouble when use butgg.bash please report here: 184 | https://github.com/mbrother2/backuptogoogle/issues 185 | ``` 186 | --- 187 | `butgg.bash --setup config` (or `butgg.sh --setup config` on BSD system) 188 | Only edit butgg.conf 189 | ##### Example 190 | ``` 191 | [thanh1@centos7 .gdrive]$ butgg.bash --setup config 192 | [ 15/11/2019 08:41:54 ] --- 193 | [ 15/11/2019 08:41:54 ] Setting up config file... 194 | Which directory do you want to upload to Google Drive?(default /home/thanh1/backup): /home/thanh1/backup2/backup 195 | How many days you want to keep backup on Google Drive?(default 7): 30 196 | [ 15/11/2019 08:42:06 ] Setup config file successful 197 | 198 | [ 15/11/2019 08:42:06 ] +----- 199 | [ 15/11/2019 08:42:06 ] | SUCESSFUL! Your information: 200 | [ 15/11/2019 08:42:06 ] | Backup dir : /home/thanh1/backup2/backup 201 | [ 15/11/2019 08:42:06 ] | Keep backup : 30 days 202 | [ 15/11/2019 08:42:06 ] | Config file : /home/thanh1/.gdrive/butgg.conf 203 | [ 15/11/2019 08:42:06 ] +----- 204 | ``` 205 | --- 206 | `butgg.bash --setup credential` (or `butgg.sh --setup credential` on BSD system) 207 | Only reset Google Drive token 208 | ##### Example 209 | ``` 210 | [thanh1@centos7 .gdrive]$ butgg.bash --setup credential 211 | [ 15/11/2019 08:46:41 ] --- 212 | [ 15/11/2019 08:46:41 ] Setting up gdrive credential... 213 | Authentication needed 214 | Go to the following url in your browser: 215 | https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=782896115405-qs2evi3rqlnkjm2vond8onilq9xxxxxx.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&state=state 216 | 217 | Enter verification code: 4/tQGSi4-XXBv3QMy8wAacJz-BARzzZM0wVZG0xtATTP3vG393axxxxxx 218 | User: mbr other, backupxxxxxx@gmail.com 219 | Used: 220 | Free: 16.1 GB 221 | Total: 16.1 GB 222 | Max upload size: 5.2 TB 223 | [ 15/11/2019 08:47:01 ] Setup gdrive credential successful 224 | ``` 225 | --- 226 | `butgg.bash --setup only-build` (or `butgg.sh --setup only-build` on BSD system) 227 | Only build gdrive bin 228 | ##### Example 229 | ``` 230 | [thanh1@centos7 .gdrive]$ butgg.bash --setup only-build 231 | [ 15/11/2019 08:42:54 ] --- 232 | [ 15/11/2019 08:42:54 ] Cheking network... 233 | [ 15/11/2019 08:42:54 ] Connect Github successful 234 | [ 15/11/2019 08:42:54 ] Connect Google successful 235 | [ 15/11/2019 08:42:54 ] Checking OS... 236 | [ 15/11/2019 08:42:54 ] OS supported 237 | /usr/bin/git 238 | [ 15/11/2019 08:42:54 ] Downloading go from Google... 239 | % Total % Received % Xferd Average Speed Time Time Time Current 240 | Dload Upload Total Spent Left Speed 241 | 100 122M 100 122M 0 0 1209k 0 0:01:43 0:01:43 --:--:-- 951k 242 | [ 15/11/2019 08:44:37 ] Extracting go lang... 243 | [ 15/11/2019 08:44:44 ] Cloning gdrive project from Github... 244 | Cloning into 'gdrive'... 245 | remote: Enumerating objects: 1458, done. 246 | remote: Total 1458 (delta 0), reused 0 (delta 0), pack-reused 1458 247 | Receiving objects: 100% (1458/1458), 465.06 KiB | 22.00 KiB/s, done. 248 | Resolving deltas: 100% (873/873), done. 249 | [ 15/11/2019 08:45:20 ] Build your own gdrive! 250 | Please go to URL to create your own Google credential: 251 | https://github.com/mbrother2/backuptogoogle/wiki/Create-own-Google-credential-step-by-step 252 | Your Google API client_id: 782896115405-qs2evi3rqlnkjm2vond8onilq9xxxxxx.apps.googleusercontent.com 253 | Your Google API client_secret: g7p_kcdNEq_ULsfxrTxxxxxx 254 | [ 15/11/2019 08:45:34 ] Building gdrive... 255 | [ 15/11/2019 08:45:36 ] Build gdrive successful. Gdrive bin locate here /home/thanh1/bin/gdrive 256 | ``` 257 | --- 258 | `butgg.sh --setup no-build` (or `butgg.sh --setup no-build` on BSD system) 259 | Setup butgg without build gdrive 260 | ##### Example 261 | ``` 262 | [thanh1@centos7 .gdrive]$ butgg.bash --setup no-build 263 | [ 15/11/2019 08:53:15 ] --- 264 | [ 15/11/2019 08:53:15 ] Cheking network... 265 | [ 15/11/2019 08:53:15 ] Connect Github successful 266 | [ 15/11/2019 08:53:15 ] Connect Google successful 267 | [ 15/11/2019 08:53:15 ] Checking OS... 268 | [ 15/11/2019 08:53:15 ] OS supported 269 | [ 15/11/2019 08:53:15 ] Downloading script cron file from github... 270 | % Total % Received % Xferd Average Speed Time Time Time Current 271 | Dload Upload Total Spent Left Speed 272 | 100 6486 100 6486 0 0 15030 0 --:--:-- --:--:-- --:--:-- 15048 273 | [ 15/11/2019 08:53:16 ] Check md5sum for file cron_backup.bash successful 274 | [ 15/11/2019 08:53:16 ] Downloading setup file from github... 275 | % Total % Received % Xferd Average Speed Time Time Time Current 276 | Dload Upload Total Spent Left Speed 277 | 100 13199 100 13199 0 0 34979 0 --:--:-- --:--:-- --:--:-- 35010 278 | [ 15/11/2019 08:53:17 ] Check md5sum for file butgg.bash successful 279 | [ 15/11/2019 08:53:17 ] Setting up gdrive credential... 280 | User: mbr other, backupxxxxxx@gmail.com 281 | Used: 282 | Free: 16.1 GB 283 | Total: 16.1 GB 284 | Max upload size: 5.2 TB 285 | [ 15/11/2019 08:53:17 ] Setup gdrive credential successful 286 | [ 15/11/2019 08:53:17 ] Setting up config file... 287 | Which directory do you want to upload to Google Drive?(default /home/thanh1/backup): 288 | How many days you want to keep backup on Google Drive?(default 7): 289 | [ 15/11/2019 08:53:25 ] Setup config file successful 290 | [ 15/11/2019 08:53:25 ] Setting up cron backup... 291 | [ 15/11/2019 08:53:25 ] Cron backup existed. Skip 292 | 293 | [ 15/11/2019 08:53:25 ] +----- 294 | [ 15/11/2019 08:53:25 ] | SUCESSFUL! Your information: 295 | [ 15/11/2019 08:53:25 ] | Backup dir : /home/thanh1/backup 296 | [ 15/11/2019 08:53:25 ] | Config file : /home/thanh1/.gdrive/butgg.conf 297 | [ 15/11/2019 08:53:25 ] | Log file : /home/thanh1/.gdrive/butgg.log 298 | [ 15/11/2019 08:53:25 ] | Keep backup : 7 days 299 | [ 15/11/2019 08:53:25 ] | butgg.sh file : /home/thanh1/bin/butgg.bash 300 | [ 15/11/2019 08:53:25 ] | Cron backup file: /home/thanh1/bin/cron_backup.bash 301 | [ 15/11/2019 08:53:25 ] | Gdrive bin file : /home/thanh1/bin/gdrive 302 | [ 15/11/2019 08:53:25 ] | Cron backup : 0 0 * * * bash /home/thanh1/bin/cron_backup.bash >/dev/null 2>&1 303 | [ 15/11/2019 08:53:25 ] | Google token : /home/thanh1/.gdrive/token_v2.json 304 | [ 15/11/2019 08:53:25 ] +----- 305 | 306 | If you get trouble when use butgg.bash please report here: 307 | https://github.com/mbrother2/backuptogoogle/issues 308 | ``` 309 | --- 310 | `butgg.sh --setup no-update` (or `butgg.sh --setup no-update` on BSD system) 311 | Setup butgg without update script 312 | ##### Example 313 | ``` 314 | [thanh1@centos7 .gdrive]$ butgg.bash --setup no-update 315 | [ 15/11/2019 08:54:20 ] --- 316 | [ 15/11/2019 08:54:20 ] Cheking network... 317 | [ 15/11/2019 08:54:20 ] Connect Github successful 318 | [ 15/11/2019 08:54:20 ] Connect Google successful 319 | [ 15/11/2019 08:54:20 ] Checking OS... 320 | [ 15/11/2019 08:54:20 ] OS supported 321 | /usr/bin/git 322 | [ 15/11/2019 08:54:20 ] Downloading go from Google... 323 | % Total % Received % Xferd Average Speed Time Time Time Current 324 | Dload Upload Total Spent Left Speed 325 | 100 122M 100 122M 0 0 10.2M 0 0:00:11 0:00:11 --:--:-- 10.9M 326 | [ 15/11/2019 08:54:32 ] Extracting go lang... 327 | [ 15/11/2019 08:54:37 ] Cloning gdrive project from Github... 328 | Cloning into 'gdrive'... 329 | remote: Enumerating objects: 1458, done. 330 | remote: Total 1458 (delta 0), reused 0 (delta 0), pack-reused 1458 331 | Receiving objects: 100% (1458/1458), 465.06 KiB | 343.00 KiB/s, done. 332 | Resolving deltas: 100% (873/873), done. 333 | [ 15/11/2019 08:54:41 ] Build your own gdrive! 334 | Please go to URL to create your own Google credential: 335 | https://github.com/mbrother2/backuptogoogle/wiki/Create-own-Google-credential-step-by-step 336 | Your Google API client_id: 782896115405-qs2evi3rqlnkjm2vond8onilq9xxxxxx.apps.googleusercontent.com 337 | Your Google API client_secret: g7p_kcdNEq_ULsfxrTxxxxxx 338 | [ 15/11/2019 08:54:54 ] Building gdrive... 339 | [ 15/11/2019 08:54:56 ] Build gdrive successful. Gdrive bin locate here /home/thanh1/bin/gdrive 340 | [ 15/11/2019 08:54:56 ] Setting up gdrive credential... 341 | User: mbr other, backupxxxxxx@gmail.com 342 | Used: 343 | Free: 16.1 GB 344 | Total: 16.1 GB 345 | Max upload size: 5.2 TB 346 | [ 15/11/2019 08:54:57 ] Setup gdrive credential successful 347 | [ 15/11/2019 08:54:57 ] Setting up config file... 348 | Which directory do you want to upload to Google Drive?(default /home/thanh1/backup): 349 | How many days you want to keep backup on Google Drive?(default 7): 350 | [ 15/11/2019 08:55:00 ] Setup config file successful 351 | [ 15/11/2019 08:55:00 ] Setting up cron backup... 352 | [ 15/11/2019 08:55:00 ] Cron backup existed. Skip 353 | 354 | [ 15/11/2019 08:55:00 ] +----- 355 | [ 15/11/2019 08:55:00 ] | SUCESSFUL! Your information: 356 | [ 15/11/2019 08:55:00 ] | Backup dir : /home/thanh1/backup 357 | [ 15/11/2019 08:55:00 ] | Config file : /home/thanh1/.gdrive/butgg.conf 358 | [ 15/11/2019 08:55:00 ] | Log file : /home/thanh1/.gdrive/butgg.log 359 | [ 15/11/2019 08:55:00 ] | Keep backup : 7 days 360 | [ 15/11/2019 08:55:00 ] | butgg.sh file : /home/thanh1/bin/butgg.bash 361 | [ 15/11/2019 08:55:00 ] | Cron backup file: /home/thanh1/bin/cron_backup.bash 362 | [ 15/11/2019 08:55:00 ] | Gdrive bin file : /home/thanh1/bin/gdrive 363 | [ 15/11/2019 08:55:00 ] | Cron backup : 0 0 * * * bash /home/thanh1/bin/cron_backup.bash >/dev/null 2>&1 364 | [ 15/11/2019 08:55:00 ] | Google token : /home/thanh1/.gdrive/token_v2.json 365 | [ 15/11/2019 08:55:00 ] +----- 366 | 367 | If you get trouble when use butgg.bash please report here: 368 | https://github.com/mbrother2/backuptogoogle/issues 369 | ``` 370 | ###### 3. Update 371 | `butgg.sh --update` (or `butgg.sh --update` on BSD system) 372 | Update to latest version 373 | ##### Example 374 | ``` 375 | [thanh1@centos7 .gdrive]$ butgg.bash --update 376 | [ 15/11/2019 08:56:43 ] --- 377 | [ 15/11/2019 08:56:43 ] Cheking network... 378 | [ 15/11/2019 08:56:43 ] Connect Github successful 379 | [ 15/11/2019 08:56:43 ] Connect Google successful 380 | [ 15/11/2019 08:56:43 ] Checking OS... 381 | [ 15/11/2019 08:56:43 ] OS supported 382 | [ 15/11/2019 08:56:43 ] Downloading script cron file from github... 383 | % Total % Received % Xferd Average Speed Time Time Time Current 384 | Dload Upload Total Spent Left Speed 385 | 100 6486 100 6486 0 0 14965 0 --:--:-- --:--:-- --:--:-- 14979 386 | [ 15/11/2019 08:56:44 ] Check md5sum for file cron_backup.bash successful 387 | [ 15/11/2019 08:56:44 ] Downloading setup file from github... 388 | % Total % Received % Xferd Average Speed Time Time Time Current 389 | Dload Upload Total Spent Left Speed 390 | 100 13199 100 13199 0 0 35969 0 --:--:-- --:--:-- --:--:-- 35964 391 | [ 15/11/2019 08:56:45 ] Check md5sum for file butgg.bash successful 392 | ``` 393 | ###### 4. Uninstall 394 | `butgg.sh --uninstall` (or `butgg.sh --uninstall` on BSD system) 395 | Remove all butgg scripts and .gdrive directory 396 | ##### Example 397 | ``` 398 | [thanh1@centos7 .gdrive]$ butgg.bash --uninstall 399 | [ 15/11/2019 08:57:14 ] --- 400 | [ 15/11/2019 08:57:14 ] Removing all butgg.bash scripts... 401 | [ 15/11/2019 08:57:14 ] Remove all butgg.bash scripts successful 402 | Do you want remove /home/thanh1/.gdrive directory?(y/n) n 403 | [ 15/11/2019 08:57:18 ] Skip remove /home/thanh1/.gdrive directory 404 | ``` 405 | ###### 5. Run upload to Google Drive immediately 406 | `cron_backup.bash` (or `cron_backup.sh` on BSD system) 407 | Run upload to Google Drive immediately without show log 408 | `cron_backup.bash -v` (or `cron_backup.sh -v` on BSD system) 409 | Run upload to Google Drive immediately with show log detail 410 | ##### Example 411 | ``` 412 | [thanh1@centos7 ~]$ cron_backup.bash -v 413 | [ 14/11/2019 10:58:54 ] --- 414 | [ 14/11/2019 10:58:55 ] Start upload to Google Drive... 415 | [ 14/11/2019 10:58:56 ] Directory 14_11_2019 existed. Skipping... 416 | [ 14/11/2019 10:58:57 ] Uploading file /home/thanh1/backup2/backup/a.txt to directory 14_11_2019... 417 | [ 14/11/2019 10:58:59 ] [UPLOAD] Uploaded file /home/thanh1/backup2/backup/a.txt to directory 14_11_2019 418 | [ 14/11/2019 10:58:59 ] Uploading file /home/thanh1/backup2/backup/b.txt to directory 14_11_2019... 419 | [ 14/11/2019 10:59:02 ] [UPLOAD] Uploaded file /home/thanh1/backup2/backup/b.txt to directory 14_11_2019 420 | [ 14/11/2019 10:59:02 ] Uploading directory /home/thanh1/backup2/backup/thanh1 to directory 14_11_2019... 421 | [ 14/11/2019 10:59:03 ] [UPLOAD] Uploaded directory /home/thanh1/backup2/backup/thanh1 to directory 14_11_2019 422 | [ 14/11/2019 10:59:03 ] Uploading directory /home/thanh1/backup2/backup/thanh2 to directory 14_11_2019... 423 | [ 14/11/2019 10:59:04 ] [UPLOAD] Uploaded directory /home/thanh1/backup2/backup/thanh2 to directory 14_11_2019 424 | [ 14/11/2019 10:59:04 ] Finish! All files and directories in /home/thanh1/backup2/backup are uploaded to Google Drive in directory 14_11_2019 425 | [ 14/11/2019 10:59:05 ] Directory 15_10_2019 does not exist. Nothing need remove! 426 | ``` 427 | # Thank you for support! 428 | If you like my script, please Buy me a coffee Buy me a coffee :) 429 | --------------------------------------------------------------------------------