From 6fa6022b8614e083383ffe5d4ddb5809e7c5292f Mon Sep 17 00:00:00 2001 From: Pe46dro Date: Sat, 23 Jun 2018 16:47:32 +0200 Subject: [PATCH 01/13] Start with new feature --- .travis.yml | 4 + README.MD | 51 +++++++---- script.sh | 251 +++++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 244 insertions(+), 62 deletions(-) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..a0da1b4 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,4 @@ +language: generic + +script: + - shellcheck script.sh diff --git a/README.MD b/README.MD index 6d95e99..74b4b36 100644 --- a/README.MD +++ b/README.MD @@ -1,31 +1,44 @@ -[Bash]FTP Backup Script -====================== -A small script to upload backup tar to an external FTP server +# [Bash]FTP Backup Script +[![Build Status](https://travis-ci.org/Pe46dro/Bash-FTP-Backup-Script.svg?branch=master)](https://travis-ci.org/Pe46dro/Bash-FTP-Backup-Script) [![Beerpay](https://img.shields.io/beerpay/hashdog/scrapfy-chrome-extension.svg)](https://beerpay.io/Pe46dro/Bash-FTP-Backup-Script) +A small script to backup a compressed folder to an external storage -How it works: -* You need to change "USERNAME,PASSWORD,SERVER" with information of your server -* You need to change "DIR" with absolute position of folder/file you want backup -* You need to change "FILE" with results of compression +### Prerequisites +Here you find the software that you need to install to have the function working +* cURL (Telegram notifications) +* mailx (Email notifications) +* Rclone (Rclone backup) +* Megatools (Mega.co.nz backup) +* sshpass (SFTP backup) + +## How it works: +* Clone the repository `git clone https://github.com/Pe46dro/Bash-FTP-Backup-Script.git` or download script.sh `wget https://github.com/Pe46dro/Bash-FTP-Backup-Script/blob/master/script.sh` +* Open script.sh with your favorite text editor `vim script.sh` +* Customize the configuration and save +* Run `bash script.sh` If you have some problem open an issues on GitHub -##FAQ; +## FAQ * Q: How can I make backups automatic? - - A: You can use crontab (on linux) [link](http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/) - - -* Q: Why can't I start the script? - - A: Make the file executable (chmod -x script.sh) - + A: Yes, you can use [crontab](http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/) * Q: Will this work on all linux distros? - - A: This script was tested on Ubuntu 12/14,Debian and Centos 6 and has worked whitout any problems + A: This script was tested on Ubuntu (12/14),Debian (8/9) and Centos 6 and has worked whitout any problems * Q: Why SFTP don't work? + A: You need to install "sshpass" ( apt-get install sshpass ) + +* Q: Can i backup my MySQL database? + A: Check my script for my [MySQL backup](https://github.com/Pe46dro/Bash-MySQL-Database-SFTP-FTP-Backup) + +# Contributors +| [](https://github.com/Pe46dro) | +|--| +| [Pe46dro](https://github.com/Pe46dro) | + + +## License - A: You need to install "sshpass" ( apt-get install sshpass ) \ No newline at end of file +This project is licensed under the GNU General Public License v3.0 License - see the [LICENSE.md](LICENSE.md) file for details \ No newline at end of file diff --git a/script.sh b/script.sh index 1a9ff4a..17fdaff 100644 --- a/script.sh +++ b/script.sh @@ -1,65 +1,230 @@ -#!/bin/sh +#!/usr/bin/env bash -# Linux FTP Backup Script -# Version: 1.0 -# Script by: Pietro Marangon -# Skype: pe46dro -# Email: pietro.marangon@gmail.com +# Linux Simple Backup Script -clean_backup() { - rm -f ./$FILE - echo 'Local Backup Removed' -} +# +# Configuration +# -######################## -# Edit Below This Line # -######################## +# Backup Information +FILE="BACKUP_NAME" #Filename of backup file to be transfered (string) +EXT="zip" # Possbile value: zip,tar (string) +DIR="/var/www" #Directory where thing to backup is located (string) +EXCLUSION=("/var/www/**/node_modules /var/www/**/vendor") #Directory to exclude (array space separeted) +CHECKSUM=true #Generate backup checksum (true|false) +ROTATION=30 #How many day keep (int|false) -# FTP Login Data -USERNAME="USERNAME HERE" -PASSWORD="PASSWORD HERE" -SERVER="IP HERE" -PORT="REMOTE SERVER PORT" +# Encryption Information +ENCRYPTION=false #Encryption (true|false) +ENCRYPTION_RANDOM=false #Random key generation (true|false) (Require notification enable) +ENCRYPTION_KEY="./my_super_secret_backup.key" #Encryption key (absolute path to file with key) -#Directory where thing to backup is located -DIR="/root" +#Notifications information +NOTIFICATION=false #Notifications (true|false) -#Remote directory where the backup will be placed -REMOTEDIR="./" +#Notifications Type +#1= Telegram Bot +#2= Email +NOTIFICATION_TYPE=(1) #Multiple notifications support (array space separeted) -#Filename of backup file to be transfered DON'T WRITE EXTENSION (.tar/.zip/ecc...) -FILE="BACKUP_NAME" +TELEGRAM_KEY="YOUR_TELEGRAM_BOT_KEY" #Multiple notifications support (string) +TELEGRAM_CHAT=(1) #Multiple recipient support (array space separeted) + +SMTP_IP="IP HERE" #SMTP Server IP (string) +SMTP_PORT=465 #SMTP Server Port (int) +SMTP_AUTH_USER="" #SMTP User (string) +SMTP_AUTH_PASSWORD="" #SMTP Password (string) +SMTP_FROM="my_backup_script@my_server" #From email (string) +SMTP_TO=("me@my_email") #Multiple recipient support (array space separeted) #Transfer type #1=FTP #2=SFTP -TYPE=1 +#3=Megatools +#4=Rclone +TYPE=(1 2 3 4) #Multiple destinations support (array space separeted) -############################## -# Don't Edit Below This Line # -############################## +# (S)FTP Login Data +USERNAME="USERNAME HERE" #Login username (string) +PASSWORD="PASSWORD HERE" #Login password (string) +SERVER="IP HERE" #Remote server address (string) +PORT="REMOTE SERVER PORT" #Remote server port (string) +REMOTEDIR="./" #Remote server port (string) -d=$(date --iso) +# Megatools Configuration +MEGATOOLS_PATH="/usr/local/bin" #Megatools binary folder (string) +MEGA_DESTIONATION="/Root/MY_BACKUP_FOLDER" #Mega folder (string) -FILE=$FILE"_"$d".tar.gz" -tar -czvf ./$FILE $DIR -echo 'Tar Complete' +# RClone Configuration +RCLONE_PATH="/usr/local/bin" #RClone binary folder (string) +RCLONE_REMOTE=("/Root/MY_BACKUP_FOLDER") #Rclone destinations (array space separeted) -if [ $TYPE -eq 1 ] -then -ftp -n -i $SERVER $PORT < Date: Sat, 23 Jun 2018 16:50:23 +0200 Subject: [PATCH 02/13] Readme fix --- README.MD | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.MD b/README.MD index 74b4b36..b420e88 100644 --- a/README.MD +++ b/README.MD @@ -1,6 +1,7 @@ # [Bash]FTP Backup Script [![Build Status](https://travis-ci.org/Pe46dro/Bash-FTP-Backup-Script.svg?branch=master)](https://travis-ci.org/Pe46dro/Bash-FTP-Backup-Script) [![Beerpay](https://img.shields.io/beerpay/hashdog/scrapfy-chrome-extension.svg)](https://beerpay.io/Pe46dro/Bash-FTP-Backup-Script) + A small script to backup a compressed folder to an external storage ### Prerequisites @@ -22,15 +23,19 @@ If you have some problem open an issues on GitHub ## FAQ * Q: How can I make backups automatic? + A: Yes, you can use [crontab](http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/) * Q: Will this work on all linux distros? + A: This script was tested on Ubuntu (12/14),Debian (8/9) and Centos 6 and has worked whitout any problems * Q: Why SFTP don't work? + A: You need to install "sshpass" ( apt-get install sshpass ) * Q: Can i backup my MySQL database? + A: Check my script for my [MySQL backup](https://github.com/Pe46dro/Bash-MySQL-Database-SFTP-FTP-Backup) # Contributors From 577d166a0a7995d0c2cec8894e1cd377be3f8d8b Mon Sep 17 00:00:00 2001 From: Pe46dro Date: Sat, 23 Jun 2018 16:52:04 +0200 Subject: [PATCH 03/13] Project rename [ci skip] --- README.MD | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.MD b/README.MD index b420e88..789c79d 100644 --- a/README.MD +++ b/README.MD @@ -1,5 +1,5 @@ -# [Bash]FTP Backup Script +# [Bash] Backup Script [![Build Status](https://travis-ci.org/Pe46dro/Bash-FTP-Backup-Script.svg?branch=master)](https://travis-ci.org/Pe46dro/Bash-FTP-Backup-Script) [![Beerpay](https://img.shields.io/beerpay/hashdog/scrapfy-chrome-extension.svg)](https://beerpay.io/Pe46dro/Bash-FTP-Backup-Script) A small script to backup a compressed folder to an external storage From 8d621315f86aee57d773eaee19dbb0672c9bd315 Mon Sep 17 00:00:00 2001 From: Pe46dro Date: Sun, 8 Jul 2018 21:11:01 +0200 Subject: [PATCH 04/13] Added readme spell check test --- .travis.yml | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index a0da1b4..388e1b2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,13 @@ -language: generic - -script: - - shellcheck script.sh +language: node_js +node_js: + - "lts/*" +cache: + directories: + - "node_modules" +before_install: + - npm i -g markdown-spellcheck write-good +jobs: + include: + - script: 'mdspell --ignore-acronyms --en-us --report README.md' + - script: 'write-good --yes-eprime --parse README.md || true' + - script: 'shellcheck script.sh' \ No newline at end of file From 23e4126fdbc5ba59c0eecf05a4eef3a0132972c0 Mon Sep 17 00:00:00 2001 From: Pe46dro Date: Sun, 8 Jul 2018 21:11:30 +0200 Subject: [PATCH 05/13] More bugs :P --- script.sh | 156 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 118 insertions(+), 38 deletions(-) diff --git a/script.sh b/script.sh index 17fdaff..7447227 100644 --- a/script.sh +++ b/script.sh @@ -13,8 +13,10 @@ DIR="/var/www" #Directory where thing to backup is located (string) EXCLUSION=("/var/www/**/node_modules /var/www/**/vendor") #Directory to exclude (array space separeted) CHECKSUM=true #Generate backup checksum (true|false) ROTATION=30 #How many day keep (int|false) +LOG_FILE="/var/log/bck_script.log" # Log file location (string) +TMP_FOLDER="/tmp/" #Temp folder for archive generation -# Encryption Information +# Encryption Information (only zip) ENCRYPTION=false #Encryption (true|false) ENCRYPTION_RANDOM=false #Random key generation (true|false) (Require notification enable) ENCRYPTION_KEY="./my_super_secret_backup.key" #Encryption key (absolute path to file with key) @@ -63,44 +65,96 @@ RCLONE_REMOTE=("/Root/MY_BACKUP_FOLDER") #Rclone destinations (array space separ BEFORE_COMMAND=false #Command to run before backup start (string|false) AFTER_COMMAND=false #Command to run after backup finish (string|false) - # # Functions # -check_configuration() { #TODO - +check_configuration() { + if [ "$ENCRYPTION" = true ] && [ "$ENCRYPTION_RANDOM" = false ] + then + if [ ! -f "$ENCRYPTION_KEY" ] + then + echo "Key file not found!" | tee -a --output-error=warn "$LOG_FILE" + exit 0 + fi + + if [ "$(stat -c %A "$ENCRYPTION_KEY")" != "-rw-------" ] + then + echo "WARNING: UNPROTECTED PRIVATE KEY FILE!" | tee -a --output-error=warn "$LOG_FILE" + echo "Permissions for '$ENCRYPTION_KEY' are too open. It is recommended that your private key files are NOT accessible by others." | tee -a --output-error=warn "$LOG_FILE" + exit 0 + fi + + fi } -generate_file_name() { #TODO +generate_file_name() { d=$(date '+%Y-%m-%d_%H') - FILE="$FILE""_$d.tar.gz" + FILE="$FILE""_$d.$EXT" + + if [ "$ROTATION" != false ] + then + d=$(date --date="-$ROTATION day" '+%Y-%m-%d_%H') + RFILE="$FILE""_$d.$EXT" + fi + } generate_key(){ - $KEY=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1) + KEY=$(tr -dc 'a-zA-Z0-9' < /dev/urandom | fold -w 32 | head -n 1) } -read_key(){ #TODO - +read_key(){ + KEY=$(cat "$ENCRYPTION_KEY") } -generate_backup_command(){ - tar -czvf "./$FILE" "$DIR" - echo 'Tar Complete' +generate_backup(){ + + if [ "$EXT" = "tar" ] + then + for EXL in "${EXCLUSION[@]}" + do + PARAMS="$PARAMS --exclude='$EXL' " + done + + tar -czvf "$TMP_FOLDER$FILE" "$DIR" | tee -a "$LOG_FILE" + echo 'Tar Complete' | tee -a "$LOG_FILE" + elif [ "$EXT" = "zip" ] + then + + if [ "$ENCRYPTION" = true ] + then + PARAMS="--password $KEY " + fi + + for EXL in "${EXCLUSION[@]}" + do + PARAMS="$PARAMS --exclude '$EXL'" + done + + zip "$PARAMS" -r "$TMP_FOLDER$FILE" "$DIR" + echo 'Zip Complete' | tee -a "$LOG_FILE" + fi + } ftp_upload(){ ftp -n -i "$SERVER" "$PORT" < Date: Tue, 13 Aug 2019 21:24:37 +0200 Subject: [PATCH 06/13] Update script.sh --- script.sh | 35 +++++++++++------------------------ 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/script.sh b/script.sh index 7447227..04525c1 100644 --- a/script.sh +++ b/script.sh @@ -42,9 +42,8 @@ SMTP_TO=("me@my_email") #Multiple recipient support (array space separeted) #Transfer type #1=FTP #2=SFTP -#3=Megatools -#4=Rclone -TYPE=(1 2 3 4) #Multiple destinations support (array space separeted) +#3=Rclone +TYPE=(1 2 3) #Multiple destinations support (array space separeted) # (S)FTP Login Data USERNAME="USERNAME HERE" #Login username (string) @@ -81,7 +80,7 @@ check_configuration() { if [ "$(stat -c %A "$ENCRYPTION_KEY")" != "-rw-------" ] then echo "WARNING: UNPROTECTED PRIVATE KEY FILE!" | tee -a --output-error=warn "$LOG_FILE" - echo "Permissions for '$ENCRYPTION_KEY' are too open. It is recommended that your private key files are NOT accessible by others." | tee -a --output-error=warn "$LOG_FILE" + echo "Permissions for '$ENCRYPTION_KEY' are too open. It is recommended that your private key files are NOT accessible by others." | tee -a --output-error=warn "$LOG_FILE" exit 0 fi @@ -162,24 +161,19 @@ sftp_upload(){ } sftp_rotation(){ #TODO - ssh "$USERNAME"@"$SERVER" 'rm "$REMOTEDIR$RFILE"' + ssh "$USERNAME"@"$SERVER" 'find $REMOTEDIR -type f -mtime +$ROTATION -exec rm {} \;' true; } -mega_upload(){ - "$MEGATOOLS_PATH"/megaput --path "$MEGA_DESTIONATION" "$TMP_FOLDER$FILE" -} - -mega_rotation(){ - "$MEGATOOLS_PATH"/megarm "$MEGA_DESTIONATION$RFILE" +rclone_upload () { + # $1 RClone remote + "$RCLONE_PATH"/rclone copy "$TMP_FOLDER$FILE" "$1" + } -rclone_upload () { #TODO - true; -} - -rclone_rotation () { #TODO - true; +rclone_rotation () { + # $1 RClone remote + "$RCLONE_PATH"/rclone delete "$1" --min-age $ROTATIONd } generate_checksum(){ @@ -252,13 +246,6 @@ do sftp_rotation fi elif [ "$operation" -eq 3 ] - then - mega_upload - if [ "$ROTATION" != false ] - then - mega_rotation - fi - elif [ "$operation" -eq 4 ] then for rclone_destination in "${RCLONE_REMOTE[@]}" do From 5d2db78842ad3a21ee356519d794dcbdeb8e36ef Mon Sep 17 00:00:00 2001 From: Pietro Marangon Date: Tue, 13 Aug 2019 21:27:56 +0200 Subject: [PATCH 07/13] Update script.sh --- script.sh | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/script.sh b/script.sh index 04525c1..30deba0 100644 --- a/script.sh +++ b/script.sh @@ -52,10 +52,6 @@ SERVER="IP HERE" #Remote server address (string) PORT="REMOTE SERVER PORT" #Remote server port (string) REMOTEDIR="./" #Remote server port (string) -# Megatools Configuration -MEGATOOLS_PATH="/usr/local/bin" #Megatools binary folder (string) -MEGA_DESTIONATION="/Root/MY_BACKUP_FOLDER" #Mega folder (string) - # RClone Configuration RCLONE_PATH="/usr/local/bin" #RClone binary folder (string) RCLONE_REMOTE=("/Root/MY_BACKUP_FOLDER") #Rclone destinations (array space separeted) @@ -173,7 +169,7 @@ rclone_upload () { rclone_rotation () { # $1 RClone remote - "$RCLONE_PATH"/rclone delete "$1" --min-age $ROTATIONd + "$RCLONE_PATH"/rclone delete "$1" --min-age "$ROTATION"d } generate_checksum(){ @@ -252,7 +248,7 @@ do rclone_upload "$rclone_destination" if [ "$ROTATION" != false ] then - rclone_rotation + rclone_rotation "$rclone_destination" fi done else From 3399afd0f9bc9b51f530bb5b44aac1cad005fef9 Mon Sep 17 00:00:00 2001 From: Pietro Marangon Date: Tue, 13 Aug 2019 22:27:59 +0200 Subject: [PATCH 08/13] Update script.sh --- script.sh | 102 +++++++++++++++++++++++++++--------------------------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/script.sh b/script.sh index 30deba0..8a89a27 100644 --- a/script.sh +++ b/script.sh @@ -41,11 +41,10 @@ SMTP_TO=("me@my_email") #Multiple recipient support (array space separeted) #Transfer type #1=FTP -#2=SFTP -#3=Rclone -TYPE=(1 2 3) #Multiple destinations support (array space separeted) +#2=Rclone +TYPE=(1 2) #Multiple destinations support (array space separeted) -# (S)FTP Login Data +# FTP(s) Login Data USERNAME="USERNAME HERE" #Login username (string) PASSWORD="PASSWORD HERE" #Login password (string) SERVER="IP HERE" #Remote server address (string) @@ -84,15 +83,13 @@ check_configuration() { } generate_file_name() { - d=$(date '+%Y-%m-%d_%H') - FILE="$FILE""_$d.$EXT" - if [ "$ROTATION" != false ] then d=$(date --date="-$ROTATION day" '+%Y-%m-%d_%H') RFILE="$FILE""_$d.$EXT" fi - + d=$(date '+%Y-%m-%d_%H') + FILE="$FILE""_$d.$EXT" } generate_key(){ @@ -105,32 +102,37 @@ read_key(){ generate_backup(){ - if [ "$EXT" = "tar" ] - then - for EXL in "${EXCLUSION[@]}" - do - PARAMS="$PARAMS --exclude='$EXL' " - done - - tar -czvf "$TMP_FOLDER$FILE" "$DIR" | tee -a "$LOG_FILE" - echo 'Tar Complete' | tee -a "$LOG_FILE" - elif [ "$EXT" = "zip" ] - then - - if [ "$ENCRYPTION" = true ] - then - PARAMS="--password $KEY " - fi - - for EXL in "${EXCLUSION[@]}" - do - PARAMS="$PARAMS --exclude '$EXL'" - done - - zip "$PARAMS" -r "$TMP_FOLDER$FILE" "$DIR" - echo 'Zip Complete' | tee -a "$LOG_FILE" - fi - + if [ "$EXT" = "tar" ] + then + for EXL in "${EXCLUSION[@]}" + do + PARAMS="$PARAMS --exclude='$EXL' " + done + + tar -czvf "$TMP_FOLDER$FILE" "$DIR" | tee -a "$LOG_FILE" + echo 'Tar Complete' | tee -a "$LOG_FILE" + elif [ "$EXT" = "zip" ] + then + + if [ "$ENCRYPTION" = true ] + then + PARAMS="-P $KEY" + fi + + for EXL in "${EXCLUSION[@]}" + do + echo "Excluding '$EXL'" + PARAMS="$PARAMS --exclude '$EXL'" + done + + PARAMS="$PARAMS -r $TMP_FOLDER$FILE $DIR" + + local CMD="zip $PARAMS" + + eval $CMD | tee -a "$LOG_FILE" + echo 'Zip Complete' | tee -a "$LOG_FILE" + fi + } ftp_upload(){ @@ -152,15 +154,6 @@ ftp_rotation(){ EOF } -sftp_upload(){ - rsync --rsh="sshpass -p \"$PASSWORD\" ssh -p \"$PORT\" -o StrictHostKeyChecking=no -l \"$USERNAME\"" "$FILE" "$SERVER":"$REMOTEDIR" -} - -sftp_rotation(){ #TODO - ssh "$USERNAME"@"$SERVER" 'find $REMOTEDIR -type f -mtime +$ROTATION -exec rm {} \;' - true; -} - rclone_upload () { # $1 RClone remote "$RCLONE_PATH"/rclone copy "$TMP_FOLDER$FILE" "$1" @@ -173,12 +166,18 @@ rclone_rotation () { } generate_checksum(){ + echo "MD5 HASH" md5sum "$TMP_FOLDER$FILE" | tee -a "$LOG_FILE" } telegram_notification(){ # $1 Chat id - curl -s -X POST https://api.telegram.org/bot"$TELEGRAM_KEY"/sendMessage -F document=@"$LOG_FILE" -F caption="Backup log" -d chat_id="$1" + curl -F chat_id="$1" -F document=@"$LOG_FILE" https://api.telegram.org/bot"$TELEGRAM_KEY"/sendDocument + + if [ "$ENCRYPTION" = true ] && [ "$ENCRYPTION_RANDOM" = true ] + then + curl -s -X POST https://api.telegram.org/bot"$TELEGRAM_KEY"/sendMessage -d chat_id="$1" -d text="Key: $KEY" + fi } smtp_notification(){ @@ -207,6 +206,10 @@ clean_backup() { check_configuration +echo "###########################################" | tee -a "$LOG_FILE" +echo "Starting backup $FILE" | tee -a "$LOG_FILE" +echo "###########################################" | tee -a "$LOG_FILE" + if [ "$BEFORE_COMMAND" != false ] then # shellcheck disable=SC2091 @@ -235,13 +238,6 @@ do ftp_rotation fi elif [ "$operation" -eq 2 ] - then - sftp_upload - if [ "$ROTATION" != false ] - then - sftp_rotation - fi - elif [ "$operation" -eq 3 ] then for rclone_destination in "${RCLONE_REMOTE[@]}" do @@ -290,4 +286,8 @@ then $($AFTER_COMMAND) fi +echo "###########################################" | tee -a "$LOG_FILE" +echo "Ending backup $FILE" | tee -a "$LOG_FILE" +echo "###########################################" | tee -a "$LOG_FILE" + #END From 0b8be1c9fe5ac99f969e9d9cf32e52a42f27478c Mon Sep 17 00:00:00 2001 From: Pietro Marangon Date: Tue, 13 Aug 2019 22:29:24 +0200 Subject: [PATCH 09/13] Update .travis.yml --- .travis.yml | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index 388e1b2..eba84eb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,13 +1,4 @@ -language: node_js -node_js: - - "lts/*" -cache: - directories: - - "node_modules" -before_install: - - npm i -g markdown-spellcheck write-good +language: generic jobs: include: - - script: 'mdspell --ignore-acronyms --en-us --report README.md' - - script: 'write-good --yes-eprime --parse README.md || true' - - script: 'shellcheck script.sh' \ No newline at end of file + - script: 'shellcheck script.sh' From c45b729d283ddf4ca8f130d17de11a64db3e4d00 Mon Sep 17 00:00:00 2001 From: Pietro Marangon Date: Tue, 13 Aug 2019 22:30:06 +0200 Subject: [PATCH 10/13] Update README.MD --- README.MD | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/README.MD b/README.MD index 789c79d..33f11c2 100644 --- a/README.MD +++ b/README.MD @@ -9,8 +9,6 @@ Here you find the software that you need to install to have the function working * cURL (Telegram notifications) * mailx (Email notifications) * Rclone (Rclone backup) -* Megatools (Mega.co.nz backup) -* sshpass (SFTP backup) ## How it works: * Clone the repository `git clone https://github.com/Pe46dro/Bash-FTP-Backup-Script.git` or download script.sh `wget https://github.com/Pe46dro/Bash-FTP-Backup-Script/blob/master/script.sh` @@ -25,19 +23,7 @@ If you have some problem open an issues on GitHub * Q: How can I make backups automatic? A: Yes, you can use [crontab](http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/) - -* Q: Will this work on all linux distros? - - A: This script was tested on Ubuntu (12/14),Debian (8/9) and Centos 6 and has worked whitout any problems - -* Q: Why SFTP don't work? - - A: You need to install "sshpass" ( apt-get install sshpass ) -* Q: Can i backup my MySQL database? - - A: Check my script for my [MySQL backup](https://github.com/Pe46dro/Bash-MySQL-Database-SFTP-FTP-Backup) - # Contributors | [](https://github.com/Pe46dro) | |--| @@ -46,4 +32,4 @@ If you have some problem open an issues on GitHub ## License -This project is licensed under the GNU General Public License v3.0 License - see the [LICENSE.md](LICENSE.md) file for details \ No newline at end of file +This project is licensed under the GNU General Public License v3.0 License - see the [LICENSE.md](LICENSE.md) file for details From 56652e0ca23aa0fb15dc55225584da77df96db51 Mon Sep 17 00:00:00 2001 From: Pietro Marangon Date: Tue, 13 Aug 2019 22:31:02 +0200 Subject: [PATCH 11/13] Update script.sh --- script.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script.sh b/script.sh index 8a89a27..b3ca352 100644 --- a/script.sh +++ b/script.sh @@ -129,7 +129,7 @@ generate_backup(){ local CMD="zip $PARAMS" - eval $CMD | tee -a "$LOG_FILE" + eval "$CMD" | tee -a "$LOG_FILE" echo 'Zip Complete' | tee -a "$LOG_FILE" fi From c58976391f11a63f569e003865ec8732f6586f20 Mon Sep 17 00:00:00 2001 From: Pietro Marangon Date: Tue, 13 Aug 2019 22:36:00 +0200 Subject: [PATCH 12/13] Update script.sh --- script.sh | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/script.sh b/script.sh index b3ca352..a64baf7 100644 --- a/script.sh +++ b/script.sh @@ -8,12 +8,12 @@ # Backup Information FILE="BACKUP_NAME" #Filename of backup file to be transfered (string) -EXT="zip" # Possbile value: zip,tar (string) +EXT="zip" #Possbile value: zip,tar (string) DIR="/var/www" #Directory where thing to backup is located (string) EXCLUSION=("/var/www/**/node_modules /var/www/**/vendor") #Directory to exclude (array space separeted) CHECKSUM=true #Generate backup checksum (true|false) -ROTATION=30 #How many day keep (int|false) -LOG_FILE="/var/log/bck_script.log" # Log file location (string) +ROTATION=30 #How many day keep for better rotation use RClone (int|false) +LOG_FILE="/var/log/bck_script.log" # Log file location (string|/dev/null) TMP_FOLDER="/tmp/" #Temp folder for archive generation # Encryption Information (only zip) @@ -23,6 +23,7 @@ ENCRYPTION_KEY="./my_super_secret_backup.key" #Encryption key (absolute path to #Notifications information NOTIFICATION=false #Notifications (true|false) +SEND_LOG=false #Send log file (true|false) #Notifications Type #1= Telegram Bot @@ -114,7 +115,7 @@ generate_backup(){ elif [ "$EXT" = "zip" ] then - if [ "$ENCRYPTION" = true ] + if [ "$ENCRYPTION" = true ] && [ "$NOTIFICATION" = true ] then PARAMS="-P $KEY" fi @@ -172,8 +173,11 @@ generate_checksum(){ telegram_notification(){ # $1 Chat id + if [ "$SEND_LOG" = true ] + then curl -F chat_id="$1" -F document=@"$LOG_FILE" https://api.telegram.org/bot"$TELEGRAM_KEY"/sendDocument - + fi + if [ "$ENCRYPTION" = true ] && [ "$ENCRYPTION_RANDOM" = true ] then curl -s -X POST https://api.telegram.org/bot"$TELEGRAM_KEY"/sendMessage -d chat_id="$1" -d text="Key: $KEY" From fcfb351235040e7bd4844222cf25b6077f180cab Mon Sep 17 00:00:00 2001 From: Pietro Marangon Date: Tue, 13 Aug 2019 23:15:27 +0200 Subject: [PATCH 13/13] Update script.sh --- script.sh | 48 ++++++++++++------------------------------------ 1 file changed, 12 insertions(+), 36 deletions(-) diff --git a/script.sh b/script.sh index a64baf7..7cd0bd5 100644 --- a/script.sh +++ b/script.sh @@ -10,13 +10,13 @@ FILE="BACKUP_NAME" #Filename of backup file to be transfered (string) EXT="zip" #Possbile value: zip,tar (string) DIR="/var/www" #Directory where thing to backup is located (string) -EXCLUSION=("/var/www/**/node_modules /var/www/**/vendor") #Directory to exclude (array space separeted) +EXCLUSION=("/var/www/**/node_modules" "/var/www/**/vendor") #Directory to exclude (array space separeted) CHECKSUM=true #Generate backup checksum (true|false) ROTATION=30 #How many day keep for better rotation use RClone (int|false) -LOG_FILE="/var/log/bck_script.log" # Log file location (string|/dev/null) +LOG_FILE="/dev/null" #Log file location (string|/dev/null) TMP_FOLDER="/tmp/" #Temp folder for archive generation -# Encryption Information (only zip) +#Encryption Information (only zip) ENCRYPTION=false #Encryption (true|false) ENCRYPTION_RANDOM=false #Random key generation (true|false) (Require notification enable) ENCRYPTION_KEY="./my_super_secret_backup.key" #Encryption key (absolute path to file with key) @@ -27,22 +27,15 @@ SEND_LOG=false #Send log file (true|false) #Notifications Type #1= Telegram Bot -#2= Email +#2= FUTURE USE NOTIFICATION_TYPE=(1) #Multiple notifications support (array space separeted) TELEGRAM_KEY="YOUR_TELEGRAM_BOT_KEY" #Multiple notifications support (string) TELEGRAM_CHAT=(1) #Multiple recipient support (array space separeted) -SMTP_IP="IP HERE" #SMTP Server IP (string) -SMTP_PORT=465 #SMTP Server Port (int) -SMTP_AUTH_USER="" #SMTP User (string) -SMTP_AUTH_PASSWORD="" #SMTP Password (string) -SMTP_FROM="my_backup_script@my_server" #From email (string) -SMTP_TO=("me@my_email") #Multiple recipient support (array space separeted) - #Transfer type #1=FTP -#2=Rclone +#2=Rclone https://rclone.org/ TYPE=(1 2) #Multiple destinations support (array space separeted) # FTP(s) Login Data @@ -54,7 +47,7 @@ REMOTEDIR="./" #Remote server port (string) # RClone Configuration RCLONE_PATH="/usr/local/bin" #RClone binary folder (string) -RCLONE_REMOTE=("/Root/MY_BACKUP_FOLDER") #Rclone destinations (array space separeted) +RCLONE_REMOTE=("DEST1:/" "DEST2:/") #Rclone destinations (array space separeted) # Task Configuration BEFORE_COMMAND=false #Command to run before backup start (string|false) @@ -70,17 +63,21 @@ check_configuration() { if [ ! -f "$ENCRYPTION_KEY" ] then echo "Key file not found!" | tee -a --output-error=warn "$LOG_FILE" - exit 0 + exit 1 fi if [ "$(stat -c %A "$ENCRYPTION_KEY")" != "-rw-------" ] then echo "WARNING: UNPROTECTED PRIVATE KEY FILE!" | tee -a --output-error=warn "$LOG_FILE" echo "Permissions for '$ENCRYPTION_KEY' are too open. It is recommended that your private key files are NOT accessible by others." | tee -a --output-error=warn "$LOG_FILE" - exit 0 + exit 1 fi fi + if [ "$ENCRYPTION" = true ] && [ "$ENCRYPTION_RANDOM" = true ] && [ "$NOTIFICATION" = false ] + then + echo "Random key require notification" | tee -a --output-error=warn "$LOG_FILE" + fi } generate_file_name() { @@ -184,21 +181,6 @@ telegram_notification(){ fi } -smtp_notification(){ - # $1 Email address - mailx -v -s "Backup completed" \ - -S smtp-use-starttls \ - -S ssl-verify=ignore \ - -S smtp-auth=login \ - -S smtp=smtp://"$SMTP_IP":"$SMTP_PORT" \ - -S from="$SMTP_FROM" \ - -S smtp-auth-user="$SMTP_AUTH_USER" \ - -S smtp-auth-password="$SMTP_AUTH_PASSWORD" \ - -S ssl-verify=ignore \ - -A "$LOG_FILE" - "$1" | tee -a "$LOG_FILE" -} - clean_backup() { rm -f "$TMP_FOLDER$FILE" echo 'Local Backup Removed' | tee -a "$LOG_FILE" @@ -271,12 +253,6 @@ then do telegram_notification "$chat_id" done - elif [ "$notification_operation" -eq 2 ] - then - for email_add in "${SMTP_TO[@]}" - do - smtp_notification "$email_add" - done fi done fi