|
| 1 | +#!/bin/bash |
| 2 | +err_report() { |
| 3 | + echo "${0##*/} : Error on line $1" |
| 4 | + echo "${0##*/} : Please report this issue at 'https://github.com/McUsr/FB/issues'" |
| 5 | +} |
| 6 | + |
| 7 | +trap 'err_report $LINENO' ERR |
| 8 | +VERSION="\"v0.0.2\"" |
| 9 | +if [ ! -v FB ] ; then |
| 10 | + echo "${0##*/}" "The variable \$FB isn't set, is the system initialized? You need configure it.\nTerminating..." | journalThis 2 |
| 11 | + exit 255 |
| 12 | +fi |
| 13 | + |
| 14 | +if [ ! -d $FB ] ; then |
| 15 | + echo -e "${0##*/}" "The folder $FB can't be found!\nThe Google Drive folder is probably not shared with with Linux. Isn't set, is the system initialized? Maybe you need to configure it.\nTerminating..." | journalThis 2 OneShot |
| 16 | + exit 255 |
| 17 | +fi |
| 18 | + |
| 19 | +if [ -r ~/.local/bin/fb/shared_functions.sh ] ; then |
| 20 | + source ~/.local/bin/fb/shared_functions.sh |
| 21 | +else |
| 22 | + echo -e "Can't source: ~/.local/bin/fb/shared_functions.sh\nTerminates... " |
| 23 | + exit 255 |
| 24 | +fi |
| 25 | +PNAME=${0##*/} |
| 26 | +GETOPT_COMPATIBLE=true |
| 27 | + |
| 28 | +if [ $# -eq 0 ] ; then |
| 29 | + echo -e $PNAME : "Too few arguments. At least I need a folder target to backup.\nExecute \"$PNAME -h\" for help. Terminating..." >&2 |
| 30 | + exit 2 |
| 31 | +fi |
| 32 | +DEBUG=1 |
| 33 | +help() { |
| 34 | +cat << EOF |
| 35 | +
|
| 36 | +$PNAME: Restores a previous folder backup, made with the fb system. |
| 37 | +
|
| 38 | +syntax: |
| 39 | +
|
| 40 | + $PNAME [options] <source folder> <full-symlink-name> <destination> |
| 41 | + It is meant to be executed by *fboneshot* and not individually. |
| 42 | +
|
| 43 | + Options: |
| 44 | + |
| 45 | + -h| --help. Shows this help. |
| 46 | + -n| --dry-run Shows what would have happened |
| 47 | + -v| --verbose Shows more detailed output. |
| 48 | + -V| --version Shows the version of $PNAME ($VERSION). |
| 49 | + |
| 50 | +EOF |
| 51 | +} |
| 52 | +# set -x |
| 53 | +# https://stackoverflow.com/questions/402377/using-getopts-to-process-long-and-short-command-line-options |
| 54 | +TEMP=$(getopt -o hnvFV --longoptions help,verbose,dry-run,force,version \ |
| 55 | + -n "$PNAME" -- "$@") |
| 56 | + |
| 57 | +if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 2 ; fi |
| 58 | + |
| 59 | +# Note the quotes around '$TEMP': they are essential! |
| 60 | +eval set -- "$TEMP" |
| 61 | + |
| 62 | +PARSE_DEBUG=1 |
| 63 | +if [ $PARSE_DEBUG -eq 0 ] ; then |
| 64 | + echo ${0##*/} TEMP : "$TEMP" |
| 65 | +fi |
| 66 | +DRYRUN=false |
| 67 | +VERBOSE=false |
| 68 | +FORCE=false |
| 69 | + |
| 70 | +# echo NARG1 : $# |
| 71 | + |
| 72 | +while true; do |
| 73 | + if [ $PARSE_DEBUG -eq 0 ] ; then |
| 74 | + echo "while ...." |
| 75 | + case "$1" in |
| 76 | + -h | --help ) echo 'h' ; help ; exit 0 ;; |
| 77 | + -n | --dry-run ) echo '--dry-run' ; DRYRUN=true; shift ;; |
| 78 | + -v | --verbose ) echo '--verbose ' ; VERBOSE=true; shift ;; |
| 79 | + -F | --force ) echo 'force' ; FORCE=true; shift ;; |
| 80 | + -V | --version ) echo 'version' ;echo $PNAME : $VERSION ; exit 0 ;; |
| 81 | + -- ) shift; break ;; |
| 82 | + # * ) break ;; |
| 83 | + esac |
| 84 | + else |
| 85 | + case "$1" in |
| 86 | + -h | --help ) help ; exit 0 ;; |
| 87 | + -n | --dry-run ) DRYRUN=true; shift ;; |
| 88 | + -v | --verbose ) VERBOSE=true; shift ;; |
| 89 | + -F | --force ) FORCE=true; shift ;; |
| 90 | + -V | --version ) echo $PNAME : $VERSION ; exit 0 ;; |
| 91 | + -- ) shift; break ;; |
| 92 | + # * ) break ;; |
| 93 | + esac |
| 94 | + fi |
| 95 | +done |
| 96 | + |
| 97 | +HAVING_ERRORS=false |
| 98 | +DEBUG=1 |
| 99 | + |
| 100 | +if [ $PARSE_DEBUG -eq 0 ] ; then |
| 101 | + echo "$@" |
| 102 | + echo 'Ended while ' |
| 103 | + echo NARG2 : $# |
| 104 | + echo D1 : $1 |
| 105 | +fi |
| 106 | + |
| 107 | +if [ $# -ne 3 ] ; then |
| 108 | + echo -e $PNAME : "Wrong number of few arguments. I need one argument for the folder to backup, the full symlink name, \nand the path to the destination folder of the backup operation.\nExecute \"$PNAME -h\" for help. Terminating..." >&2 |
| 109 | + exit 2 |
| 110 | +fi |
| 111 | + |
| 112 | +# we do check if the source folder exists. and that it doesn't exist within the FB-backup tree. |
| 113 | + |
| 114 | +if [ -d "$1" ] ; then |
| 115 | + TARGET_TEST="${1/$FB/}" |
| 116 | + if [ "$TARGET_TEST" != "$1" ] ; then |
| 117 | + if [ $DRYRUN = true ] ; then |
| 118 | + echo -e $PNAME : "The target folder IS inside "$FB".\n("$1")." |
| 119 | + HAVING_ERRORS=true |
| 120 | + else |
| 121 | + # same whether dry-run, verbose, or not. |
| 122 | + echo -e $PNAME : "The target of the backup is not allowed to be inside "$FB".\nTerminating..." |
| 123 | + exit 2 |
| 124 | + fi |
| 125 | + else |
| 126 | + if [[ $VERBOSE = true || $DEBUG -eq 0 ]] ; then |
| 127 | + echo -e $PNAME : "The target folder is NOT inside "$FB".\n("$1")." |
| 128 | + elif [[ $DRYRUN = true ]] ; then |
| 129 | + echo -e $PNAME : "The target folder is NOT inside "$FB".\n("$1")." |
| 130 | + # else we're passing through further down the road. |
| 131 | + fi |
| 132 | + fi |
| 133 | +else |
| 134 | + if [ $DRYRUN = false ] ; then |
| 135 | + echo -e $PNAME : "The destination folder $2 does not exist.\nTerminating..." |
| 136 | + exit 2 |
| 137 | + else |
| 138 | + HAVING_ERRORS=true |
| 139 | + echo -e $PNAME : "The destination folder $2 does not exist." |
| 140 | + fi |
| 141 | +fi |
| 142 | +TARGET_FOLDER="$1" |
| 143 | + |
| 144 | +# we check if the destination path exists, and within the FB-backup tree. |
| 145 | +if [ -d "$2" ] ; then |
| 146 | + DEST_TEST="${2/$FB/}" |
| 147 | + if [ "$DEST_TEST" = "$2" ] ; then |
| 148 | + # same whether dry-run, verbose, or not. |
| 149 | +# if [ $DRYRUN = false ] ; then |
| 150 | + echo -e $PNAME : "The destination folder is NOT inside\n"$FB"\n which it must be. Terminating..." |
| 151 | + exit 2 |
| 152 | +# else |
| 153 | +# echo -e $PNAME : "The destination folder is NOT inside\n"$FB"\n which it must be. Terminating..." |
| 154 | +# HAVING_ERRORS = true |
| 155 | + # TODO: DEBATE: continue/not? |
| 156 | +# fi |
| 157 | + else |
| 158 | + if [[ $VERBOSE = true || $DEBUG -eq 0 ]] ; then |
| 159 | + echo -e $PNAME : "The destination folder is inside "$FB".\n("$2")." |
| 160 | + fi |
| 161 | + # else .. silently moving on ... |
| 162 | + fi |
| 163 | +else |
| 164 | + if [ $DRYRUN = false ] ; then |
| 165 | + echo -e $PNAME : "The destination folder $2 does not exist.\nTerminating..." |
| 166 | + exit 2 |
| 167 | + else |
| 168 | + HAVING_ERRORS=true |
| 169 | + echo -e $PNAME : "The destination folder $2 does not exist." |
| 170 | + fi |
| 171 | +fi |
| 172 | + |
| 173 | +TODAYS_BACKUP_FOLDER="$2" |
| 174 | +# we need to check if the full symlink name is within the destination path. |
| 175 | + |
| 176 | +SYMLINK_TEST="${TODAYS_BACKUP_FOLDER/$3/}" |
| 177 | +if [ "$SYMLINK_TEST" = "$TODAYS_BACKUP_FOLDER " ] ; then |
| 178 | + # same whether dry-run, verbose, or not. |
| 179 | +# if [ $DRYRUN = false ] ; then |
| 180 | + echo -e $PNAME : "The full symlink name is not the correct one.\n It is not the name of the root folder of the backup.\nTerminating..." |
| 181 | + exit 2 |
| 182 | +# else |
| 183 | +# echo -e $PNAME : "The full symlink name is not the correct one.\n It is not the name of the root folder of the backup.\nTerminating..." |
| 184 | +# HAVING_ERRORS = true |
| 185 | +# fi |
| 186 | +else |
| 187 | + if [[ $VERBOSE = true || $DEBUG -eq 0 ]] ; then |
| 188 | + echo -e $PNAME : "The full symlink name "$3" is the same as the name of the root folder of the backup:\n$TODAYS_BACKUP_FOLDER" |
| 189 | + fi |
| 190 | + # else .. silently moving on ... |
| 191 | +fi |
| 192 | +SYMLINK_NAME="$3" |
| 193 | +# time to look for any exclude files |
| 194 | + |
| 195 | +HAS_DROPIN_DIRECTORY=false |
| 196 | + |
| 197 | +if [ -d ~/.local/bin/fb/OneShot/"$SYMLINK_NAME".d ] ; then |
| 198 | + |
| 199 | + HAS_DROPIN_DIRECTORY=true |
| 200 | + if [[ $VERBOSE = true || $DEBUG -eq 0 || $DRYRUN = true ]] ; then |
| 201 | + echo -e $PNAME : "We have a dropin directory:\n$HOME/.local/bin/fb/OneShot/"$SYMLINK_NAME".d" |
| 202 | + fi |
| 203 | + |
| 204 | + HAS_EXCLUDE_FILE=false |
| 205 | + if [ -f ~/.local/bin/fb/OneShot/"$SYMLINK_NAME".d/exclude.file ] ; then |
| 206 | + HAS_EXCLUDE_FILE=true |
| 207 | + EXCLUDE_FILE=~/.local/bin/fb/OneShot/"$SYMLINK_NAME".d/exclude.file |
| 208 | + if [[ $VERBOSE = true || $DEBUG -eq 0 || $DRYRUN = true ]] ; then |
| 209 | + echo -e $PNAME : "We have an \"exclude.file\" file:\n$HOME/.local/bin/fb/OneShot/"$SYMLINK_NAME".d/exclude.file" |
| 210 | + fi |
| 211 | + fi |
| 212 | +else |
| 213 | + if [[ $VERBOSE = true || $DEBUG -eq 0 || $DRYRUN = true ]] ; then |
| 214 | + echo -e $PNAME : "We don't have a dropin directory: $HOME/.local/bin/fb/OneShot/"$SYMLINK_NAME".d" |
| 215 | + echo -e $PNAME : "We don't have an \"exclude.file\" file: $HOME/.local/bin/fb/OneShot/"$SYMLINK_NAME".d/exclude.file" |
| 216 | + fi |
| 217 | +fi |
| 218 | + |
| 219 | +PARSE_DEBUG=1 |
| 220 | +if [ $PARSE_DEBUG -eq 0 ] ; then |
| 221 | +# This is where we start: |
| 222 | + echo $PNAME : DRYRUN : $DRYRUN |
| 223 | + echo $PNAME : VERBOSE : $VERBOSE |
| 224 | + echo $PNAME : TODAYS_BACKUP_FOLDER : $TODAYS_BACKUP_FOLDER |
| 225 | + echo $PNAME : TARGET_FOLDER : $TARGET_FOLDER |
| 226 | + echo $PNAME : SYMLINK_NAME : $SYMLINK_NAME |
| 227 | + echo $PNAME : HAS_DROPIN_DIRECTORY : $HAS_DROPIN_DIRECTORY |
| 228 | + echo $PNAME : HAS_EXCLUDE_FILE : $HAS_EXCLUDE_FILE |
| 229 | + echo $PNAME : EXCLUDE_FILE : $EXCLUDE_FILE |
| 230 | +fi |
| 231 | + |
| 232 | +# Kan optimalisere senere ved å bare ha en EXCLUDE_FILE variabel. |
| 233 | +if [ $HAS_EXCLUDE_FILE ] ; then |
| 234 | + if [ $VERBOSE = true ] ; then |
| 235 | + echo "$PNAME : I have an exclude file : $EXCLUDE_FILE " |
| 236 | + cat "$EXCLUDE_FILE" |
| 237 | + fi |
| 238 | + EXCLUDE_OPTIONS="--exclude-from="$EXCLUDE_FILE |
| 239 | +else |
| 240 | + EXCLUDE_OPTIONS= |
| 241 | +fi |
| 242 | + |
| 243 | +if [[ $VERBOSE = true || $DEBUG -eq 0 ]] ; then |
| 244 | + VERBOSE_OPTIONS="-v -v" |
| 245 | +else |
| 246 | + VERBOSE_OPTIONS="-v" |
| 247 | +fi |
| 248 | +exit_status=0 |
| 249 | +if [ $DRYRUN = true ] ; then |
| 250 | + |
| 251 | + DRY_RUN_FOLDER=$(mktemp -d "/tmp/OneShot.backup.sh.XXX") |
| 252 | + |
| 253 | + trap "HAVING_ERRORS=true;ctrl_c" INT |
| 254 | + |
| 255 | +ctrl_c() { |
| 256 | + echo "$PNAME : trapped ctrl-c - interrupted tar command!" |
| 257 | + echo "$PNAME : We: rm -fr $DRY_RUN_FOLDER." |
| 258 | + rm -fr $DRY_RUN_FOLDER |
| 259 | +} |
| 260 | + |
| 261 | +# Bedre å sjekke error code? |
| 262 | + TAR_BALL_NAME="$DRY_RUN_FOLDER"/"$(baseNameTimeStamped "$SYMLINK_NAME" )"-backup.tar.gz |
| 263 | + if [ $HAVING_ERRORS = false ] ; then |
| 264 | + # echo $PNAME : "$DRY_RUN_FOLDER"/"$(baseNameTimeStamped "$SYMLINK_NAME" )" |
| 265 | + # echo Hasta la vista! |
| 266 | + # exit 0 |
| 267 | + echo "$PNAME : sudo tar -z $VERBOSE_OPTIONS -c -f "$TAR_BALL_NAME" $EXCLUDE_OPTIONS -C "$TARGET_FOLDER" . " |
| 268 | + # | journalThis 7 OneShot |
| 269 | + sudo tar -z -c $VERBOSE_OPTIONS -f "$TAR_BALL_NAME" $EXCLUDE_OPTIONS -C "$TARGET_FOLDER" . |
| 270 | + exit_status=$? |
| 271 | + # | journalThis 7 OneShot |
| 272 | + if [ $exit_status -gt 1 ] ; then |
| 273 | + echo "$PNAME : exit status after tar commmand = $exit_status" |
| 274 | + echo "$PNAME : rm -fr $DRY_RUN_FOLDER" |
| 275 | + if [ -d $DRY_RUN_FOLDER ] ; then |
| 276 | + rm -fr $DRY_RUN_FOLDER |
| 277 | + fi |
| 278 | + fi |
| 279 | + else |
| 280 | + echo -e "$PNAME : DRY_RUN_FOLDER=\$(mktemp -d \"/tmp/OneShot.restore.sh.XXX\")" |
| 281 | + echo -e "$PNAME : sudo tar -z -c $VERBOSE_OPTIONS -c $EXCLUDE_OPTIONS -f "$TAR_BALL_NAME" -C "$TARGET_FOLDER" . " |
| 282 | + echo -e "$PNAME : rm -fr $DRY_RUN_FOLDER" |
| 283 | + fi |
| 284 | +else |
| 285 | + TAR_BALL_NAME="$TODAYS_BACKUP_FOLDER"/$(baseNameTimeStamped "$SYMLINK_NAME" )-backup.tar.gz |
| 286 | + # TODO: installer trap her. |
| 287 | + trap "HAVING_ERRORS=true;ctrl_c" INT |
| 288 | +ctrl_c() { |
| 289 | + echo trapped ctrl-c |
| 290 | + echo rm -f $TAR_BALL_NAME |
| 291 | + rm -f $TAR_BALL_NAME |
| 292 | +} |
| 293 | + |
| 294 | + if [[ $VERBOSE = true || $DEBUG -eq 0 ]] ; then |
| 295 | + echo -e "$PNAME : sudo tar -z -c $VERBOSE_OPTIONS -c $EXCLUDE_OPTIONS -f $TAR_BALL_NAME -C $TARGET_FOLDER" . |
| 296 | + fi |
| 297 | + sudo tar -z $VERBOSE_OPTIONS -c $EXCLUDE_OPTIONS -f $TAR_BALL_NAME -C "$TARGET_FOLDER" . |
| 298 | + exit_status=$? |
| 299 | + # | journalThis 7 OneShot |
| 300 | + if [ $exit_status -gt 1 ] ; then |
| 301 | + |
| 302 | + if [[ $VERBOSE = true || $DEBUG -eq 0 ]] ; then |
| 303 | + echo "$PNAME : exit status after tar commmand = $exit_status" |
| 304 | + fi |
| 305 | + |
| 306 | + if [ -f $TAR_BALL_NAME ] ; then |
| 307 | + if [[ $VERBOSE = true || $DEBUG -eq 0 ]] ; then |
| 308 | + echo "$PNAME : rm -f $TAR_BALL_NAME" |
| 309 | + fi |
| 310 | + rm -f $TAR_BALL_NAME |
| 311 | + fi |
| 312 | + elif [ $exit_status -eq 0 ] ; then |
| 313 | + echo "("$TAR_BALL_NAME")" |
| 314 | + fi |
| 315 | + |
| 316 | + # TODO: test på exit code her, og rydd opp hvis stoerre enn 1 (på denne backupen er det bare fila som skal slettes). |
| 317 | + # the output sent as a notice message. |
| 318 | +fi |
| 319 | + |
| 320 | +# TODO: More work on the notify-send message, and needs to send a message to the Journal as well. |
| 321 | +# Needs to learn the journalctl better first. |
| 322 | +if [[ $exit_status -eq 0 && $DRYRUN = false && $VERBOSE = true || $DEBUG -eq 0 ]] ; then |
| 323 | + echo "${0##*/} (Folder Backup):" "OneShot backup of $TARGET_FOLDER into $TODAYS_BACKUP_FOLDER complete!" | journalThis 5 OneShot |
| 324 | +fi |
| 325 | +exit $exit_status |
0 commit comments