]> lifelog.hopto.org Git - wb-shell-scripts.git/commitdiff
Updated to store archive stats and info. Fixed pv progress bar display in verbose...
authorWill Budic <redacted>
Tue, 6 Aug 2024 11:15:27 +0000 (21:15 +1000)
committerWill Budic <redacted>
Tue, 6 Aug 2024 11:15:27 +0000 (21:15 +1000)
linux-B_L_R_via_sshfs/backup.sh
linux-B_L_R_via_sshfs/list.sh
linux-B_L_R_via_sshfs/restore.sh

index 3390cdac08a47b23c602d3bab24114834e9e5cda..789a21e025e5ab0da6deb4321b732b056356fac0 100755 (executable)
@@ -1,55 +1,54 @@
 #!/bin/bash
-# Included backup.config is only to be modified.
+# Included backup.config file is only to be modified as the copy in -> ~/.config/backup.config
 SRC_DIR=`dirname "$0"`
 if [[ -f ~/.config/backup.config ]]; then
-CONFIG_FILE="$HOME/.config/backup.config"
+    CONFIG_FILE="$HOME/.config/backup.config"
 else
-CONFIG_FILE="$SRC_DIR/backup.config"
-fi 
+    CONFIG_FILE="$SRC_DIR/backup.config"
+fi
 #
-
-if ! command -v /usr/bin/fdfind &>  /dev/null 
+BCK_LST="/tmp/backup.lst"
+if ! command -v /usr/bin/fdfind &>  /dev/null
 then
     echo "Terminating, the 'find-fd' utility not found! Try -> sudo apt install fd-find"
     exit
 fi
 
-if ! command -v gpg &>  /dev/null 
+if ! command -v gpg &>  /dev/null
 then
     echo "Terminating, the 'gpg' utility <https://www.gnupg.org> not found! Try -> sudo apt install pgp"
     exit
 fi
 
-
 if [[ $1 =~ ^--config= ]]
 then
-  _load=$(echo $1 | awk -F= '{print $2}');  shift;
-elif [[ $1 =~ ^--config ]]; then
-  shift; _load="$1"; shift
+    _load=$(echo $1 | awk -F= '{print $2}');  shift;
+    elif [[ $1 =~ ^--config ]]; then
+    shift; _load="$1"; shift
 fi
-if [[ $_load ]]; then 
+if [[ $_load ]]; then
     if [[ -f $_load ]]; then
-        . $_load ; echo "Loaded config file '$_load'"; 
+        . $_load ; echo "Loaded config file '$_load'";
     else
         echo "Error! Config file '$_load' not found!"; exit 2;
     fi
 else
-        . $CONFIG_FILE
+    . $CONFIG_FILE
 fi
 
 if [[ $1 =~ ^--target= ]]
 then
-  TARGET=$(echo $1 | awk -F= '{print $2}')
-  echo -e "Target directory set as: $TARGET"; IS_LOCAL=1
-  shift; 
+    TARGET=$(echo $1 | awk -F= '{print $2}')
+    echo -e "Target directory set as: $TARGET"; IS_LOCAL=1
+    shift;
 fi
 [[ $1 =~ ^--target ]] && shift #handled as last argument later.
 
 if [[ $1 =~ ^--name= ]]
 then
-  POSFIX=$(echo $1 | awk -F= '{print $2}');shift
-elif [[ $1 =~ ^--name ]]; then
-  shift; POSFIX="$1";
+    POSFIX=$(echo $1 | awk -F= '{print $2}');shift
+    elif [[ $1 =~ ^--name ]]; then
+    shift; POSFIX="$1";
 fi
 [[ $POSFIX ]] && echo -e "Posfix name set as: $POSFIX" && POSTFIX="$POSFIX-";
 
