├── MD5SUM ├── CHANGLOG.md ├── LICENSE ├── cron_backup.bash ├── README.md └── butdr.bash /MD5SUM: -------------------------------------------------------------------------------- 1 | 0cf383a4faa95fae0ad7c1a3eae4bcf6 butdr.bash 2 | fd96fbb4214126c77a0cdc9a65990175 cron_backup.bash 3 | -------------------------------------------------------------------------------- /CHANGLOG.md: -------------------------------------------------------------------------------- 1 | #### butdr 1.4.1 (04/06/2020) Add support 2 | - [Add support] Add support Fedora 3 | 4 | #### butdr 1.4.0 (31/12/2019) Add support 5 | - [Add support] Add support Oracle Linux 6 | 7 | #### butdr 1.3.0 (25/12/2019) Add cloud 8 | - [Add cloud] Add support Backblaze 9 | 10 | #### butdr 1.2.0 (24/12/2019) Add cloud 11 | - [Add cloud] Add support Yandex 12 | - [Add cloud] Add support One Drive 13 | 14 | #### butdr 1.1.0 (20/12/2019) Add cloud 15 | - [Add cloud] Add support Dropbox 16 | 17 | #### butdr 1.0.0 (19/12/2019) First Release 18 | - [Release] Release butgg 1.0.0 stable 19 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /cron_backup.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Setup variables 4 | BIN_DIR="${HOME}/bin" 5 | CONF_DIR="${HOME}/.config" 6 | ACCT_DIR="${CONF_DIR}/accounts" 7 | BUTDR_CONF="${CONF_DIR}/butdr.conf" 8 | BUTDR_DEBUG="${CONF_DIR}/detail.log" 9 | BUTDR_EXCLUDE="${CONF_DIR}/exclude.list" 10 | RCLONE_BIN="${BIN_DIR}/rclone" 11 | DF_BACKUP_DIR="${HOME}/backup" 12 | DF_TAR_BEFORE_UPLOAD="No" 13 | DF_SYNC_FILE="No" 14 | DF_LOG_FILE="${CONF_DIR}/butdr.log" 15 | DF_DAY_REMOVE="7" 16 | DF_DRIVE_FOLDER_ID="None" 17 | DF_FOLDER_NAME="None" 18 | DF_EMAIL_USER="None" 19 | DF_EMAIL_PASS="None" 20 | DF_EMAIL_TO="None" 21 | FIRST_OPTION=$1 22 | 23 | # Date variables 24 | RUN_START=`date +%s` 25 | TODAY=`date +"%d_%m_%Y"` 26 | RANDOM_STRING=`date +%s | sha256sum | base64 | head -c 16` 27 | 28 | # Color variables 29 | GREEN='\e[32m' 30 | RED='\e[31m' 31 | YELLOW='\e[33m' 32 | REMOVE='\e[0m' 33 | 34 | # Time to run cron_backup 35 | run_time(){ 36 | RUN_END=`date +%s` 37 | RUN_TIME=`expr ${RUN_END} - ${RUN_START}` 38 | RUN_TIME=`date -d@${RUN_TIME} -u +%Hh%Mm%Ss` 39 | show_write_log "Run time: ${RUN_TIME}" 40 | } 41 | 42 | # Detect error 43 | detect_error(){ 44 | if [ $? -ne 0 ] 45 | then 46 | show_write_log "[${CURRENT_ACCOUNT}] `change_color red $3` $4. Exit" 47 | send_error_email "butdr $3 - [${CURRENT_ACCOUNT}]" "$4" 48 | run_time 49 | exit 1 50 | else 51 | show_write_log "[${CURRENT_ACCOUNT}] `change_color green $1` $2" 52 | fi 53 | } 54 | 55 | # Change color of words 56 | change_color(){ 57 | case $1 in 58 | green) echo -e "${GREEN}$2${REMOVE}";; 59 | red) echo -e "${RED}$2${REMOVE}";; 60 | yellow) echo -e "${YELLOW}$2${REMOVE}";; 61 | *) echo "$2";; 62 | esac 63 | } 64 | 65 | # Show processing and write log 66 | show_write_log(){ 67 | if [ "${FIRST_OPTION}" == "-v" ] 68 | then 69 | echo `date "+[ %d/%m/%Y %H:%M:%S ]"` $1 70 | fi 71 | echo `date "+[ %d/%m/%Y %H:%M:%S ]"` $1 >> ${LOG_FILE} 72 | } 73 | 74 | # Check file type 75 | check_file_type(){ 76 | if [ -d $1 ] 77 | then 78 | FILE_TYPE="directory" 79 | elif [ -f $1 ] 80 | then 81 | FILE_TYPE="file" 82 | else 83 | show_write_log "`change_color red [CHECK][FAIL]` Can not detect file type for $1. Exit" 84 | run_time 85 | exit 1 86 | fi 87 | } 88 | 89 | # Detect OS 90 | detect_os(){ 91 | show_write_log "Checking OS..." 92 | if [ -f /etc/os-release ] 93 | then 94 | OS=`cat /etc/os-release | grep "^NAME=" | cut -d'"' -f2 | awk '{print $1}'` 95 | show_write_log "OS supported" 96 | else 97 | show_write_log "Sorry! We do not support your OS. Exit" 98 | run_time 99 | exit 1 100 | fi 101 | } 102 | 103 | # Check config 104 | check_config(){ 105 | if [ $2 == LOG_FILE ] 106 | then 107 | show_write_log "---" 108 | fi 109 | if [ "$4" == "" ] 110 | then 111 | VALUE=$3 112 | if [ -f $1 ] 113 | then 114 | sed -i "/^$2=/d" $1 115 | fi 116 | echo "$2=$VALUE" >> $1 117 | show_write_log "`change_color yellow [WARNING]` $2 does not exist. Use default config" 118 | else 119 | VALUE=$4 120 | fi 121 | VAR=$2 122 | eval "$VAR"="$VALUE" 123 | if [ $3 == LOG_FILE ] 124 | then 125 | show_write_log "---" 126 | fi 127 | } 128 | 129 | # Get global config 130 | get_config_global(){ 131 | if [ ! -f ${BUTDR_CONF} ] 132 | then 133 | check_config ${BUTDR_CONF} LOG_FILE ${DF_LOG_FILE} 134 | check_config ${BUTDR_CONF} EMAIL_USER ${DF_EMAIL_USER} 135 | check_config ${BUTDR_CONF} EMAIL_PASS ${DF_EMAIL_PASS} 136 | check_config ${BUTDR_CONF} EMAIL_TO ${DF_EMAIL_TO} 137 | else 138 | LOG_FILE=`cat ${BUTDR_CONF} | grep "^LOG_FILE" | cut -d"=" -f2 | sed 's/"//g' | sed "s/'//g"` 139 | check_config ${BUTDR_CONF} LOG_FILE ${DF_LOG_FILE} ${LOG_FILE} 140 | EMAIL_USER=`cat ${BUTDR_CONF} | grep "^EMAIL_USER" | cut -d"=" -f2 | sed 's/"//g' | sed "s/'//g"` 141 | check_config ${BUTDR_CONF} EMAIL_USER ${DF_EMAIL_USER} ${EMAIL_USER} 142 | EMAIL_PASS=`cat ${BUTDR_CONF} | grep "^EMAIL_PASS" | cut -d"=" -f2 | sed 's/"//g' | sed "s/'//g"` 143 | check_config ${BUTDR_CONF} EMAIL_PASS ${DF_EMAIL_PASS} ${EMAIL_PASS} 144 | EMAIL_TO=`cat ${BUTDR_CONF} | grep "^EMAIL_TO" | cut -d"=" -f2 | sed 's/"//g' | sed "s/'//g"` 145 | check_config ${BUTDR_CONF} EMAIL_TO ${DF_EMAIL_TO} ${EMAIL_TO} 146 | fi 147 | } 148 | 149 | # Get backup config 150 | get_config_backup(){ 151 | if [ ! -f $1 ] 152 | then 153 | check_config $1 BACKUP_DIR ${DF_BACKUP_DIR} 154 | check_config $1 DAY_REMOVE ${DF_DAY_REMOVE} 155 | if [ "${CLOUD_TYPE}" == "drive" ] 156 | then 157 | check_config $1 DRIVE_FOLDER_ID ${DF_DRIVE_FOLDER_ID} 158 | fi 159 | check_config $1 SYNC_FILE ${DF_SYNC_FILE} 160 | check_config $1 TAR_BEFORE_UPLOAD ${DF_TAR_BEFORE_UPLOAD} 161 | else 162 | BACKUP_DIR=`cat $1 | grep "^BACKUP_DIR" | cut -d"=" -f2 | sed 's/"//g' | sed "s/'//g"` 163 | check_config $1 BACKUP_DIR ${DF_BACKUP_DIR} ${BACKUP_DIR} 164 | DAY_REMOVE=`cat $1 | grep "^DAY_REMOVE" | cut -d"=" -f2 | sed 's/"//g' | sed "s/'//g"` 165 | check_config $1 DAY_REMOVE ${DF_DAY_REMOVE} ${DAY_REMOVE} 166 | if [ "${CLOUD_TYPE}" == "drive" ] 167 | then 168 | DRIVE_FOLDER_ID=`cat $1 | grep "^DRIVE_FOLDER_ID" | cut -d"=" -f2 | sed 's/"//g' | sed "s/'//g"` 169 | check_config $1 DRIVE_FOLDER_ID ${DF_DRIVE_FOLDER_ID} ${DRIVE_FOLDER_ID} 170 | else 171 | FOLDER_NAME=`cat $1 | grep "^FOLDER_NAME" | cut -d"=" -f2 | sed 's/"//g' | sed "s/'//g"` 172 | check_config $1 FOLDER_NAME ${DF_FOLDER_NAME} ${FOLDER_NAME} 173 | fi 174 | SYNC_FILE=`cat $1 | grep "^SYNC_FILE" | cut -d"=" -f2 | sed 's/"//g' | sed "s/'//g"` 175 | check_config $1 SYNC_FILE ${DF_SYNC_FILE} ${SYNC_FILE} 176 | TAR_BEFORE_UPLOAD=`cat $1 | grep "^TAR_BEFORE_UPLOAD" | cut -d"=" -f2 | sed 's/"//g' | sed "s/'//g"` 177 | check_config $1 TAR_BEFORE_UPLOAD ${DF_TAR_BEFORE_UPLOAD} ${TAR_BEFORE_UPLOAD} 178 | fi 179 | } 180 | 181 | # Check infomations before upload to Cloud 182 | check_info(){ 183 | if [ ! -d "${BACKUP_DIR}" ] 184 | then 185 | show_write_log "`change_color red [CHECK][FAIL]` Directory ${BACKUP_DIR} does not exist. Exit" 186 | send_error_email "butdr [CHECK][FAIL] - [${CURRENT_ACCOUNT}]" "Directory ${BACKUP_DIR} does not exist" 187 | run_time 188 | exit 1 189 | fi 190 | show_write_log "[${CURRENT_ACCOUNT}] Checking connect to ${CLOUD_TYPE}..." 191 | ${RCLONE_BIN} lsd ${CURRENT_ACCOUNT}: &>/dev/null 192 | detect_error "[INFO]" "Connect ${CLOUD_TYPE} successful" "[CHECK][FAIL]" "Can not connect ${CLOUD_TYPE} with your credential, please check again" 193 | if [ "${CLOUD_TYPE}" == "drive" ] 194 | then 195 | FOLDER_NAME="" 196 | else 197 | if [ "${FOLDER_NAME}" == "None" ] 198 | then 199 | FOLDER_NAME="" 200 | else 201 | show_write_log "[${CURRENT_ACCOUNT}] Checking folder name on Cloud..." 202 | CHECK_FOLDER_NAME=`rclone lsd ${CLOUD_TYPE}: | awk '{print $5}' | grep -c "^${FOLDER_NAME}$"` 203 | if [ ${CHECK_FOLDER_NAME} -eq 0 ] 204 | then 205 | show_write_log "[${CURRENT_ACCOUNT}] `change_color red [CHECK][FAIL]` Can not find folder name ${FOLDER_NAME}. Exit" 206 | send_error_email "butdr [CHECK][FAIL] - [${CURRENT_ACCOUNT}]" "Can not find folder name ${FOLDER_NAME}" 207 | run_time 208 | exit 1 209 | else 210 | show_write_log "[${CURRENT_ACCOUNT}] Check folder name successful" 211 | fi 212 | fi 213 | fi 214 | } 215 | 216 | # Send error email 217 | send_error_email(){ 218 | if [ "${EMAIL_USER}" == "None" ] 219 | then 220 | show_write_log "`change_color yellow [WARNING]` Email not config, do not send error email" 221 | else 222 | show_write_log "Sending error email..." 223 | 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") 224 | if [ $? -ne 0 ] 225 | then 226 | echo "" >> ${BUTDR_DEBUG} 227 | echo `date "+[ %d/%m/%Y %H:%M:%S ]"` "---" >> ${BUTDR_DEBUG} 228 | 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 ${BUTDR_DEBUG}_${TODAY} 229 | cat ${BUTDR_DEBUG}_${TODAY} >> ${BUTDR_DEBUG} 230 | rm -f ${BUTDR_DEBUG}_${TODAY} 231 | show_write_log "`change_color red [EMAIL][FAIL]` Can not send error email. See ${BUTDR_DEBUG} for more detail" 232 | else 233 | show_write_log "Send error email successful" 234 | fi 235 | fi 236 | } 237 | 238 | # Run upload to Cloud 239 | run_upload(){ 240 | if [ "${TAR_BEFORE_UPLOAD}" == "Yes" ] 241 | then 242 | show_write_log "[${CURRENT_ACCOUNT}] Compressing backup directory..." 243 | cd ${BACKUP_DIR} 244 | BACKUP_DIR_NAME=`basename ${BACKUP_DIR}` 245 | tar -zcf ${BACKUP_DIR_NAME}_${RANDOM_STRING}.tar.gz * 246 | detect_error "[COMPRESS]" "Compress ${BACKUP_DIR} successful" "[COMPRESS][FAIL]" "Can not compress ${BACKUP_DIR}" 247 | show_write_log "[${CURRENT_ACCOUNT}] Uploading ${BACKUP_DIR_NAME}_${RANDOM_STRING}.tar.gz to directory ${TODAY}..." 248 | ${RCLONE_BIN} copy -P ${BACKUP_DIR_NAME}_${RANDOM_STRING}.tar.gz ${CURRENT_ACCOUNT}:${FOLDER_NAME}/${TODAY} 249 | detect_error "[UPLOAD]" "Uploaded ${BACKUP_DIR_NAME}_${RANDOM_STRING}.tar.gz to directory ${TODAY}" "[UPLOAD][FAIL]" "Can not upload to Cloud" 250 | show_write_log "[${CURRENT_ACCOUNT}] Removing ${BACKUP_DIR_NAME}_${RANDOM_STRING}.tar.gz after upload..." 251 | rm -f ${BACKUP_DIR_NAME}_${RANDOM_STRING}.tar.gz 252 | if [ $? -ne 0 ] 253 | then 254 | show_write_log "[${CURRENT_ACCOUNT}] `change_color red [REMOVE][FAIL]` Can not remove file ${BACKUP_DIR_NAME}_${RANDOM_STRING}.tar.gz on local. You must delete it manually" 255 | send_error_email "butdr [REMOVE][FAIL] - [${CURRENT_ACCOUNT}]" "Can not remove file ${BACKUP_DIR_NAME}_${RANDOM_STRING}.tar.gz on local. You must delete it manually" 256 | else 257 | show_write_log "[${CURRENT_ACCOUNT}] `change_color green [REMOVE]` Remove file ${BACKUP_DIR_NAME}_${RANDOM_STRING}.tar.gz on local successful" 258 | fi 259 | elif [ "${TAR_BEFORE_UPLOAD}" == "No" ] 260 | then 261 | BACKUP_DIR=`realpath ${BACKUP_DIR}` 262 | if [ -f "${ACCT_DIR}/${CURRENT_ACCOUNT}.include" ] 263 | then 264 | show_write_log "[${CURRENT_ACCOUNT}] `change_color green [INFO]` File ${CURRENT_ACCOUNT}.include exists, only upload file & directories inside it" 265 | ${RCLONE_BIN} copy -P ${BACKUP_DIR} ${CURRENT_ACCOUNT}:${FOLDER_NAME}/${TODAY} --create-empty-src-dirs --include-from ${ACCT_DIR}/${CURRENT_ACCOUNT}.include 266 | detect_error "[UPLOAD]" "Finish! All files and directories in ${ACCT_DIR}/${CURRENT_ACCOUNT}.list are uploaded to Cloud" "[UPLOAD][FAIL]" "Can not upload to Cloud" 267 | else 268 | show_write_log "[${CURRENT_ACCOUNT}] `change_color green [INFO]` You do not compress directory before upload" 269 | show_write_log "[${CURRENT_ACCOUNT}] Uploading from ${BACKUP_DIR} to ${TODAY} on Cloud" 270 | if [ -f "${ACCT_DIR}/${CURRENT_ACCOUNT}.exclude" ] 271 | then 272 | ${RCLONE_BIN} copy -P ${BACKUP_DIR} ${CURRENT_ACCOUNT}:${FOLDER_NAME}/${TODAY} --create-empty-src-dirs --exclude-from ${ACCT_DIR}/${CURRENT_ACCOUNT}.exclude 273 | else 274 | ${RCLONE_BIN} copy -P ${BACKUP_DIR} ${CURRENT_ACCOUNT}:${FOLDER_NAME}/${TODAY} --create-empty-src-dirs 275 | fi 276 | detect_error "[UPLOAD]" "Finish! All files and directories in ${BACKUP_DIR} are uploaded to Cloud" "[UPLOAD][FAIL]" "Can not upload to Cloud" 277 | fi 278 | else 279 | show_write_log "[${CURRENT_ACCOUNT}] `change_color yellow [CHECK][FAIL]` Option TAR_BEFORE_UPLOAD=${TAR_BEFORE_UPLOAD} not support. Only Yes or No. Exit" 280 | send_error_email "butdr [CHECK][FAIL] - [${CURRENT_ACCOUNT}]" "Option TAR_BEFORE_UPLOAD=${TAR_BEFORE_UPLOAD} not support" 281 | run_time 282 | exit 1 283 | fi 284 | } 285 | 286 | # Remove old directory on Google Drive 287 | remove_old_dir(){ 288 | OLD_BACKUP_DAY=`date +%d_%m_%Y -d "-${DAY_REMOVE} day"` 289 | CHECK_OLD_BACKUP_DIR=`${RCLONE_BIN} lsd ${CURRENT_ACCOUNT}:${FOLDER_NAME} | awk '{print $5}' | grep -c "${OLD_BACKUP_DAY}"` 290 | if [ ${CHECK_OLD_BACKUP_DIR} -eq 0 ] 291 | then 292 | show_write_log "[${CURRENT_ACCOUNT}] Directory ${OLD_BACKUP_DAY} on Cloud does not exist. Nothing need remove!" 293 | else 294 | if [ "${CLOUD_TYPE}" == "drive" ] 295 | then 296 | ${RCLONE_BIN} purge ${CURRENT_ACCOUNT}:${FOLDER_NAME}/${OLD_BACKUP_DAY} --drive-use-trash=false 297 | detect_error "[REMOVE]" "Removed directory ${OLD_BACKUP_DAY} successful" "[REMOVE][FAIL]" "Directory ${OLD_BACKUP_DAY} on Cloud exists but can not remove!" 298 | elif [ "${CLOUD_TYPE}" == "yandex" ] 299 | then 300 | ${RCLONE_BIN} purge ${CURRENT_ACCOUNT}:${FOLDER_NAME}/${OLD_BACKUP_DAY} 301 | YANDEX_TOKEN=`cat ${CONF_DIR}/rclone/rclone.conf | grep "access_token" | cut -d'"' -f4` 302 | YANDEX_REMOVE=`curl -s -X DELETE "https://cloud-api.yandex.net/v1/disk/trash/resources?path=${OLD_BACKUP_DAY}" -H "Authorization: OAuth ${YANDEX_TOKEN}"` 303 | if [ -z "${YANDEX_REMOVE}" ] 304 | then 305 | show_write_log "[${CURRENT_ACCOUNT}] `change_color green [REMOVE]` Removed directory ${OLD_BACKUP_DAY} on Cloud successful" 306 | else 307 | echo "---" >> ${BUTDR_DEBUG} 308 | echo "${YANDEX_REMOVE}" >> ${BUTDR_DEBUG} 309 | show_write_log "[${CURRENT_ACCOUNT}] `change_color red [REMOVE][FAIL]` Directory ${OLD_BACKUP_DAY} on Cloud exists but can not remove. See ${BUTDR_DEBUG} for more detail. Exit" 310 | send_error_email "butdr [REMOVE][FAIL] - [${CURRENT_ACCOUNT}]" "Directory ${OLD_BACKUP_DAY} on Cloud exists but can not remove!" 311 | run_time 312 | exit 1 313 | fi 314 | elif [ "${CLOUD_TYPE}" == "backblaze" ] 315 | then 316 | ${RCLONE_BIN} purge ${CURRENT_ACCOUNT}:${FOLDER_NAME}/${OLD_BACKUP_DAY} 317 | detect_error "[REMOVE]" "Removed directory ${OLD_BACKUP_DAY} successful" "[REMOVE][FAIL]" "Directory ${OLD_BACKUP_DAY} on Cloud exists but can not remove!" 318 | else 319 | ${RCLONE_BIN} purge ${CURRENT_ACCOUNT}:${FOLDER_NAME}/${OLD_BACKUP_DAY} 320 | if [ $? -eq 0 ] 321 | then 322 | show_write_log "[${CURRENT_ACCOUNT}] `change_color yellow [WARNING]` Only remove ${OLD_BACKUP_DAY} on Cloud to trash, please manual delete it permanently" 323 | send_error_email "butdr [WARNING] - [${CURRENT_ACCOUNT}]" "Only remove ${OLD_BACKUP_DAY} on Cloud to trash, please manual delete it permanently" 324 | else 325 | show_write_log "[${CURRENT_ACCOUNT}] `change_color red [REMOVE][FAIL]` Directory ${OLD_BACKUP_DAY} on Cloud exists but can not remove. Exit" 326 | send_error_email "butdr [REMOVE][FAIL] - [${CURRENT_ACCOUNT}]" "Directory ${OLD_BACKUP_DAY} on Cloud exists but can not remove!" 327 | run_time 328 | exit 1 329 | fi 330 | fi 331 | fi 332 | } 333 | 334 | # Sync data from local to Google Drive 335 | run_sync(){ 336 | show_write_log "[${CURRENT_ACCOUNT}] Syncing ${BACKUP_DIR} to Cloud..." 337 | ${RCLONE_BIN} sync -P ${BACKUP_DIR} ${CURRENT_ACCOUNT}:${FOLDER_NAME} --create-empty-src-dirs 338 | detect_error "[SYNC]" "Finish! All files and directories in ${BACKUP_DIR} are synced to Cloud" "[SYNC][FAIL]" "Can not Sync with Cloud" 339 | } 340 | 341 | # Main functions 342 | get_config_global 343 | detect_os 344 | show_write_log "Start upload to Cloud..." 345 | CLOUD_TYPE=`cat ${BUTDR_CONF} | grep "^CLOUD_TYPE=" | cut -d"=" -f2` 346 | CURRENT_ACCOUNT=${CLOUD_TYPE} 347 | get_config_backup ${ACCT_DIR}/${CURRENT_ACCOUNT}.conf 348 | check_info 349 | if [ "${SYNC_FILE}" == "No" ] 350 | then 351 | run_upload 352 | remove_old_dir 353 | elif [ "${SYNC_FILE}" == "Yes" ] 354 | then 355 | run_sync 356 | else 357 | show_write_log "`change_color yellow [CHECK][FAIL]` Option SYNC_FILE=${SYNC_FILE} not support. Only Yes or No. Exit" 358 | send_error_email "butdr [CHECK][FAIL] - [${CURRENT_ACCOUNT}]" "Option SYNC_FILE=${SYNC_FILE} not support" 359 | run_time 360 | exit 1 361 | fi 362 | show_write_log "Finish! All files and directories are uploaded or synced to Cloud" 363 | run_time -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # butdr 2 | Backup to Cloud solution use [rclone](https://github.com/rclone/rclone) 3 | --- 4 | 5 | # What can this script do? 6 | - Download & config rclone with your [Cloud](https://github.com/mbrother2/butdr/blob/master/README.md#cloud-support) credential 7 | - Create cron auto backup 8 | - Exclude/include file/directory when run cron backup 9 | - Sync backup directory from local to Cloud 10 | - Compress backup directory before upload 11 | - Send error email if upload to Cloud fail 12 | - Auto remove old backup on Cloud 13 | - Run upload from your backup directory to Cloud whenever you want 14 | - Detail log 15 | 16 | # Structure 17 | ``` 18 | $HOME (/root or /home/$USER) 19 | ├── bin 20 | │   ├── butdr.bash 21 | │ ├── cron_backup.bash 22 | │   └── rclone 23 | │ 24 | └── .config 25 | ├── accounts 26 | │   ├── .conf 27 | │ ├── .exclude 28 | │   └── .include 29 | ├── butdr.conf 30 | ├── butdr.log 31 | ├── detail.log 32 | └── rclone 33 | └── rclone.conf 34 | 35 | ``` 36 | 37 | # OS support(x86_64): 38 | - **Linux:** CentOS, Debian, Ubuntu, openSUSE, Oracle Linux 39 | 40 | # Cloud support: 41 | - Google Drive 42 | - Dropbox 43 | - Yandex 44 | - One Drive 45 | - Backblaze 46 | 47 | # How to use 48 | **On Linux system:** 49 | ``` 50 | curl -o butdr.bash https://raw.githubusercontent.com/mbrother2/butdr/master/butdr.bash 51 | bash butdr.bash --setup 52 | ``` 53 | After install, run backup immediately to test: 54 | ``` 55 | cron_backup.bash -v 56 | ``` 57 | 58 | # Wiki 59 | ### Common 60 | ##### [What is the option SYNC_FILE?](https://github.com/mbrother2/butdr/wiki/What-is-the-option-SYNC_FILE%3F) 61 | 62 | ### Google Drive( drive) 63 | ##### [Create own Google credential step by step](https://github.com/mbrother2/butdr/wiki/Create-own-Google-credential-step-by-step) 64 | ##### [Get Google folder ID](https://github.com/mbrother2/butdr/wiki/Get-Google-folder-ID) 65 | ##### [Turn on 2 Step Verification & create app's password for Google email](https://github.com/mbrother2/butdr/wiki/Turn-on-2-Step-Verification-&-create-app's-password-for-Google-email) 66 | 67 | ### Dropbox( dropbox) 68 | ##### [Create own Dropbox credential step by step](https://github.com/mbrother2/butdr/wiki/Create-own-Dropbox-credential-step-by-step) 69 | 70 | ### Yandex( yandex) 71 | ##### [Create own Yandex credential step by step](https://github.com/mbrother2/butdr/wiki/Create-own-Yandex-credential-step-by-step) 72 | 73 | ### One Drive( onedrive) 74 | ##### [Create own One Drive credential step by step](https://github.com/mbrother2/butdr/wiki/Create-own-One-Drive-credential-step-by-step) 75 | 76 | ### Backblaze( b2) 77 | ##### [Create own Backblaze credential step by step](https://github.com/mbrother2/butdr/wiki/Create-own-Backblaze-credential-step-by-step) 78 | ##### [Backblaze Create new bucket](https://github.com/mbrother2/butdr/wiki/Backblaze-Create-new-bucket) 79 | 80 | # Change log 81 | https://github.com/mbrother2/butdr/blob/master/CHANGLOG.md 82 | 83 | # Options 84 | Run command `bash butdr.bash --help` to show all options( After install you only need run `butdr.bash --help` 85 | ``` 86 | butdr@ubuntu1804:~$ butdr.bash --help 87 | butdr.bash - Backup to Cloud solution 88 | 89 | Usage: butdr.bash [option] [command] 90 | 91 | Options: 92 | --help show this help message and exit 93 | --setup setup or reset all scripts & config file 94 | --account reset account 95 | reset reset all accounts 96 | --config config global or backup file 97 | backup-single config backup file for single account 98 | global config global file 99 | show show all configs 100 | --update update butdr.bash & cron_backup.bash to latest version 101 | --uninstall remove all butdr scripts and /home/butdr/.config directory 102 | ``` 103 | 104 | ###### 1. Setup 105 | `butdr.bash --setup` 106 | Setup or reset all scripts & config file 107 | ##### Example 108 | ``` 109 | butdr@ubuntu1804:~$ bash butdr.bash --setup 110 | [ 19/12/2019 12:03:27 ] --- 111 | [ 19/12/2019 12:03:27 ] Creating necessary directory... 112 | [ 19/12/2019 12:03:27 ] Directory /home/butdr/.config existed. Skip 113 | [ 19/12/2019 12:03:27 ] Check write to /home/butdr/.config successful 114 | [ 19/12/2019 12:03:27 ] Create directory /home/butdr/.config/accounts successful 115 | [ 19/12/2019 12:03:27 ] Check write to /home/butdr/.config/accounts successful 116 | [ 19/12/2019 12:03:27 ] Directory /home/butdr/bin existed. Skip 117 | [ 19/12/2019 12:03:27 ] Check write to /home/butdr/bin successful 118 | [ 19/12/2019 12:03:27 ] Checking OS... 119 | [ 19/12/2019 12:03:27 ] OS supported 120 | [ 19/12/2019 12:03:27 ] Checking necessary package... 121 | [ 19/12/2019 12:03:27 ] Package curl is installed 122 | [ 19/12/2019 12:03:27 ] Package unzip is installed 123 | [ 19/12/2019 12:03:27 ] Cheking network... 124 | [ 19/12/2019 12:03:27 ] Connect Github successful 125 | [ 19/12/2019 12:03:29 ] Connect rclone successful 126 | [ 19/12/2019 12:03:29 ] Downloading script cron_backup from github... 127 | [ 19/12/2019 12:03:29 ] Check md5sum for file cron_backup.bash successful 128 | [ 19/12/2019 12:03:30 ] Downloading script butdr from github... 129 | [ 19/12/2019 12:03:30 ] Check md5sum for file butdr.bash successful 130 | [ 19/12/2019 12:03:30 ] Downloading rclone from homepage... 131 | % Total % Received % Xferd Average Speed Time Time Time Current 132 | Dload Upload Total Spent Left Speed 133 | 100 11.1M 100 11.1M 0 0 888k 0 0:00:12 0:00:12 --:--:-- 1183k 134 | [ 19/12/2019 12:03:43 ] Download rclone successful 135 | 136 | Read more: https://github.com/mbrother2/backuptogoogle/wiki/Create-own-Google-credential-step-by-step 137 | Your Google API client_id: xxxxxx 138 | Your Google API client_secret: xxxxxx 139 | 140 | Read more https://github.com/mbrother2/backuptogoogle/wiki/Get-Google-folder-ID 141 | Your Google folder ID: 1TQwTpELBtA5SaIi8mIVzVJdcdv_GNzM3 142 | 2019/12/19 13:35:04 NOTICE: Config file "/home/butdr/.config/rclone/rclone.conf" not found - using defaults 143 | Remote config 144 | Use auto config? 145 | * Say Y if not sure 146 | * Say N if you are working on a remote or headless machine 147 | Auto confirm is set: answering No, override by setting config parameter config_is_local=true 148 | If your browser doesn't open automatically go to the following link: https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=xxxxxx&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&state=A6Joak1v_mTJ_Yct8i3diQ 149 | Log in and authorize rclone for access 150 | Enter verification code> 4/xxxxxx 151 | -------------------- 152 | [googledrive] 153 | type = drive 154 | root_folder_id = 1TQwTpELBtA5SaIi8mIVzVJdcdv_GNzM3 155 | config_is_local = false 156 | scope = drive 157 | client_id = xxxxxx 158 | client_secret = xxxxxx 159 | token = {"access_token":"ya29.Il-xxxxxx","token_type":"Bearer","refresh_token":"1//xxxxxx","expiry":"2019-12-19T14:35:26.759362203+07:00"} 160 | -------------------- 161 | 162 | Which directory on your server do you want to upload to account googledrive?(default /home/butdr/backup): 163 | How many days do you want to keep backup on Google Drive?(default 7): 164 | 165 | Read more https://github.com/mbrother2/backuptogoogle/wiki/What-is-the-option-SYNC_FILE%3F 166 | Do you want only sync file(default no)(y/n): 167 | Do you want compress directory before upload?(default no)(y/n): 168 | [ 19/12/2019 13:35:31 ] Setup config file for account googledrive successful 169 | [ 19/12/2019 13:35:31 ] Setting up global config file... 170 | 171 | Read more https://github.com/mbrother2/backuptogoogle/wiki/Turn-on-2-Step-Verification-&-create-app's-password-for-Google-email 172 | Do you want to send email if upload error(default no)(y/n): 173 | [ 19/12/2019 13:35:31 ] Setup global config file successful 174 | 175 | [ 19/12/2019 13:35:31 ] Setting up cron backup... 176 | [ 19/12/2019 13:35:31 ] Cron backup existed. Skip 177 | 178 | [ 19/12/2019 13:35:31 ] +----- 179 | [ 19/12/2019 13:35:31 ] | SUCESSFUL! Your informations: 180 | [ 19/12/2019 13:35:31 ] |--- 181 | [ 19/12/2019 13:35:31 ] | Your email : None 182 | [ 19/12/2019 13:35:31 ] | Email password : None 183 | [ 19/12/2019 13:35:31 ] | Email notify : None 184 | [ 19/12/2019 13:35:31 ] | Global config file : /home/butdr/.config/butdr.conf 185 | [ 19/12/2019 13:35:32 ] |--- 186 | [ 19/12/2019 13:35:32 ] | Account : googledrive 187 | [ 19/12/2019 13:35:32 ] | Backup dir : /home/butdr/backup 188 | [ 19/12/2019 13:35:32 ] | Keep backup : 7 days 189 | [ 19/12/2019 13:35:32 ] | Google folder ID : 1TQwTpELBtA5SaIi8mIVzVJdcdv_GNzM3 190 | [ 19/12/2019 13:35:32 ] | Sync file : No 191 | [ 19/12/2019 13:35:32 ] | Tar before upload : No 192 | [ 19/12/2019 13:35:32 ] +----- 193 | 194 | IMPORTANT: Please run command to use butdr: source /home/butdr/.profile 195 | If you get trouble when use butdr.bash please report here: 196 | https://github.com/mbrother2/butdr/issues 197 | ``` 198 | 199 | ###### 2. Account 200 | `butdr.bash --account reset` 201 | Reset account 202 | ##### Example 203 | ``` 204 | butdr@ubuntu1804:~$ butdr.bash --account reset 205 | [ 19/12/2019 14:03:58 ] --- 206 | [ 19/12/2019 14:03:58 ] Creating necessary directory... 207 | [ 19/12/2019 14:03:58 ] Directory /home/butdr/.config existed. Skip 208 | [ 19/12/2019 14:03:58 ] Check write to /home/butdr/.config successful 209 | [ 19/12/2019 14:03:58 ] Directory /home/butdr/.config/accounts existed. Skip 210 | [ 19/12/2019 14:03:58 ] Check write to /home/butdr/.config/accounts successful 211 | [ 19/12/2019 14:03:58 ] Directory /home/butdr/bin existed. Skip 212 | [ 19/12/2019 14:03:58 ] Check write to /home/butdr/bin successful 213 | [ 19/12/2019 14:03:58 ] Creating googledrive account... 214 | 215 | Read more: https://github.com/mbrother2/backuptogoogle/wiki/Create-own-Google-credential-step-by-step 216 | Your Google API client_id: xxxxxx 217 | Your Google API client_secret: xxxxxx 218 | 219 | Read more https://github.com/mbrother2/backuptogoogle/wiki/Get-Google-folder-ID 220 | Your Google folder ID: 1TQwTpELBtA5SaIi8mIVzVJdcdv_GNzM3 221 | 2019/12/19 14:04:18 NOTICE: Config file "/home/butdr/.config/rclone/rclone.conf" not found - using defaults 222 | Remote config 223 | Use auto config? 224 | * Say Y if not sure 225 | * Say N if you are working on a remote or headless machine 226 | Auto confirm is set: answering No, override by setting config parameter config_is_local=true 227 | If your browser doesn't open automatically go to the following link: https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=xxxxxx&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&state=G33rjrgGHNXRKPs59H5JOQ 228 | Log in and authorize rclone for access 229 | Enter verification code> 4/xxxxxx 230 | -------------------- 231 | [googledrive] 232 | type = drive 233 | config_is_local = false 234 | scope = drive 235 | client_id = xxxxxx 236 | client_secret = xxxxxx 237 | root_folder_id = 1TQwTpELBtA5SaIi8mIVzVJdcdv_GNzM3 238 | token = {"access_token":"ya29.Il-xxxxxx","token_type":"Bearer","refresh_token":"1//xxxxxx","expiry":"2019-12-19T15:04:43.403854207+07:00"} 239 | -------------------- 240 | 241 | [ 19/12/2019 14:04:43 ] +----- 242 | [ 19/12/2019 14:04:43 ] | SUCESSFUL! Your informations: 243 | [ 19/12/2019 14:04:43 ] |--- 244 | [ 19/12/2019 14:04:43 ] | Account : googledrive 245 | [ 19/12/2019 14:04:43 ] | Backup dir : /home/butdr/backup 246 | [ 19/12/2019 14:04:43 ] | Keep backup : 7 days 247 | [ 19/12/2019 14:04:43 ] | Google folder ID : 1TQwTpELBtA5SaIi8mIVzVJdcdv_GNzM3 248 | [ 19/12/2019 14:04:43 ] | Sync file : No 249 | [ 19/12/2019 14:04:43 ] | Tar before upload : No 250 | [ 19/12/2019 14:04:43 ] +----- 251 | ``` 252 | 253 | ###### 3. Config 254 | `butdr.bash --config backup-single` 255 | Config backup file for single account 256 | ##### Example 257 | ``` 258 | butdr@ubuntu1804:~$ butdr.bash --config backup-single 259 | [ 19/12/2019 14:08:41 ] --- 260 | [ 19/12/2019 14:08:41 ] Creating necessary directory... 261 | [ 19/12/2019 14:08:41 ] Directory /home/butdr/.config existed. Skip 262 | [ 19/12/2019 14:08:41 ] Check write to /home/butdr/.config successful 263 | [ 19/12/2019 14:08:41 ] Directory /home/butdr/.config/accounts existed. Skip 264 | [ 19/12/2019 14:08:41 ] Check write to /home/butdr/.config/accounts successful 265 | [ 19/12/2019 14:08:41 ] Directory /home/butdr/bin existed. Skip 266 | [ 19/12/2019 14:08:42 ] Check write to /home/butdr/bin successful 267 | 268 | [ 19/12/2019 14:08:42 ] Setting up backup config file... 269 | 270 | Which directory on your server do you want to upload to account googledrive?(default /home/butdr/backup): /home/butdr/backup2 271 | How many days do you want to keep backup on Google Drive?(default 7): 30 272 | 273 | Read more https://github.com/mbrother2/backuptogoogle/wiki/What-is-the-option-SYNC_FILE%3F 274 | Do you want only sync file(default no)(y/n): y 275 | 276 | Read more https://github.com/mbrother2/backuptogoogle/wiki/Get-Google-folder-ID 277 | Because you choose sync file method, so you must enter exactly Google folder ID here! 278 | Your Google folder ID(default None): 1TQwTpELBtA5SaIi8mIVzVJdcdv_GNzM3 279 | [ 19/12/2019 14:09:03 ] [WARNING] Directory /home/butdr/backup2 does not exist! Ensure you will be create it after. 280 | [ 19/12/2019 14:09:06 ] Setup config file for account googledrive successful 281 | 282 | [ 19/12/2019 14:09:06 ] +----- 283 | [ 19/12/2019 14:09:06 ] | SUCESSFUL! Your informations: 284 | [ 19/12/2019 14:09:06 ] |--- 285 | [ 19/12/2019 14:09:06 ] | Account : googledrive 286 | [ 19/12/2019 14:09:06 ] | Backup dir : /home/butdr/backup2 287 | [ 19/12/2019 14:09:06 ] | Keep backup : 30 days 288 | [ 19/12/2019 14:09:06 ] | Google folder ID : 1TQwTpELBtA5SaIi8mIVzVJdcdv_GNzM3 289 | [ 19/12/2019 14:09:06 ] | Sync file : Yes 290 | [ 19/12/2019 14:09:06 ] | Tar before upload : No 291 | [ 19/12/2019 14:09:06 ] +----- 292 | ``` 293 | `butdr.bash --config global` 294 | Config global file 295 | ##### Example 296 | ``` 297 | butdr@ubuntu1804:~$ butdr.bash --config global 298 | [ 19/12/2019 14:10:48 ] --- 299 | [ 19/12/2019 14:10:48 ] Creating necessary directory... 300 | [ 19/12/2019 14:10:48 ] Directory /home/butdr/.config existed. Skip 301 | [ 19/12/2019 14:10:49 ] Check write to /home/butdr/.config successful 302 | [ 19/12/2019 14:10:49 ] Directory /home/butdr/.config/accounts existed. Skip 303 | [ 19/12/2019 14:10:49 ] Check write to /home/butdr/.config/accounts successful 304 | [ 19/12/2019 14:10:49 ] Directory /home/butdr/bin existed. Skip 305 | [ 19/12/2019 14:10:49 ] Check write to /home/butdr/bin successful 306 | [ 19/12/2019 14:10:49 ] Setting up global config file... 307 | 308 | Read more https://github.com/mbrother2/backuptogoogle/wiki/Turn-on-2-Step-Verification-&-create-app's-password-for-Google-email 309 | Do you want to send email if upload error(default no)(y/n): y 310 | Your Google email user name: backupxxxxxx@gmail.com 311 | Your Google email password: xxxxxx 312 | Which email will be receive notify?: backupxxxxxx@gmail.com 313 | [ 19/12/2019 14:11:11 ] Setup global config file successful 314 | 315 | [ 19/12/2019 14:11:11 ] +----- 316 | [ 19/12/2019 14:11:11 ] | SUCESSFUL! Your informations: 317 | [ 19/12/2019 14:11:11 ] |--- 318 | [ 19/12/2019 14:11:11 ] | Your email : backupxxxxxx@gmail.com 319 | [ 19/12/2019 14:11:11 ] | Email password : xxxxxx 320 | [ 19/12/2019 14:11:11 ] | Email notify : backupxxxxxx@gmail.com 321 | [ 19/12/2019 14:11:11 ] | Global config file : /home/butdr/.config/butdr.conf 322 | [ 19/12/2019 14:11:11 ] +----- 323 | ``` 324 | `butdr.bash --config show` 325 | Show all configs 326 | ##### Example 327 | ``` 328 | butdr@ubuntu1804:~$ butdr.bash --config show 329 | [ 19/12/2019 14:12:20 ] --- 330 | [ 19/12/2019 14:12:20 ] Creating necessary directory... 331 | [ 19/12/2019 14:12:20 ] Directory /home/butdr/.config existed. Skip 332 | [ 19/12/2019 14:12:20 ] Check write to /home/butdr/.config successful 333 | [ 19/12/2019 14:12:20 ] Directory /home/butdr/.config/accounts existed. Skip 334 | [ 19/12/2019 14:12:20 ] Check write to /home/butdr/.config/accounts successful 335 | [ 19/12/2019 14:12:20 ] Directory /home/butdr/bin existed. Skip 336 | [ 19/12/2019 14:12:20 ] Check write to /home/butdr/bin successful 337 | 338 | [ 19/12/2019 14:12:20 ] +----- 339 | [ 19/12/2019 14:12:20 ] | SUCESSFUL! Your informations: 340 | [ 19/12/2019 14:12:20 ] |--- 341 | [ 19/12/2019 14:12:20 ] | Your email : backupxxxxxx@gmail.com 342 | [ 19/12/2019 14:12:20 ] | Email password : xxxxxx 343 | [ 19/12/2019 14:12:20 ] | Email notify : backupxxxxxx@gmail.com 344 | [ 19/12/2019 14:12:20 ] | Global config file : /home/butdr/.config/butdr.conf 345 | [ 19/12/2019 14:12:20 ] |--- 346 | [ 19/12/2019 14:12:20 ] | Account : googledrive 347 | [ 19/12/2019 14:12:20 ] | Backup dir : /home/butdr/backup2 348 | [ 19/12/2019 14:12:20 ] | Keep backup : 30 days 349 | [ 19/12/2019 14:12:20 ] | Google folder ID : 1TQwTpELBtA5SaIi8mIVzVJdcdv_GNzM3 350 | [ 19/12/2019 14:12:20 ] | Sync file : Yes 351 | [ 19/12/2019 14:12:20 ] | Tar before upload : No 352 | [ 19/12/2019 14:12:20 ] +----- 353 | ``` 354 | 355 | ###### 4. Update 356 | `butdr.bash --update` 357 | Update butdr.bash & cron_backup.bash to latest version 358 | ##### Example 359 | ``` 360 | butdr@ubuntu1804:~$ butdr.bash --update 361 | [ 19/12/2019 14:12:52 ] --- 362 | [ 19/12/2019 14:12:52 ] Creating necessary directory... 363 | [ 19/12/2019 14:12:52 ] Directory /home/butdr/.config existed. Skip 364 | [ 19/12/2019 14:12:52 ] Check write to /home/butdr/.config successful 365 | [ 19/12/2019 14:12:52 ] Directory /home/butdr/.config/accounts existed. Skip 366 | [ 19/12/2019 14:12:52 ] Check write to /home/butdr/.config/accounts successful 367 | [ 19/12/2019 14:12:52 ] Directory /home/butdr/bin existed. Skip 368 | [ 19/12/2019 14:12:52 ] Check write to /home/butdr/bin successful 369 | [ 19/12/2019 14:12:52 ] Checking OS... 370 | [ 19/12/2019 14:12:52 ] OS supported 371 | [ 19/12/2019 14:12:52 ] Checking necessary package... 372 | [ 19/12/2019 14:12:52 ] Package curl is installed 373 | [ 19/12/2019 14:12:52 ] Package unzip is installed 374 | [ 19/12/2019 14:12:52 ] Cheking network... 375 | [ 19/12/2019 14:12:52 ] Connect Github successful 376 | [ 19/12/2019 14:12:52 ] Downloading script cron_backup from github... 377 | [ 19/12/2019 14:12:53 ] Check md5sum for file cron_backup.bash successful 378 | [ 19/12/2019 14:12:53 ] Downloading script butdr from github... 379 | [ 19/12/2019 14:12:54 ] Check md5sum for file butdr.bash successful 380 | [ 19/12/2019 14:12:54 ] [INFO] Update butdr successful 381 | ``` 382 | 383 | ###### 5. Uninstall 384 | `butdr.bash --uninstall` 385 | Remove all butdr scripts and /home/butdr.config directory 386 | ##### Example 387 | ``` 388 | butdr@ubuntu1804:~$ butdr.bash --uninstall 389 | [ 19/12/2019 14:16:17 ] --- 390 | [ 19/12/2019 14:16:17 ] Creating necessary directory... 391 | [ 19/12/2019 14:16:17 ] Directory /home/butdr/.config existed. Skip 392 | [ 19/12/2019 14:16:17 ] Check write to /home/butdr/.config successful 393 | [ 19/12/2019 14:16:17 ] Directory /home/butdr/.config/accounts existed. Skip 394 | [ 19/12/2019 14:16:17 ] Check write to /home/butdr/.config/accounts successful 395 | [ 19/12/2019 14:16:17 ] Directory /home/butdr/bin existed. Skip 396 | [ 19/12/2019 14:16:17 ] Check write to /home/butdr/bin successful 397 | [ 19/12/2019 14:16:17 ] Removing all butdr.bash scripts... 398 | [ 19/12/2019 14:16:17 ] Remove all butdr.bash scripts successful 399 | Do you want remove /home/butdr/.config directory?(y/n) y 400 | [ 19/12/2019 14:16:22 ]Remove directory /home/butdr/.config successful 401 | ``` 402 | 403 | ###### 6. Run cron_backup.bash 404 | `cron_backup.bash` 405 | Run upload to Google Drive immediately without show log 406 | 407 | `cron_backup.bash -v` 408 | Run upload to Google Drive immediately with show log detail 409 | ##### Example 410 | Backup to cloud normaly 411 | ``` 412 | butdr@ubuntu1804:~$ cron_backup.bash -v 413 | [ 19/12/2019 14:15:52 ] --- 414 | [ 19/12/2019 14:15:52 ] Checking OS... 415 | [ 19/12/2019 14:15:52 ] OS supported 416 | [ 19/12/2019 14:15:52 ] Start upload to Cloud... 417 | [ 19/12/2019 14:15:52 ] [googledrive] Checking Google folder ID... 418 | [ 19/12/2019 14:15:53 ] [googledrive] [INFO] Check Google folder ID successful 419 | [ 19/12/2019 14:15:53 ] [googledrive] Directory 19_12_2019 existed. Skipping... 420 | [ 19/12/2019 14:15:53 ] [googledrive] [INFO] You do not compress directory before upload 421 | [ 19/12/2019 14:15:53 ] [googledrive] Uploading from /home/butdr/backup2 to 19_12_2019 on Cloud 422 | [ 19/12/2019 14:16:07 ] [googledrive] [UPLOAD] Finish! All files and directories in /home/butdr/backup2 are uploaded to Cloud 423 | [ 19/12/2019 14:16:10 ] [googledrive] Directory 19_11_2019 does not exist. Nothing need remove! 424 | [ 19/12/2019 14:16:10 ] Finish! All files and directories are uploaded or synced to Cloud 425 | ``` 426 | 427 | Backup to cloud with SYNC_FILE=yes 428 | ``` 429 | butdr@ubuntu1804:~$ cron_backup.bash -v 430 | [ 19/12/2019 14:14:08 ] --- 431 | [ 19/12/2019 14:14:08 ] Checking OS... 432 | [ 19/12/2019 14:14:08 ] OS supported 433 | [ 19/12/2019 14:14:08 ] Start upload to Cloud... 434 | [ 19/12/2019 14:14:08 ] [googledrive] Syncing /home/butdr/backup2 to Cloud... 435 | [ 19/12/2019 14:14:44 ] [googledrive] [SYNC] Finish! All files and directories in /home/butdr/backup2 are synced to Cloud 436 | [ 19/12/2019 14:14:44 ] Finish! All files and directories are uploaded or synced to Cloud 437 | ``` 438 | 439 | Backup to cloud with TAR_BEFORE_UPLOAD=Yes 440 | ``` 441 | butdr@ubuntu1804:~$ cron_backup.bash -v 442 | [ 19/12/2019 14:15:18 ] --- 443 | [ 19/12/2019 14:15:18 ] Checking OS... 444 | [ 19/12/2019 14:15:18 ] OS supported 445 | [ 19/12/2019 14:15:18 ] Start upload to Cloud... 446 | [ 19/12/2019 14:15:18 ] [googledrive] Checking Google folder ID... 447 | [ 19/12/2019 14:15:19 ] [googledrive] [INFO] Check Google folder ID successful 448 | [ 19/12/2019 14:15:20 ] [googledrive] Directory 19_12_2019 does not exist. Creating... 449 | [ 19/12/2019 14:15:25 ] [googledrive] [CREATE] Created directory 19_12_2019 successful 450 | [ 19/12/2019 14:15:25 ] [googledrive] Compressing backup directory... 451 | [ 19/12/2019 14:15:25 ] [googledrive] [COMPRESS] Compress /home/butdr/backup2 successful 452 | [ 19/12/2019 14:15:25 ] [googledrive] Uploading backup2_NjkxNTQ4NTJlMDcw.tar.gz to directory 19_12_2019... 453 | [ 19/12/2019 14:15:28 ] [googledrive] [UPLOAD] Uploaded backup2_NjkxNTQ4NTJlMDcw.tar.gz to directory 19_12_2019 454 | [ 19/12/2019 14:15:28 ] [googledrive] Removing backup2_NjkxNTQ4NTJlMDcw.tar.gz after upload... 455 | [ 19/12/2019 14:15:28 ] [googledrive] [REMOVE] Remove backup2_NjkxNTQ4NTJlMDcw.tar.gz successful 456 | [ 19/12/2019 14:15:29 ] [googledrive] Directory 19_11_2019 does not exist. Nothing need remove! 457 | [ 19/12/2019 14:15:29 ] Finish! All files and directories are uploaded or synced to Cloud 458 | ``` 459 | -------------------------------------------------------------------------------- /butdr.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Setup variables 4 | GITHUB_LINK="https://raw.githubusercontent.com/mbrother2/butdr/master" 5 | BUTDR_WIKI="https://github.com/mbrother2/butdr/wiki" 6 | BIN_DIR="${HOME}/bin" 7 | CONF_DIR="${HOME}/.config" 8 | ACCT_DIR="${CONF_DIR}/accounts" 9 | BUTDR_CONF="${CONF_DIR}/butdr.conf" 10 | RCLONE_CONF="${HOME}/.config/rclone/rclone.conf" 11 | DF_BACKUP_DIR="${HOME}/backup" 12 | DF_TAR_BEFORE_UPLOAD="No" 13 | DF_SYNC_FILE="No" 14 | DF_LOG_FILE="${CONF_DIR}/butdr.log" 15 | DF_DAY_REMOVE="7" 16 | DF_DRIVE_FOLDER_ID="None" 17 | DF_FOLDER_NAME="None" 18 | DF_EMAIL_USER="None" 19 | DF_EMAIL_PASS="None" 20 | DF_EMAIL_TO="None" 21 | RCLONE_BIN="${BIN_DIR}/rclone" 22 | CRON_BACKUP="${BIN_DIR}/cron_backup.bash" 23 | SETUP_FILE="${BIN_DIR}/butdr.bash" 24 | CRON_TEMP="${CONF_DIR}/old_cron" 25 | FIRST_OPTION=$1 26 | SECOND_OPTION=$2 27 | 28 | # Date variables 29 | TODAY=`date +"%d_%m_%Y"` 30 | 31 | # Color variables 32 | GREEN='\e[32m' 33 | RED='\e[31m' 34 | YELLOW='\e[33m' 35 | REMOVE='\e[0m' 36 | 37 | # Detect error 38 | detect_error(){ 39 | if [ $? -ne 0 ] 40 | then 41 | show_write_log "`change_color red $2` $3. Exit" 42 | exit 1 43 | else 44 | show_write_log "$1" 45 | fi 46 | } 47 | 48 | # Change color of words 49 | change_color(){ 50 | case $1 in 51 | green) echo -e "${GREEN}$2${REMOVE}";; 52 | red) echo -e "${RED}$2${REMOVE}";; 53 | yellow) echo -e "${YELLOW}$2${REMOVE}";; 54 | *) echo "$2";; 55 | esac 56 | } 57 | 58 | # Check option 59 | check_option(){ 60 | # Check option is number 61 | if [ "$1" == number ] 62 | then 63 | if [ -z "$3" ] 64 | then 65 | echo "Please choose from 1 to $2" 66 | return 1 67 | else 68 | TRAP=`echo $2 | tr -d "[:digit:]"` 69 | if [ -z "${TRAP}" ] 70 | then 71 | if [[ $2 -lt 1 ]] || [[ $2 -gt $3 ]] 72 | then 73 | echo "Please choose from 1 to $3" 74 | return 1 75 | else 76 | return 0 77 | fi 78 | else 79 | echo "Please choose from 1 to $3" 80 | return 1 81 | fi 82 | fi 83 | fi 84 | # Check option in a list 85 | if [ "$1" == list ] 86 | then 87 | if [ -z $2 ] 88 | then 89 | echo -e "${RED}Missing command${REMOVE}" 90 | return 1 91 | fi 92 | VALUE=\\b$2\\b 93 | List=($3) 94 | if [[ ${List[*]} =~ ${VALUE} ]] 95 | then 96 | return 0 97 | else 98 | echo -e "${RED}No such command ${SECOND_OPTION}${REMOVE}" 99 | return 1 100 | fi 101 | fi 102 | } 103 | 104 | # Check MD5 of downloaded file 105 | check_md5sum(){ 106 | curl -s -o $2 ${GITHUB_LINK}/$1 107 | ORIGIN_MD5=`curl -s -H 'Cache-Control: no-cache' ${GITHUB_LINK}/MD5SUM | grep $1 | awk '{print $1}'` 108 | LOCAL_MD5=`md5sum $2 | awk '{print $1}'` 109 | if [ "${ORIGIN_MD5}" == "${LOCAL_MD5}" ] 110 | then 111 | show_write_log "Check md5sum for file $1 successful" 112 | else 113 | show_write_log "`change_color red [CHECKS][FAIL]` Can not verify md5 for file $1. Exit!" 114 | exit 1 115 | fi 116 | } 117 | 118 | # Check log file 119 | check_log_file(){ 120 | if [ ! -f ${BUTDR_CONF} ] 121 | then 122 | LOG_FILE=${DF_LOG_FILE} 123 | else 124 | LOG_FILE=`cat ${BUTDR_CONF} | grep "^LOG_FILE" | cut -d"=" -f2 | sed 's/"//g' | sed "s/'//g"` 125 | if [ "${LOG_FILE}" == "" ] 126 | then 127 | LOG_FILE=${DF_LOG_FILE} 128 | fi 129 | fi 130 | create_dir .config/rclone 131 | create_dir .config/accounts 132 | create_dir bin 133 | } 134 | 135 | # Write log 136 | show_write_log(){ 137 | echo "`date '+[ %d/%m/%Y %H:%M:%S ]'` $1" | tee -a ${LOG_FILE} 138 | } 139 | 140 | # Create necessary directory 141 | create_dir(){ 142 | if [ ! -d ${HOME}/$1 ] 143 | then 144 | mkdir -p ${HOME}/$1 145 | if [ ! -d ${HOME}/$1 ] 146 | then 147 | echo "Can not create directory ${HOME}/$1. Exit" 148 | exit 1 149 | else 150 | if [ "$1" == ".config/rclone" ] 151 | then 152 | show_write_log "---" 153 | show_write_log "Creating necessary directory..." 154 | fi 155 | show_write_log "Create directory ${HOME}/$1 successful" 156 | fi 157 | else 158 | if [ "$1" == ".config/rclone" ] 159 | then 160 | show_write_log "---" 161 | show_write_log "Creating necessary directory..." 162 | fi 163 | show_write_log "Directory ${HOME}/$1 existed. Skip" 164 | fi 165 | echo 1 >> ${HOME}/$1/test.txt 166 | detect_error "Check write to ${HOME}/$1 successful" "[CHECK][FAIL]" "Can not write to ${HOME}/$1" 167 | rm -f ${HOME}/$1/test.txt 168 | } 169 | 170 | # Check pre-install package 171 | check_package(){ 172 | which $1 >/dev/null 173 | if [ $? -ne 0 ] 174 | then 175 | show_write_log "Command $1 not found. Trying to install $1..." 176 | sleep 3 177 | ${INSTALL_CM} install -y $1 178 | which $1 >/dev/null 179 | if [ $? -ne 0 ] 180 | then 181 | show_write_log "Can not install $1 package. Please install $1 manually." 182 | exit 1 183 | fi 184 | detect_error "Package $1 is installed" "[CHECK][FAIL]" "Can not install $1 package. Please install $1 manually" 185 | else 186 | show_write_log "Package $1 is installed" 187 | fi 188 | } 189 | 190 | # Write config 191 | write_config(){ 192 | if [ "$4" == "" ] 193 | then 194 | VALUE=$3 195 | else 196 | VALUE=$4 197 | fi 198 | VAR=$2 199 | eval "$VAR"="$VALUE" 200 | if [ -f $1 ] 201 | then 202 | CHECK_VALUE=`cat $1 | grep -c "^$2="` 203 | if [ ${CHECK_VALUE} -eq 1 ] 204 | then 205 | VALUE=`echo "${VALUE}" | sed 's/\//\\\\\//g'` 206 | sed -i "s/$2=.*/$2=$VALUE/" $1 207 | else 208 | echo "$2=$VALUE" >> $1 209 | fi 210 | else 211 | echo "$2=$VALUE" >> $1 212 | fi 213 | } 214 | 215 | # Detect OS 216 | detect_os(){ 217 | show_write_log "Checking OS..." 218 | if [ -f /etc/os-release ] 219 | then 220 | OS=`cat /etc/os-release | grep "^NAME=" | cut -d'"' -f2 | awk '{print $1}' | cut -d"=" -f2` 221 | if [[ "${OS}" == "CentOS" ]] || [[ "${OS}" == "CloudLinux" ]] || [[ "${OS}" == "Oracle" ]] || [[ "${OS}" == "Fedora" ]] 222 | then 223 | INSTALL_CM="yum" 224 | elif [[ "${OS}" == "Ubuntu" ]] || [[ "${OS}" == "Debian" ]] 225 | then 226 | INSTALL_CM="apt" 227 | elif [[ "${OS}" == "openSUSE" ]] || [[ "${OS}" == "SLES" ]] 228 | then 229 | INSTALL_CM="zypper" 230 | else 231 | show_write_log "Sorry! We do not support your OS. Exit" 232 | exit 1 233 | fi 234 | else 235 | show_write_log "Sorry! We do not support your OS. Exit" 236 | exit 1 237 | fi 238 | show_write_log "OS supported" 239 | show_write_log "Checking necessary package..." 240 | check_package curl 241 | check_package unzip 242 | } 243 | 244 | # Check network 245 | check_network(){ 246 | show_write_log "Cheking network..." 247 | curl -sI raw.githubusercontent.com >/dev/null 248 | detect_error "Connect Github successful" "[CHECKS][FAIL]" "Can not connect to Github file, please check your network. Exit" 249 | if [ "${FIRST_OPTION}" != "--update" ] 250 | then 251 | curl -sI https://downloads.rclone.org >/dev/null 252 | detect_error "Connect rclone successful" "[CHECKS][FAIL]" "Can not connect to rclone file, please check your network. Exit" 253 | fi 254 | } 255 | 256 | # Download file from Github 257 | download_file(){ 258 | show_write_log "Downloading script cron_backup from github..." 259 | check_md5sum cron_backup.bash "${CRON_BACKUP}" 260 | show_write_log "Downloading script butdr from github..." 261 | check_md5sum butdr.bash "${SETUP_FILE}" 262 | chmod 755 ${SETUP_FILE} ${CRON_BACKUP} 263 | } 264 | 265 | # Download rclone 266 | download_rclone(){ 267 | show_write_log "Downloading rclone from homepage..." 268 | cd $HOME/bin 269 | curl -o rclone.zip https://downloads.rclone.org/v1.55.1/rclone-v1.55.1-linux-amd64.zip 270 | detect_error "Download rclone successful" "[DOWNLOAD][FAIL]" "Can not download rclone, please check your network. Exit" 271 | unzip -q rclone.zip -d rclone-butdr 272 | mv rclone-butdr/rclone-*-linux-amd64/rclone rclone 273 | rm -rf rclone.zip rclone-butdr 274 | } 275 | 276 | # Choose Cloud 277 | choose_cloud(){ 278 | echo "" 279 | echo "Which cloud you will use?" 280 | echo "1. Google Drive (drive)" 281 | echo "2. Dropbox (dropbox)" 282 | echo "3. Yandex (yandex)" 283 | echo "4. One Drive (onedrive)" 284 | echo "5. Backblaze (b2)" 285 | read -p " Your choice: " CHOOSE_CLOUD 286 | check_option number "${CHOOSE_CLOUD}" 5 287 | while [ $? -ne 0 ] 288 | do 289 | read -p " Your choice: " CHOOSE_CLOUD 290 | check_option number "${CHOOSE_CLOUD}" 5 291 | done 292 | case ${CHOOSE_CLOUD} in 293 | 1) CLOUD_TYPE="drive"; setup_drive ;; 294 | 2) CLOUD_TYPE="dropbox"; setup_dropbox ;; 295 | 3) CLOUD_TYPE="yandex"; setup_yandex ;; 296 | 4) CLOUD_TYPE="onedrive"; setup_onedrive ;; 297 | 5) CLOUD_TYPE="b2"; setup_backblaze ;; 298 | esac 299 | } 300 | 301 | # Setup Google drive account 302 | setup_drive(){ 303 | show_write_log "Creating Google drive account..." 304 | echo "" 305 | echo "Read more: ${BUTDR_WIKI}/Create-own-Google-credential-step-by-step" 306 | read -p " Your Google API client_id: " DRIVE_CLIENT_ID 307 | read -p " Your Google API client_secret: " DRIVE_CLIENT_SECRET 308 | echo "" 309 | echo "Read more ${BUTDR_WIKI}/Get-Google-folder-ID" 310 | read -p " Your Google folder ID: " DRIVE_FOLDER_ID 311 | rm -f ${RCLONE_CONF} 312 | if [[ -z ${DRIVE_CLIENT_ID} ]] || [[ -z ${DRIVE_CLIENT_SECRET} ]] 313 | then 314 | show_write_log "`change_color yellow [WARNING]` You're using rclone Google credential, it may be low performance" 315 | if [ -z ${DRIVE_FOLDER_ID} ] 316 | then 317 | ${RCLONE_BIN} config create drive drive config_is_local false scope drive 318 | else 319 | ${RCLONE_BIN} config create drive drive config_is_local false scope drive root_folder_id ${DRIVE_FOLDER_ID} 320 | fi 321 | else 322 | if [ -z ${DRIVE_FOLDER_ID} ] 323 | then 324 | ${RCLONE_BIN} config create drive drive config_is_local false scope drive client_id ${DRIVE_CLIENT_ID} client_secret ${DRIVE_CLIENT_SECRET} 325 | else 326 | ${RCLONE_BIN} config create drive drive config_is_local false scope drive client_id ${DRIVE_CLIENT_ID} client_secret ${DRIVE_CLIENT_SECRET} root_folder_id ${DRIVE_FOLDER_ID} 327 | fi 328 | fi 329 | show_write_log "Checking connect to Google Drive..." 330 | ${RCLONE_BIN} about drive: 331 | detect_error "Connect Google Drive successful" "[CONNECT][FAIL]" "Can not connect Google Drive with your credential, please check again" 332 | write_config "${ACCT_DIR}/drive.conf" DRIVE_FOLDER_ID "${DF_DRIVE_FOLDER_ID}" "${DRIVE_FOLDER_ID}" 333 | write_config "${ACCT_DIR}/drive.conf" CLOUD_TYPE "${CLOUD_TYPE}" 334 | write_config ${BUTDR_CONF} CLOUD_TYPE "${CLOUD_TYPE}" 335 | } 336 | 337 | # Setup Dropbox account 338 | setup_dropbox(){ 339 | show_write_log "Creating Dropbox account..." 340 | echo "" 341 | echo "Read more: ${BUTDR_WIKI}/Create-own-Dropbox-credential-step-by-step" 342 | read -p " Your Dropbox App key: " DROPBOX_APP_KEY 343 | read -p " Your Dropbox App secret: " DROPBOX_APP_SECRET 344 | read -p " Your Dropbox access token: " DROPBOX_ACCESS_TOKEN 345 | if [[ -z ${DROPBOX_APP_KEY} ]] || [[ -z ${DROPBOX_APP_SECRET} ]] || [[ -z ${DROPBOX_ACCESS_TOKEN} ]] 346 | then 347 | show_write_log "`change_color red [FAIL]` butdr only support rclone with your own Dropbox credential. Exit" 348 | exit 1 349 | fi 350 | read -p " Your Dropbox folder name: " DROPBOX_FOLDER_NAME 351 | rm -f ${RCLONE_CONF} 352 | cat >> "${RCLONE_CONF}" <> "${RCLONE_CONF}" <> "${RCLONE_CONF}" <> "${RCLONE_CONF}" <> ${HOME}/.profile 584 | echo "export PATH" >> ${HOME}/.profile 585 | source ${HOME}/.profile 586 | fi 587 | crontab -l > ${CRON_TEMP} 588 | CHECK_CRON=`cat ${CRON_TEMP} | grep -c "cron_backup.bash"` 589 | if [ ${CHECK_CRON} -eq 0 ] 590 | then 591 | echo "PATH=$PATH" >> ${CRON_TEMP} 592 | echo "0 0 * * * bash ${CRON_BACKUP} >/dev/null 2>&1" >> ${CRON_TEMP} 593 | crontab ${CRON_TEMP} 594 | if [ $? -ne 0 ] 595 | then 596 | show_write_log "Can not setup cronjob to backup! Please check again" 597 | SHOW_CRON="`change_color yellow [WARNING]` Can not setup cronjob to backup" 598 | else 599 | show_write_log "Setup cronjob to backup successful" 600 | SHOW_CRON="0 0 * * * bash ${CRON_BACKUP} >/dev/null 2>&1" 601 | fi 602 | else 603 | show_write_log "Cron backup existed. Skip" 604 | SHOW_CRON=`cat ${CRON_TEMP} | grep "cron_backup.bash"` 605 | fi 606 | rm -f ${CRON_TEMP} 607 | } 608 | 609 | # Reset account 610 | account_reset(){ 611 | CLOUD_TYPE=`cat ${BUTDR_CONF} | grep "^CLOUD_TYPE=" | cut -d"=" -f2` 612 | if [ ! -z "${CLOUD_TYPE}" ] 613 | then 614 | echo "" 615 | echo "Current account: ${CLOUD_TYPE}" 616 | echo "---" 617 | fi 618 | choose_cloud 619 | create_config 620 | } 621 | 622 | # Config backup file for single account 623 | config_backup_single(){ 624 | CLOUD_TYPE=`cat ${BUTDR_CONF} | grep "^CLOUD_TYPE=" | cut -d"=" -f2` 625 | echo "" 626 | echo "Current account: ${CLOUD_TYPE}" 627 | echo "---" 628 | echo "" 629 | show_write_log "Setting up backup config file..." 630 | create_config 631 | } 632 | 633 | # Show global config 634 | show_global_config(){ 635 | CLOUD_TYPE=`cat ${BUTDR_CONF} | grep "^CLOUD_TYPE=" | cut -d"=" -f2` 636 | show_write_log "|---" 637 | show_write_log "| Account type : ${CLOUD_TYPE}" 638 | show_write_log "| Your email : ${EMAIL_USER}" 639 | show_write_log "| Email password : ${EMAIL_PASS}" 640 | show_write_log "| Email notify : ${EMAIL_TO}" 641 | show_write_log "| Global config file : ${BUTDR_CONF}" 642 | } 643 | 644 | # Show backup config 645 | show_backup_config(){ 646 | CLOUD_TYPE=`cat ${BUTDR_CONF} | grep "^CLOUD_TYPE=" | cut -d"=" -f2` 647 | CURRENT_ACCOUNT=${CLOUD_TYPE} 648 | BACKUP_DIR=`cat ${ACCT_DIR}/${CURRENT_ACCOUNT}.conf | grep "^BACKUP_DIR=" | cut -d"=" -f2` 649 | DAY_REMOVE=`cat ${ACCT_DIR}/${CURRENT_ACCOUNT}.conf | grep "^DAY_REMOVE=" | cut -d"=" -f2` 650 | BACKUP_DIR=`cat ${ACCT_DIR}/${CURRENT_ACCOUNT}.conf | grep "^BACKUP_DIR=" | cut -d"=" -f2` 651 | if [ "${CLOUD_TYPE}" == "drive" ] 652 | then 653 | DRIVE_FOLDER_ID=`cat ${ACCT_DIR}/${CURRENT_ACCOUNT}.conf | grep "^DRIVE_FOLDER_ID=" | cut -d"=" -f2` 654 | else 655 | CLOUD_FOLDER_NAME=`cat ${ACCT_DIR}/${CURRENT_ACCOUNT}.conf | grep "^FOLDER_NAME=" | cut -d"=" -f2` 656 | fi 657 | SYNC_FILE=`cat ${ACCT_DIR}/${CURRENT_ACCOUNT}.conf | grep "^SYNC_FILE=" | cut -d"=" -f2` 658 | TAR_BEFORE_UPLOAD=`cat ${ACCT_DIR}/${CURRENT_ACCOUNT}.conf | grep "^TAR_BEFORE_UPLOAD=" | cut -d"=" -f2` 659 | show_write_log "|---" 660 | show_write_log "| Account : ${CURRENT_ACCOUNT}" 661 | show_write_log "| Backup dir : ${BACKUP_DIR}" 662 | show_write_log "| Keep backup : ${DAY_REMOVE} days" 663 | if [ "${CLOUD_TYPE}" == "drive" ] 664 | then 665 | show_write_log "| Google folder ID : ${DRIVE_FOLDER_ID}" 666 | else 667 | show_write_log "| Cloud folder name : ${CLOUD_FOLDER_NAME}" 668 | fi 669 | show_write_log "| Sync file : ${SYNC_FILE}" 670 | show_write_log "| Tar before upload : ${TAR_BEFORE_UPLOAD}" 671 | } 672 | 673 | # Show information 674 | show_info(){ 675 | echo "" 676 | show_write_log "+-----" 677 | show_write_log "| SUCESSFUL! Your informations:" 678 | if [ "${FIRST_OPTION}" == "--account" ] 679 | then 680 | show_backup_config 681 | show_write_log "+-----" 682 | elif [ "${FIRST_OPTION}" == "--config" ] 683 | then 684 | if [ "${SECOND_OPTION}" == "global" ] 685 | then 686 | show_global_config 687 | elif [ "${SECOND_OPTION}" == "show" ] 688 | then 689 | EMAIL_USER=`cat ${BUTDR_CONF} | grep "^EMAIL_USER=" | cut -d"=" -f2` 690 | EMAIL_PASS=`cat ${BUTDR_CONF} | grep "^EMAIL_PASS=" | cut -d"=" -f2` 691 | EMAIL_TO=`cat ${BUTDR_CONF} | grep "^EMAIL_TO=" | cut -d"=" -f2` 692 | show_global_config 693 | show_backup_config 694 | else 695 | show_backup_config 696 | fi 697 | show_write_log "+-----" 698 | else 699 | show_global_config 700 | show_backup_config 701 | show_write_log "+-----" 702 | echo "" 703 | if [[ "${OS}" == "Ubuntu" ]] || [[ "${OS}" == "Debian" ]] 704 | then 705 | echo "IMPORTANT: Please run command to use butdr: source ${HOME}/.profile " 706 | fi 707 | echo "If you get trouble when use butgg.bash please report here:" 708 | echo "https://github.com/mbrother2/butdr/issues" 709 | fi 710 | } 711 | 712 | # Show help 713 | _help(){ 714 | echo "butdr.bash - Backup to Cloud solution" 715 | echo "" 716 | echo "Usage: butdr.bash [option] [command]" 717 | echo "" 718 | echo "Options:" 719 | echo " --help show this help message and exit" 720 | echo " --setup setup or reset all scripts & config file" 721 | echo " --account reset account" 722 | echo " reset reset all accounts" 723 | echo " --config config global or backup file" 724 | echo " backup-single config backup file for single account" 725 | echo " global config global file" 726 | echo " show show all configs" 727 | echo " --update update butdr.bash & cron_backup.bash to latest version" 728 | echo " --uninstall remove all butdr scripts and ${HOME}.config directory" 729 | } 730 | 731 | # Setup or reset all scripts & config file 732 | _setup(){ 733 | rm -rf ${ACCT_DIR} ${BUTDR_CONF}/butgg.conf ${RCLONE_CONF} 734 | rm -rf ${RCLONE_BIN} ${CRON_BACKUP} ${SETUP_FILE} 735 | check_log_file 736 | detect_os 737 | check_network 738 | download_file 739 | download_rclone 740 | choose_cloud 741 | create_config 742 | config_global 743 | setup_cron 744 | show_info 745 | } 746 | 747 | # Reset account 748 | _account(){ 749 | List_option=(reset) 750 | check_option list "${SECOND_OPTION}" "${List_option[*]}" 751 | if [ $? -ne 0 ] 752 | then 753 | _help 754 | exit 1 755 | fi 756 | if [ "${FIRST_OPTION}" == "--account" ] 757 | then 758 | check_log_file 759 | fi 760 | case $1 in 761 | reset) account_reset ;; 762 | esac 763 | if [ "${FIRST_OPTION}" == "--account" ] 764 | then 765 | show_info 766 | fi 767 | } 768 | 769 | # Config global or backup file 770 | _config(){ 771 | List_option=(backup-single global show) 772 | check_option list "${SECOND_OPTION}" "${List_option[*]}" 773 | if [ $? -ne 0 ] 774 | then 775 | _help 776 | exit 1 777 | fi 778 | if [ "${FIRST_OPTION}" == "--config" ] 779 | then 780 | check_log_file 781 | fi 782 | case $1 in 783 | backup-single) config_backup_single ;; 784 | global) config_global ;; 785 | show) show_info ;; 786 | esac 787 | if [[ "${FIRST_OPTION}" == "--config" ]] && [[ "${SECOND_OPTION}" != "show" ]] 788 | then 789 | show_info 790 | fi 791 | } 792 | 793 | # Update butdr.bash & cron_backup.bash to latest version 794 | _update(){ 795 | check_log_file 796 | detect_os 797 | check_network 798 | download_file 799 | show_write_log "`change_color green [INFO]` Update butdr successful" 800 | } 801 | 802 | # Remove all butdr scripts and ${HOME}.config directory 803 | _uninstall(){ 804 | check_log_file 805 | show_write_log "Removing all butdr.bash scripts..." 806 | rm -f ${RCLONE_BIN} ${CRON_BACKUP} ${SETUP_FILE} 807 | detect_error "Remove all butdr.bash scripts successful" "[ERROR]" "Can not remove all butdr.bash scripts. Please check permission of these files" 808 | read -p " Do you want remove ${CONF_DIR} directory?(y/n) " REMOVE_RCLONE_DIR 809 | if [[ "${REMOVE_RCLONE_DIR}" == "y" ]] || [[ "${REMOVE_RCLONE_DIR}" == "Y" ]] 810 | then 811 | rm -rf ${CONF_DIR} 812 | if [ $? -eq 0 ] 813 | then 814 | echo "`date '+[ %d/%m/%Y %H:%M:%S ]' `Remove directory ${CONF_DIR} successful" 815 | else 816 | show_write_log "[ERROR] Can not remove directory ${CONF_DIR}. Please check permission of this directory" 817 | fi 818 | else 819 | show_write_log "Skip remove ${CONF_DIR} directory" 820 | fi 821 | } 822 | 823 | # Main functions 824 | if [ -z ${FIRST_OPTION} ] 825 | then 826 | echo -n "${RED}Missing option${REMOVE}" 827 | _help 828 | else 829 | case ${FIRST_OPTION} in 830 | --help) _help ;; 831 | --setup) _setup ;; 832 | --account) _account ${SECOND_OPTION};; 833 | --config) _config ${SECOND_OPTION};; 834 | --update) _update ;; 835 | --uninstall) _uninstall ;; 836 | *) echo "No such option ${FIRST_OPTION}"; _help ;; 837 | esac 838 | fi 839 | --------------------------------------------------------------------------------