diff --git a/.gitignore b/.gitignore index 0cd2367..1734a1e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ databases folders config.sh +dropboxpath diff --git a/README.md b/README.md index 8830cc7..e0af754 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ Make sure the run file is executable $ chmod +x run.sh ``` -## Automated Option (guided setup option) +## Auto Setup Option Just run :) @@ -35,7 +35,7 @@ Just run :) $ ./run.sh ``` -## Manual Option +## Manual Setup Option Copy __config.txt__ to __config.sh__ and update the values in the file. @@ -61,6 +61,14 @@ Run the script $ ./run.sh ``` +## Fetch Backup and Restore + +Either revert to previous backup (restore) or to do a new deployment. + +```bash + $ ./run.sh -r +``` + ## Tested Environments * GNU Linux diff --git a/config.txt b/config.txt index 7b46d7d..b32a08d 100644 --- a/config.txt +++ b/config.txt @@ -18,11 +18,11 @@ REMOTESSH="user@yourserver.com" DROPBOX="/home/path/to/Dropbox-Uploader/dropbox_uploader.sh" # PATH DETAILS -REMOTEDBPATH="your_db_path/" -REMOTEWEBPATH="your_website_path/" +REMOTEDBPATH="db_path/" +REMOTEWEBPATH="website_path/" ## WEBSITE BACKUP TYPE (1 = PER/FILE || 2 = ZIPPED) -WEBBACKUPTYPE=1 +WEBBACKUPTYPE=2 ## 0 = no date | 1 = year | 2 = year-month | 3 = your-month-day | 4 = your-month-day:time # For DB file name -USEDATE=0 +USEDATE=1 diff --git a/incl.sh b/incl.sh index 887b23e..354c0cc 100644 --- a/incl.sh +++ b/incl.sh @@ -101,8 +101,12 @@ function moveWEB () { else FILE="$2.zip" fi + # go to local folder + cd "${localFolder}" # zip the website - zip -r -q "${FILE}" "${localFolder}" + zip -r -q "${tmpFolder}/${FILE}" * + # we move to tmp folder + cd "$tmpFolder" # set the paths PaTh="${FILE}" remotePaTh="${REMOTEWEBPATH}${FILE}" diff --git a/main.sh b/main.sh index 3fd5f4e..14fc81a 100644 --- a/main.sh +++ b/main.sh @@ -33,10 +33,20 @@ MOVEWEBWHAT='None' ### MAIN ### function main () { - # backup the databases now - backupDB - # backup the websites now - backupWEB + # do backup + if [ "$REVERT" -ne 1 ]; then + # backup the databases now + backupDB + # backup the websites now + backupWEB + else + # revert the databases now + revertDB + # revert the websites now + revertWEB + # force remove tmp + rmTmp 4 + fi } function rmTmp () { @@ -47,10 +57,13 @@ function rmTmp () { elif [ "$1" -eq "2" ]; then #confirm we are done BACKUPWEBDONE=1 + elif [ "$1" -eq "4" ]; then + # now force remove tmp + rmFolder "$tmpFolder" fi # only if both are done if [ "$BACKUPWEBDONE" -eq "1" ] && [ "$BACKUPDBDONE" -eq "1" ]; then - # now remove the local file + # now remove tmp rmFolder "$tmpFolder" fi } @@ -100,6 +113,26 @@ function backupWEB () { rmTmp 2 } +# function to revert DB's +function revertDB () { + while IFS=$'\t' read -r -a database + do + [[ "$database" =~ ^#.*$ ]] && continue + # the local database details + # echo "${database[0]}" "${database[1]}" "${database[2]}" "${database[3]}" "${database[4]}" + done < $databaseBuilder +} + +# function to revert WEB folders +function revertWEB () { + while IFS=$'\t' read -r -a foalder + do + [[ "$foalder" =~ ^#.*$ ]] && continue + # the local folder & remote file name + # echo "${foalder[0]}" "${foalder[1]}" + done < $folderBuilder +} + # run the main only at the end! main diff --git a/run.sh b/run.sh index 7c13b5d..e9098f3 100755 --- a/run.sh +++ b/run.sh @@ -21,6 +21,8 @@ # user home dir USERHOME=~/ +# switch to revert +REVERT=0 # get script path DIR="${BASH_SOURCE%/*}" if [[ ! -d "$DIR" || "$DIR" == '.' ]]; then DIR="$PWD"; fi @@ -54,7 +56,7 @@ tmpFolder="${USERHOME}T3MPR3P0_${newFolder}" # create tmp folder if [ ! -d "$tmpFolder" ] then - mkdir -p "$tmpFolder" + mkdir -p "$tmpFolder" fi # DB file @@ -78,5 +80,16 @@ fi # we move to user folder cd "$USERHOME" +#Look for optional config parameter +while getopts ":r" opt; do + case $opt in + + r) + REVERT=1 + ;; + + esac +done + # run main . "$BASEDIR/main.sh" diff --git a/setup.sh b/setup.sh index 543c1bc..e25f890 100644 --- a/setup.sh +++ b/setup.sh @@ -23,17 +23,35 @@ function runSetupConfig () { # get backup type echo -ne "\n Select Backup Type\n" - echo -ne " 1 = Remote Server\n" + echo -ne " 1 = Remote Server (default)\n" echo -ne " 2 = Dropbox\n" echo -ne " # Make your selection [1/2]: " read -r INPUT_BACKUPTYPE + # set default + INPUT_BACKUPTYPE=${INPUT_BACKUPTYPE:-1} # get the details for the backup type if [ "$INPUT_BACKUPTYPE" -eq "2" ]; then - # get dropbox uploader path - echo -ne "\n Set the Path to the Dropbox Uploader File\n" - echo -ne " Use https://github.com/andreafabrizi/Dropbox-Uploader\n" - echo -ne " # Example (/home/path/to/Dropbox-Uploader/dropbox_uploader.sh): " - read -r INPUT_DROPBOX + # check if another should be added + echo -ne "\n Do you have the path to the Dropbox Uploader File? [y/N]: " + read -r answer + if [[ $answer != "y" ]]; then + echo -ne "\n YOU MUST GET THE PATH TO THE DROPBOX UPLOADER FILE!\n" ; + echo -ne " For more help https://github.com/andreafabrizi/Dropbox-Uploader\n" + # start again + exit 1 + else + # get dropbox uploader path + echo -ne "\n Set the Path to the Dropbox Uploader File\n" + echo -ne " # Example (/home/path/to/Dropbox-Uploader/dropbox_uploader.sh): " + read -r INPUT_DROPBOX + # check that we have a string + if [ ! ${#INPUT_DROPBOX} -ge 2 ]; then + echo -ne "\n YOU MUST GET THE PATH TO THE DROPBOX UPLOADER FILE!\n" ; + echo -ne " For more help https://github.com/andreafabrizi/Dropbox-Uploader\n" + # start again + exit 1 + fi + fi # set default remote server details INPUT_REMOTESSH="user@yourserver.com" else @@ -41,32 +59,47 @@ function runSetupConfig () { echo -ne "\n Set the Remote Server Details\n" echo -ne " # Example (user@yourserver.com): " read -r INPUT_REMOTESSH + # check that we have a string + if [ ! ${#INPUT_REMOTESSH} -ge 2 ]; then + echo -ne "\n YOU MUST GIVE THE REMOTE SERVER DETAILS!\n" ; + echo -ne " # Example (user@yourserver.com)\n" + # start again + exit 1 + fi # set default dropbox details - INPUT_DROPBOX="/media/host/root/Dropbox-Uploader/dropbox_uploader.sh" + INPUT_DROPBOX="/home/path/to/Dropbox-Uploader/dropbox_uploader.sh" fi # get the remote database backup paths echo -ne "\n Set Remote Backup Path for Database Backups\n" - echo -ne " # Example (your_db_path/): " + echo -ne " # Default (db_path/): " read -r INPUT_REMOTEDBPATH + # set default + INPUT_REMOTEDBPATH=${INPUT_REMOTEDBPATH:-'db_path/'} # get the remote website backup paths echo -ne "\n Set Remote Backup Path for Website Backups\n" - echo -ne " # Example (your_website_path/): " + echo -ne " # Default (website_path/): " read -r INPUT_REMOTEWEBPATH + # set default + INPUT_REMOTEWEBPATH=${INPUT_REMOTEWEBPATH:-'website_path/'} # select the website backup type echo -ne "\n Select the Website Backup Type\n" echo -ne " 1 = per/file\n" - echo -ne " 2 = zipped package\n" + echo -ne " 2 = zipped package (default)\n" echo -ne " # Make your selection [1/2]: " read -r INPUT_WEBBACKUPTYPE + # set default + INPUT_WEBBACKUPTYPE=${INPUT_WEBBACKUPTYPE:-2} # select the backup file name convention echo -ne "\n Select the Backup File Name Convention\n" echo -ne " 0 = add no date\n" - echo -ne " 1 = add only year\n" + echo -ne " 1 = add only year (default)\n" echo -ne " 2 = add year & month\n" echo -ne " 3 = add year, month & day\n" echo -ne " 4 = add year, month, day & time\n" echo -ne " # Make your selection [1-4]: " read -r INPUT_USEDATE + # set default + INPUT_USEDATE=${INPUT_USEDATE:-1} # now add it all to the config file echo "#!/bin/bash" > "$1"