@@ -68,25 +67,25 @@ read -r -d '' help <<-__help
                          This option is usually used in combination with the --name posfix.
 
     \e[0;37m--target\e[0;32m{=} {path} - Specifically assigned backup target path.
-    \e[0;37m--name=\e[0;32m{word}      - Assign posfix name for current run, the default config doesn't use it or set it, 
+    \e[0;37m--name=\e[0;32m{word}      - Assign posfix name for current run, the default config doesn't use it or set it,
                          so an potetnially existing backup on the target will not be overwritten,
                          i.e. while testing.
-    
+
     \e[0;37m--help\e[1;32m | \e[0;37m-?\e[0;32m        - Prints this help.
 
     The reason for the backup command being semi-automatic via an config file, is to expect have everything setup in that file
-    and usually run once in a week as a background cron job. Or from the command line. 
-    As an typical backup can take an long time to run. Use the enarch utility (file://$SRC_DIR/enarch.pl), 
+    and usually run once in a week as a background cron job. Or from the command line.
+    As an typical backup can take an long time to run. Use the enarch utility (file://$SRC_DIR/enarch.pl),
     for smaller e-safe archives instead.
 
-    The resulting backup will be both encrypted and compressed as the final product. 
+    The resulting backup will be both encrypted and compressed as the final product.
     \e[31mWarning\e[0m -> \e[0;32mDo not keep an copy of the key or config file on the target or server computer. Store it in a password manager.
     Recomended passwprd manaker I use is -> https://keepassxc.org/
 
 \e[0mThis script originated from https://github.com/wbudic/B_L_R_via_sshfs
 __help
-echo -e "$help"
-exit
+    echo -e "$help"
+    exit
 }
 [[ $1 =~ ^--h || $1 =~ ^-\? ]] && showHelp
 
@@ -95,198 +94,239 @@ DATE=$(date +%Y%m%d)
 export BACKUP_START=`date +%F%t%T`
 BACKUP_FILE="$THIS_MACHINE-$POSFIX$DATE.$EXT_ENC"
 BACKUP_INDEX="$THIS_MACHINE-$POSFIX$DATE.$EXT_LST"
+BACKUP_STATS_FILE="$THIS_MACHINE-$POSFIX$DATE.txt"
 
-if [[ -z IS_LOCAL || -z "$1" ]] 
-then        
+if [[ -z IS_LOCAL || -z "$1" ]]
+then
     # By default the backup goes to a remote mounting point target in the config file.
     #[[ ! `stat -c%d "$TARGET" > /dev/null 2>&1` ]] && echo "Error target '$TARGET' is not valid!" && exit 2
     
     if [ `stat -c%d "$TARGET" 2>&1` != `stat -c%d "$TARGET/.." 2>&1` ]; then
-         [[ $? -eq 1 ]] && exit 1
+        [[ $? -eq 1 ]] && exit 1
         echo "We have access, $TARGET is already mounted."
     else
-        echo "Your are about to remote backup '$HOME' to $TARGET";    
+        echo "Your are about to remote backup '$HOME' to $TARGET";
         sshfs "$USER@$DEST_SERVER:$BACKUP_DIRECTORY" $TARGET -o allow_other
         [[ $? -eq 1 ]] && echo "Error aborting! '$TARGET' is not valid!" && exit 2
         echo "Mounted $TARGET."
-    fi    
+    fi
 else
     [[ $1 ]] && TARGET=$1
     #echo "Local target: $TARGET";
-    if [[ ! -d "$TARGET" ]] 
+    if [[ ! -d "$TARGET" ]]
     then
         echo -e "\nTarget location doesn't exits: $TARGET"
         echo -e "Available Media:"
-        find /media/$USER -maxdepth 2 -type d -print         
+        find /media/$USER -maxdepth 2 -type d -print
         echo -e "\nAvailable Mounts:"
         find /mnt/ -maxdepth 2 -type d -not -path '*/\.*' -print
         exit 1
     else
         echo "Your are about to backup locally to $TARGET/$BACKUP_DIRECTORY/$BACKUP_FILE"
     fi
-
+    
     if [[ ! -d "$TARGET/$BACKUP_DIRECTORY" ]]; then
-            echo -e "Target directory doesn't exist: $TARGET"
-            echo -e "If this is expected to be mounted on another server, you don't want it to be created from this script."
-            declare -i times=0
-            while true; do
-                read -p "Do you want it to be created ?" yn
-                case $yn in
-                    [Yy]* ) mkdir $TARGET/$BACKUP_DIRECTORY; break;;
-                    [Nn]* ) exit;;
-                    * ) times+=1;let left=3-$times; echo -e "You made $times attempts have $left left.\nPlease answer [y]es or [n]o."; [[ $times > 2 ]] && exit 1;;
-                esac
-            done
-            $TARGET="$TARGET/$BACKUP_DIRECTORY"
+        echo -e "Target directory doesn't exist: $TARGET"
+        echo -e "If this is expected to be mounted on another server, you don't want it to be created from this script."
+        declare -i times=0
+        while true; do
+            read -p "Do you want it to be created ?" yn
+            case $yn in
+                [Yy]* ) mkdir $TARGET/$BACKUP_DIRECTORY; break;;
+                [Nn]* ) exit;;
+                * ) times+=1;let left=3-$times; echo -e "You made $times attempts have $left left.\nPlease answer [y]es or [n]o."; [[ $times > 2 ]] && exit 1;;
+            esac
+        done
+        $TARGET="$TARGET/$BACKUP_DIRECTORY"
     fi
 fi
 
+function printLine {    
+    
+    ret=`printf "\e[42;1m %.0s\e[0m" {1..80}`
+    #ret=`printf "%.0s" {1..80}`;
+    echo -e "$ret"
+}
 
 function DoBackup () {
-
-echo "Obtaining file selection list..."
-pushd $HOME
-echo -e "Started creating $TARGET/$BACKUP_FILE   Using Config:$CONFIG_FILE"
-
-# Add by default all the dotties hotties first.
-/usr/bin/fdfind -H -t file --max-depth 1 . | sed -e 's/^\.\///' | sort -d > /tmp/backup.lst
-
-#Check config file specified directories to exist.
-for dir in $DIRECTORIES
-do
-  if [[ -d $dir ]]; then
-    directories="$directories $dir"
-    elif [[ -f $dir ]]; then #maybe glob expanded to an file?
-        glob_files=$(echo -e "$glob_files $dir\f") 
+    
+    echo "Obtaining file selection list..."
+    pushd $HOME
+    echo -e "Started creating $TARGET/$BACKUP_FILE   Using Config:$CONFIG_FILE"
+    
+    # Add by default all the dotties hotties first.
+    /usr/bin/fdfind -H -t file --max-depth 1 . | sed -e 's/^\.\///' | sort -d > $BCK_LST
+    
+    #Check config file specified directories to exist.
+    stat_glob="Using Config:$CONFIG_FILE\n"
+    
+    for dir in $DIRECTORIES
+    do
+        if [[ -d $dir ]]; then
+            directories="$directories $dir"
+            elif [[ -f $dir ]]; then #maybe glob expanded to an file?
+            glob_files+=$(echo -e "$glob_files $dir\f")
+        else
+            stat_m="Skipping specified directory '$dir' not found!";
+            echo "$stat_m"
+            stat_glob+="$stat_m\n"
+        fi
+    done
+    
+    if [[ ! -z  $directories ]]; then
+        stat_m="Collecting files from: $directories";
+        echo "$stat_m";
+        stat_glob+="$stat_m\n";
+        /usr/bin/fdfind -H -t file -I $EXCLUDES . $directories | sort -d  >> $BCK_LST;
+        [[ $? != 0 ]] && exit $?;
+    fi
+    
+    if [[ ! -z  $glob_files ]]; then
+        glob_files=$(echo "$glob_files" | perl -pe 's/\f\s*/\n/g');
+        stat_m="Adding glob files:\n$glob_files\n";
+        stat_glob+="$stat_m\n";
+        echo -e "$stat_m";
+        echo -e "$glob_files" | sed -e 's/^[[:blank:]]*//' >> $BCK_LST;
+        [[ $? != 0 ]] && exit $?;
+    fi
+    
+    file_size=0
+    file_cnt=0
+    # Prevent glob expansion during rsync: set -f
+    set -f
+    for entry in $WILDFILES
+    do
+        if [[ $entry =~ ^\*\. ]]; then
+            echo "Ignoring obsolete extension glob setting -> $entry"
+        else
+            set +f
+            #via echo glob translate to find the files.
+            echo $entry | sed -e 's/ /\n/g' > /tmp/backup_wilderbeasts.lst
+            set -f
+            while IFS= read -r file;
+            do if [[ -n "$file" ]]; then
+                    size=$(stat -c %s "$file");
+                    file_size=$(($file_size + $size));  #file_cnt=$(($file_cnt + 1))
+                    #[[ ! -d  ~/_BACKUP_WILDFILES ]] &&  mkdir ~/_BACKUP_WILDFILES
+                    rsync -rlR $file ~/_BACKUP_WILDFILES
+                    echo "_BACKUP_WILDFILES$file" >> $BCK_LST
+                fi
+            done < /tmp/backup_wilderbeasts.lst
+        fi
+    done
+    set +f
+            
+    if [[ "$BACKUP_VERBOSE" -eq 1 ]]; then
+        while NFS= read -r file;
+        do if [[ -n "$file" ]]; then
+                size=$(stat -c %s "$file");
+                file_cnt=$(($file_cnt + 1));
+                file_size=$(($file_size + $size));
+                file_size=$(($file_size + ${#file}));
+            fi
+        done < <(pv -N "Please wait, obtaining all file stats..." -pt -w 100 $BCK_LST);
+        
+        target_avail_space=$(df "$TARGET" | awk '{print $4}' | tail -n 1);
+        file_size_formated=$(numfmt --to=iec-i "$file_size");
+        file_size_kb=$(($file_size / 1024));
+        stats="Target avail space: $target_avail_space\n";
+        stats+="Backup File count: $file_cnt ($file_size bytes)\n";
+        stats+="Backup uncompressed size in kbytes: $file_size_kb\n";
+        echo -e "$stats";
+        
+        if [[ $file_size_kb -gt $target_avail_space ]]
+        then
+            target_avail_space=$(numfmt --to=iec-i $target_avail_space)
+            echo -e "\nAvailable space on $TARGET is $target_avail_space,"
+            echo -e "this is less to volume required of $file_size_formated of $file_cnt files uncompressed.\n"
+            read -p "Are you sure you want to proceed? \(Answer Yes/No\): " rep;
+            if [[ ! $rep =~ ^Y|^y ]]
+            then
+                echo "Backup has been skipped."
+                exit 1;
+            fi
+        fi
+        
+        printLine;
+        echo "  Started archiving! Archive Bulk to Compress is: $file_size_formated";
+        printLine;
+        
+        # Notice - tar archives and compresses in blocks, piped out to pv is not actual byte mass, hence no lineral progressbar.
+        # xz compression level 1 will spped up the backup, compression size if of little difference on a large number of text files in bulk.
+        # Change to XZ_OPT=-e9 if backing up large binaries.
+        #
+        export XZ_OPT=-e1;
+        export status_bar_lbl=`echo -e "\e[32mBackup Status\e[0m"`;
+        echo -e "\e[32mBacking up to ->\e[0m $TARGET/$BACKUP_FILE";
+        #
+        tar cJvi --exclude-caches-all --exclude-vcs --exclude-vcs-ignores --exclude-backups -P -T $BCK_LST | \
+        pv -N "$status_bar_lbl" --timer --rate --eta -pw 80 -bs `echo "$file_size/1.8"|bc` | \
+        gpg -c --no-symkey-cache --batch --passphrase $GPG_PASS > $TARGET/$BACKUP_FILE 2>&1;
     else
-        echo "Skipping specified directory '$dir' not found!"
+        tar cJi --exclude-caches-all --exclude-vcs --exclude-vcs-ignores --exclude-backups \
+        -P -T $BCK_LST | \
+        gpg -c --no-symkey-cache --batch --passphrase $GPG_PASS > $TARGET/$BACKUP_FILE 2>&1;
+    fi #of [[ "$BACKUP_VERBOSE" -eq 1 ]];
+    [[ $? != 0 ]] && echo "FATAL ERROR, EXITING BACKUP FOR $TARGET/$BACKUP_FILE !" && exit $?;
+    
+    export status_bar_lbl=`echo -e "\e[32mBackup Done\e[0m  "`;
+    pv -N "$status_bar_lbl" -pw 70 -bs $(stat -c %s "$BCK_LST") $BCK_LST >> /dev/null
+    
+    stats_backup_file=`ls -lah "$TARGET/$BACKUP_FILE"`;
+    stats_backup_file_size=$(echo "$stats_backup_file" | awk '{print $5}');    
+       
+    if [[ $? == 0 && $(ls -la "$TARGET/$BACKUP_FILE" | awk '{print $5}') -eq 0 ]]; then
+        echo "BACKUP FAILED FOR $TARGET/$BACKUP_FILE!!!";
+        exit $?
     fi
-done
-
-if [[ ! -z  $directories ]]; then
-    echo "Collecting files from: $directories"
-    /usr/bin/fdfind -H -t file -I $EXCLUDES . $directories | sort -d  >> /tmp/backup.lst
-    [[ $? != 0 ]] && exit $?;
-fi
-
-if [[ ! -z  $glob_files ]]; then    
-    glob_files=$(echo "$glob_files" | perl -pe 's/\f\s*/\n/g')    
-    echo -e "Adding glob files:\n$glob_files"    
-    echo -e "$glob_files" | sed -e 's/^[[:blank:]]*//' >> /tmp/backup.lst
-    [[ $? != 0 ]] && exit $?;
-fi
-
-
-file_size=0
-file_cnt=0
-set -f
-for entry in $WILDFILES
-do
- if [[ $entry =~ ^\*\. ]]; then
-        echo "Igoning obsolete extension glob setting -> $entry"        
- else
-       set +f
-       #via echo glob translate to find the files.
-       echo $entry | sed -e 's/ /\n/g' > /tmp/backup_wilderbeasts.lst
-       set -f
-       while IFS= read -r file; 
-       do if [[ -n "$file" ]]; then 
-          size=$(stat -c %s "$file"); file_size=$(($file_size + $size));  file_cnt=$(($file_cnt + 1))
-          #[[ ! -d  ~/_BACKUP_WILDFILES ]] &&  mkdir ~/_BACKUP_WILDFILES
-          rsync -rlR $file ~/_BACKUP_WILDFILES
-          echo "_BACKUP_WILDFILES$file" >> /tmp/backup.lst
-       fi 
-       done < /tmp/backup_wilderbeasts.lst
- fi
-done
-set +f
-
-
-#################################################################################################
-if [[ "$BACKUP_VERBOSE" -eq 1 ]]; then 
-
- while NFS= read -r file; 
-    do if [[ -n "$file" ]]; then size=$(stat -c %s "$file"); file_size=$(($file_size + $size)); file_cnt=$(($file_cnt + 1)); fi 
- done < <(pv -N "Please wait, obtaining all file stats" -pt -w 100 /tmp/backup.lst)
- echo "Backup File count: $file_cnt";
- target_avail_space=$(df "$TARGET" | awk '{print $4}' | tail -n 1) 
- echo "Target avail space: $target_avail_space"
- file_size_formated=$(numfmt --to=iec-i "$file_size") 
- file_size_kb=$(($file_size / 1024));
- echo "Backup size in kbytes:$file_size";
-  
- if [[ $file_size_kb -gt $target_avail_space ]] 
- then
-    target_avail_space=$(numfmt --to=iec-i $target_avail_space) 
-    echo -e "\nAvailable space on $TARGET is $target_avail_space,"
-    echo -e "this is less to volume required of $file_size_formated of $file_cnt files uncompressed.\n"
-       read -p "Are you sure you want to proceed? (Answer Yes/No): " rep; 
-       if [[ ! $rep =~ ^Y|^y ]]
-       then
-            echo "Backup has been skipped."
-            exit 1;
-       fi
- fi
- echo '#########################################################################'; 
- echo "  Started archiving! Expected archive size: $file_size_formated";
- echo '#########################################################################'; 
- # Notice - tar archives and compresses in blocks, piped out to pv is not actual byte mass, hence no lineral progressbar.
- tar cJvi --exclude-caches-all --exclude-vcs --exclude-vcs-ignores --exclude-backups -P -T /tmp/backup.lst | \
- pv -N "Backup Status" --timer --rate --bytes -pw 70| \
- gpg -c --no-symkey-cache --batch --passphrase $GPG_PASS > $TARGET/$BACKUP_FILE 2>&1;
- else
- tar cJi --exclude-caches-all --exclude-vcs --exclude-vcs-ignores --exclude-backups \
- -P -T /tmp/backup.lst | \
- gpg -c --no-symkey-cache --batch --passphrase $GPG_PASS > $TARGET/$BACKUP_FILE 2>&1;
-fi #of [[ "$BACKUP_VERBOSE" -eq 1 ]];
-#################################################################################################
-[[ $? != 0 ]] && echo "FATAL ERROR, EXITING BACKUP FOR $TARGET/$BACKUP_FILE !" && exit $?;
-
-echo '#########################################################################'; 
-ls -lah "$TARGET/$BACKUP_FILE"; 
-if [[ $? == 0 &&  $(ls -la "$TARGET/$BACKUP_FILE" | awk '{print $5}') -eq 0 ]]; then
-echo "BACKUP FAILED FOR $TARGET/$BACKUP_FILE!!!"
-exit $?
-fi 
-# Index
-cat /tmp/backup.lst | xz -9e -c > $TARGET/$BACKUP_INDEX
-rm /tmp/backup.lst
-rm /tmp/backup_wilderbeasts.lst
-[[ -d  ~/_BACKUP_WILDFILES ]] && rm -rf ~/_BACKUP_WILDFILES 
-##
-if [[ $? == 0 &&  $(ls -la "$TARGET/$BACKUP_INDEX" | awk '{print $5}') -eq 0 ]]; then
-echo "BACKUP FAILED FOR $TARGET/$BACKUP_INDEX!!!"
-exit $?
-fi 
-
-df -h "$TARGET/$BACKUP_FILE";
-
-#Remove older backups
-find $TARGET/backups/$THIS_MACHINE*.$EXT_ENC -mtime +1 -exec rm {} + > /dev/null 2>&1
-find $TARGET/backups/$THIS_MACHINE*.$EXT_LST -mtime +1 -exec rm {} + > /dev/null 2>&1
-echo '#########################################################################'; 
-echo "Backup has finished for: $USER@$DEST_SERVER:$TARGET/$BACKUP_FILE"
-
-export BACKUP_END=`date +%F%t%T`;
-export BACKUP_TIME=`dateutils.ddiff -f "%H hours and %M minutes %S seconds" "$BACKUP_START" "$BACKUP_END" \
-| awk '{gsub(/^0 hours and /,"");}1' | awk '{gsub(/^0 minutes\s*/,"");}1'`
-echo "Backup started : $BACKUP_START"
-echo "Backup ended   : $BACKUP_END"
-echo "Backup took    : $BACKUP_TIME";
-
-popd > /dev/null
-# Mine (Will Budic) user variable concept, should be part of the system.
-# So if the computer has been shut down on the last given backup date, 
-# to start an backup immediately on the next cron_maintenance script run.
-[[ -f "/home/will/uvar.sh" ]] && /home/will/uvar.sh -n "LAST_BACKUP_DATE" -v "$BACKUP_END"
-echo -e "\nDone with backup of $HOME on " `date` ", have a nice day!"
-
+    # Generate Index file and remove the temps.
+    cat $BCK_LST | xz -9e -c > $TARGET/$BACKUP_INDEX;
+    rm $BCK_LST;
+    rm /tmp/backup_wilderbeasts.lst;
+    [[ -d  ~/_BACKUP_WILDFILES ]] && rm -rf ~/_BACKUP_WILDFILES ;
+    ##
+    if [[ $? == 0 &&  $(ls -la "$TARGET/$BACKUP_INDEX" | awk '{print $5}') -eq 0 ]]; then
+        echo "BACKUP FAILED FOR $TARGET/$BACKUP_INDEX!!!"
+        exit $?
+    fi        
+    # Remove older backups if any.
+    find $TARGET/$THIS_MACHINE*.$EXT_ENC -mtime +1 -exec rm {} + > /dev/null 2>&1
+    find $TARGET/$THIS_MACHINE*.$EXT_LST -mtime +1 -exec rm {} + > /dev/null 2>&1
+    find $TARGET/$THIS_MACHINE*.txt -mtime +1 -exec rm {} + > /dev/null 2>&1
+     printLine;
+    echo -e "Backup has finished for: $stats_end";
+    printLine;
+    stats_end="$USER@$DEST_SERVER:$TARGET/$BACKUP_FILE\n";    
+    export BACKUP_END=`date +%F%t%T`;
+    export BACKUP_TIME=`dateutils.ddiff -f "%H hours and %M minutes %S seconds" "$BACKUP_START" "$BACKUP_END" \
+    | awk '{gsub(/^0 hours and /,"");}1' | awk '{gsub(/^0 minutes\s*/,"");}1'`;
+    export stats_target_space=`df -h "$TARGET/$BACKUP_FILE"`;
+    stats_end="Backup         : $stats_end";
+    stats_end+="Backup F. Size : $stats_backup_file_size\n";
+    stats_end+="File count     : $file_cnt\n";
+    stats_end+="Backup started : $BACKUP_START\n";
+    stats_end+="Backup ended   : $BACKUP_END\n";
+    stats_end+="Backup took    : $BACKUP_TIME\n";
+    stats_end+="Target Space   :{\n$stats_target_space\n}";
+    echo -e "$stats_end";
+    echo -e "$stats$stat_glob$stats_backup_file\n$stats_end" > $TARGET/$BACKUP_STATS_FILE;
+    
+    # cd back where came from.
+    popd > /dev/null;
+    #
+    # Setting mine (Will Budic) user variable concept, these should be part of the system.
+    # So if the computer has been shut down on the last given backup date,
+    # to start an backup immediately on the next cron_maintenance script run.
+    #
+    [[ -f "/home/will/uvar.sh" ]] && /home/will/uvar.sh -n "LAST_BACKUP_DATE" -v "$BACKUP_END";
+    
+    echo -e "Done with backup of $HOME on "`date`", have a nice day!";
+    
 }
-
 ##
 crontab -l>crontab.lst
 code --list-extensions > vs_code_extensions.lst
-DoBackup 
+DoBackup
 exit 0
 
 
index dc78e836d40c60139c575af9c21a08ffbf0fb0c8..5b09250c37e454e7740fb70e04177ec67df22d6c 100755 (executable)
@@ -9,9 +9,9 @@ fi
 . $CONFIG_FILE
 #
 echo -e "\n--------------------------------------------------------------------------------------------------------------"
-echo -e "This is an backup archive restore list creator, use this program from the directory you restore to locally."
-echo -e "Backup location is DEST_SERVER='$DEST_SERVER'"
-echo -e "Restore location is PWD='`pwd`'"
+echo -e " This is an backup archive restore list creator, use this program from the directory you restore to locally."
+echo -e " Backup location is DEST_SERVER='$DEST_SERVER'"
+echo -e " Restore location is PWD='`pwd`'"
 echo -e "--------------------------------------------------------------------------------------------------------------"
 echo -e "Syntax: $0 {-target=/some/path} {-keep} {restore.lst}\n"
 echo "After listing, to restore from the backup, use the list with the restore shell script."
index ad26cc8f941c74d2e0ccb0ddab40451b27927ada..c22a756a75837c2e4cf83bcde447dc9856baa134 100755 (executable)
 #!/bin/bash
 # Includes
+
+function showHelp () {
+    echo -e "--------------------------------------------------------------------------------------------------------------"
+    echo -e "Backup restore Utility\n\t This utility restores latest backup found in an target directory."
+    echo -e "The default settings or arguments are set in the backup.config file.\n And the list utility can be used for fast individual file slections. Before calling restore."
+    echo -e "Other availabe command line options:"
+    echo -e "-h|--help - For this help."
+    echo -e "--target {full_path_to_dir} - To select alterneative backup target location."
+    echo -e "                              Note - BACKUP_DIRECTORY and TARGET config settings will be also ignored."
+    echo -e "--gpg-pass {the_other_gpg_pass} - To overwrite the pgp_pass setting, found in backup.config."
+    echo -e "--------------------------------------------------------------------------------------------------------------"
+}
+
+
 SRC_DIR=`dirname "$0"`
 if [[ -f ~/.config/backup.config ]]; then
-CONFIG_FILE="$HOME/.config/backup.config"
+    CONFIG_FILE="$HOME/.config/backup.config"
 else
-CONFIG_FILE="$SRC_DIR/backup.config"
-fi 
+    CONFIG_FILE="$SRC_DIR/backup.config"
+fi
 . $CONFIG_FILE
 #
 # LST_ARG=restore.lst and individual files to restore as arguments follow.
 LST_ARG=$1;
 
 while [ ! -z "$1" ];do
-if [[ $1 =~ ^--target= ]]
-then
-  TARGET=$(echo $1 | awk -F= '{print $2}')
-  echo -e "Target directory set as: $TARGET"
-  shift; continue
-fi
-if [[ $1 =~ ^--gpg-pass= ]]
-then
-  GPG_PASS=$(echo $1 | awk -F= '{print $2}')
-  echo -e "Using gpg-pass: $GPG_PASS"
-  shift; continue; 
-fi
-case "$1" in
-    -h|--help)
-      showHelp; exit
-      ;;
-    --gpg-pass)
-      shift
-      GPG_PASS="$1"
-      echo -e "Using gpg-pass: $GPG_PASS"
-      ;;
-    --target)
-      shift
-      TARGET="$1"
-      shift
-      echo -e "Target directory set as: $TARGET"          
-      continue
-      ;;
-    *)
-        if [[ $1 =~ ^-- ]]; then
-          echo -e "Err: Unknow option $1 ignoring it. Try -h|--help?"
-          shift;
-          continue
-        fi
-
-        LST_ARG=$1;
-        shift 
-    ;;        
-esac
+    if [[ $1 =~ ^--target= ]]
+    then
+        TARGET=$(echo $1 | awk -F= '{print $2}')
+        echo -e "Target directory set as: $TARGET"
+        shift; continue
+    fi
+    if [[ $1 =~ ^--gpg-pass= ]]
+    then
+        GPG_PASS=$(echo $1 | awk -F= '{print $2}')
+        echo -e "Using gpg-pass: $GPG_PASS"
+        shift; continue;
+    fi
+    case "$1" in
+        -h|--help)
+            showHelp; exit
+        ;;
+        --gpg-pass)
+            shift
+            GPG_PASS="$1"
+            echo -e "Using gpg-pass: $GPG_PASS"
+        ;;
+        --target)
+            shift
+            TARGET="$1"
+            shift
+            echo -e "Target directory set as: $TARGET"
+            continue
+        ;;
+        *)
+            if [[ $1 =~ ^-- ]]; then
+                echo -e "Err: Unknow option $1 ignoring it. Try -h|--help?"
+                shift;
+                continue
+            fi
+            
+            LST_ARG=$1;
+            shift
+        ;;
+    esac
 done
 
-function showHelp () {
-echo -e "--------------------------------------------------------------------------------------------------------------"
-echo -e "Backup restore Utility\n\t This utility restores latest backup found in an target directory."
-echo -e "The default settings or arguments are set in the backup.config file.\n And the list utility can be used for fast individual file slections. Before calling restore."
-echo -e "Other availabe command line options:"
-echo -e "-h|--help - For this help."
-echo -e "--target {full_path_to_dir} - To select alterneative backup target location."
-echo -e "                              Note - BACKUP_DIRECTORY and TARGET config settings will be also ignored."
-echo -e "--gpg-pass {the_other_gpg_pass} - To overwrite the pgp_pass setting, found in backup.config."
-echo -e "--------------------------------------------------------------------------------------------------------------"
-}
-
 [[ ! -d "$TARGET" ]] && "$TARGET=$BACKUP_DIRECTORY/$TARGET" && echo -e "Reseting to: $TARGET"
-  if [[ ! -d "$TARGET" ]] 
-  then
-      echo -e "\nTarget doesn't exits: $TARGET"
-      echo -e "Available Media:"
-      find /media/$USER -maxdepth 2 -type d -print 
-      #ls -lr "/media/$USER" |  awk '! /^total|\./ {print}';
-      echo -e "\nAvailable Mounts:"
-      #ls -lr "/mnt/"|  awk '! /^total|\./ {print}';
-      find /mnt/ -maxdepth 2 -type d -print 
-      exit 1;
-  else
-      IS_LOCAL=1
-      DEST_SERVER=$THIS_MACHINE
-      LA1=$2;LA2=$3;LA3=$4;LA4=$5;
-  fi  
+if [[ ! -d "$TARGET" ]]
+then
+    echo -e "\nTarget doesn't exits: $TARGET"
+    echo -e "Available Media:"
+    find /media/$USER -maxdepth 2 -type d -print
+    #ls -lr "/media/$USER" |  awk '! /^total|\./ {print}';
+    echo -e "\nAvailable Mounts:"
+    #ls -lr "/mnt/"|  awk '! /^total|\./ {print}';
+    find /mnt/ -maxdepth 2 -type d -print
+    exit 1;
+else
+    IS_LOCAL=1
+    DEST_SERVER=$THIS_MACHINE
+    LA1=$2;LA2=$3;LA3=$4;LA4=$5;
+fi
 
 
 #echo -e "Last Argument:[$1]"
 if [ -z $IS_LOCAL ]
 then
-echo -e "\nYour are about to restore from '$USER@$DEST_SERVER:$BACKUP_DIRECTORY'" "Please enter $THIS_MACHINE's sudo password ->"
-sudo sshfs "$USER@$DEST_SERVER:$BACKUP_DIRECTORY" $TARGET -o allow_other 
-[[ $? -ne 1 ]] && echo -e "Exiting..." && exit 1
+    echo -e "\nYour are about to restore from '$USER@$DEST_SERVER:$BACKUP_DIRECTORY'" "Please enter $THIS_MACHINE's sudo password ->"
+    sudo sshfs "$USER@$DEST_SERVER:$BACKUP_DIRECTORY" $TARGET -o allow_other
+    [[ $? -ne 1 ]] && echo -e "Exiting..." && exit 1
 fi
 
 BACKUP_FILE=$(ls $TARGET/$THIS_MACHINE-*.$EXT_ENC|tail -n 1)
-if [[ -f $BACKUP_FILE ]] 
+if [[ -f $BACKUP_FILE ]]
 then
-echo -e "\nLocated backup file is -> $BACKUP_FILE"
-echo -e "Restore location or target will be -> $PWD\n"
-       else
-echo "No backup file has been found!"
-exit;
+    echo -e "\nLocated backup file is -> $BACKUP_FILE"
+    echo -e "Restore location or target will be -> $PWD\n"
+else
+    echo "No backup file has been found!"
+    exit;
 fi
 
 if [[ "." = "$SRC_DIR" ]]
 then
-       echo "Error, you are in the backup source directory: $PWD!"
-       exit 1;
+    echo "Error, you are in the backup source directory: $PWD!"
+    exit 1;
 fi
 
 
-function start (){  
-  RESTORE_START=`date +%F%t%T`;
-  FC=$(awk 'END{print NR}' $LST_ARG);
-  echo "Restoring files from [$LST_ARG] ($FC) ..."
-  gpg --decrypt --batch --passphrase $GPG_PASS $BACKUP_FILE | pv -N "Status" | tar -Jxv --files-from $LST_ARG
-  i#[[ $? != 0 ]] && echo "FATAL ERROR, EXITING RESTORE OF $BACKUP_FILE!\nFatal erros, check if your PGP_PASS is right?" && exit $?;
+function start (){
+    RESTORE_START=`date +%F%t%T`;
+    FC=$(awk 'END{print NR}' $LST_ARG);
+    echo "Restoring files from [$LST_ARG] ($FC) ..."
+    gpg --decrypt --batch --passphrase $GPG_PASS $BACKUP_FILE | pv -N "Status" | tar -Jxv --files-from $LST_ARG
+    [[ $? != 0 ]] && echo "FATAL ERROR, EXITING RESTORE OF $BACKUP_FILE!\nFatal erros, check if your PGP_PASS is right?" && exit $?;
 }
 
 function end (){
-  RESTORE_END=`date +%F%t%T`;
-  RESTORE_TIME=`dateutils.ddiff -f "%H hours and %M minutes %S seconds" "$RESTORE_START" "$RESTORE_END" \
-  | awk '{gsub(/^0 hours and/,"");}1' | awk '{gsub(/^\s*0 minutes\s*/,"");}1'`
-  echo -e "\nRestore started : $RESTORE_START"
-  echo -e "Restore ended   : $RESTORE_END"
-  echo -e "Restore took    : $RESTORE_TIME"
-  echo -e "Done with restore from: $BACKUP_FILE\nHave a nice day!"
+    RESTORE_END=`date +%F%t%T`;
+    RESTORE_TIME=`dateutils.ddiff -f "%H hours and %M minutes %S seconds" "$RESTORE_START" "$RESTORE_END" \
+    | awk '{gsub(/^0 hours and/,"");}1' | awk '{gsub(/^\s*0 minutes\s*/,"");}1'`
+    echo -e "\nRestore started : $RESTORE_START"
+    echo -e "Restore ended   : $RESTORE_END"
+    echo -e "Restore took    : $RESTORE_TIME"
+    echo -e "Done with restore from: $BACKUP_FILE\nHave a nice day!"
 }
 
 ##
@@ -132,22 +134,22 @@ function end (){
 
 if [[ -z $LST_ARG || ! -f $LST_ARG ]]
 then
-       echo -e "No valid list of files has been provided as argument,\nShould next the whole backup be restored into the '$PWD' directory."
-       read -p "Are you sure you want to proceed? (Answer Yes/No): " rep; 
-       if [[ $rep =~ ^Y|^y ]]
-       then
-          RESTORE_START=`date +%F%t%T`;
-          echo "Restoring whole archive!"
-          gpg --decrypt --batch --passphrase $GPG_PASS $BACKUP_FILE | pv -N "Status" | tar -Jxv $LA1 $LA2 $LA3 $LA4 $LA5; 
-          [[ $? != 0 ]] && echo -e "\nFatal erros, check if your --gpg-pass is right?" && exit $?;
-          end
-       else
-            echo "Restore has been skipped."
-            exit 1;
-       fi
-else 
-  start
-  end
+    echo -e "No valid list of files has been provided as argument,\nShould next the whole backup be restored into the '$PWD' directory."
+    read -p "Are you sure you want to proceed? (Answer Yes/No): " rep;
+    if [[ $rep =~ ^Y|^y ]]
+    then
+        RESTORE_START=`date +%F%t%T`;
+        echo "Restoring whole archive!"
+        gpg --decrypt --batch --passphrase $GPG_PASS $BACKUP_FILE | pv -N "Status" | tar -Jxv $LA1 $LA2 $LA3 $LA4 $LA5;
+        [[ $? != 0 ]] && echo -e "\nFatal erros, check if your --gpg-pass is right?" && exit $?;
+        end
+    else
+        echo "Restore has been skipped."
+        exit 1;
+    fi
+else
+    start
+    end
 fi