Skip to content

Commit

Permalink
v1.2.0
Browse files Browse the repository at this point in the history
* Added Pushbullet notifications support using the -p argument.
* Added -y argument to skip warnings (which by default, require user input to continue)
  • Loading branch information
MichaelYochpaz committed Jun 18, 2020
1 parent 280ef00 commit 2dfe202
Showing 1 changed file with 48 additions and 22 deletions.
70 changes: 48 additions & 22 deletions backup
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
## ------------------------------------
## Made By: Michael Yochpaz (C) 2020
## https://github.com/MichaelYochpaz/Backup-Script
## Version: 1.1.1
## Version: 1.2
## License: GPLv3
## ------------------------------------
## -------------- Usage ---------------
Expand All @@ -13,22 +13,24 @@
## - In order to use the upload feature, rclone must be installed and have at least one cloud service setup in config.
## ------------------------------------
##
## Usage: backup [-n <name>] [-s <path>] [-e <pattern>]... [-u <path>] [-r] [-v] <path-to-backup>
## Usage: backup [-n <name>] [-s <path>] [-e <pattern>]... [-u <path>] [-p <API key>] [-r] [-y] [-v] <path-to-backup>
##
## Options:
## -n <name> Sets the tar.gz file name [default: "backup"]
## -s <path> Path to which the generated backup file will be saved to [default: current working directory]
## -e <pattern> Exclude a pattern (specific files / folders) from being backed up
## -u <path> rclone path to which the backup file will be uploaded to (not providing one will skip the upload process)
## -p <API key> Sends a Pushbullet notification once backup is done to the given API key
## -r Removes local copy of backup file after it's been uploaded
## -v Uses the '-v' option when running tar and rclone
## -y Skip warnings (by default, warnings require user input to contine)
## -v Uses '-v' option when running tar and rclone
##
## Commands:
## -h Displays this help and exists.
##
## Examples:
## backup "/home/user/important_stuff"
## backup -u "GDrive:/Backups" -r "/home/user/important_stuff/"
## backup -u "GDrive:/Backups" -r -y -p "XXXXXXXXXXXXXXXX" "/home/user/important_stuff/"
## backup -n "important-stuff-backup" -s "/home/user/backups" -e "*.pdf" -e "important_stuff/dont_backup_this_folder" "/home/user/important_stuff/"
##
## --------- Configuration ------------
Expand All @@ -37,12 +39,23 @@ BACKUP_FOLDER="" # folder to backup (full path)
EXCLUDES=() # array to store exclusion patterns. example: ("*.mkv" "*.pdf" "/home/user/important_stuff/no_backup_folder")
SAVE_FOLDER="$PWD" # folder to save backup file to (full path)
RCLONE_FOLDER="" # path to rclone folder to which file will be uploaded to. leave empty to skip upload
PUSHBULLET_API_KEY="" # Pusbullet API key for sending a notification once backup is done
REMOVE_LOCAL=false # remove local backup file after upload
SKIP_WARNINGS=false # skip warnings that require user input to continue
VERBOSE=false # use the '-v' option when running tar and rclone
PUSHBULLET_TITLE="Backup Script" # Pusbullet - notification title
PUSHBULLET_MESSAGE_START="Backup script " # Pushbullet - notification messages will start with this string, followed by one of the followings
PUSHBULLET_MESSAGE_FINISHED_SUCCESS="finished successfully." # Pusbullet - message will be shown if script finished successfully
PUSHBULLET_MESSAGE_FINISHED_ERRORS="finished with errors." # Pusbullet - message will be shown if script finished with errors
PUSHBULLET_MESSAGE_FAIL="failed." # Pusbullet - message will be shown if script failed
## ------------------------------------

usage() { echo "Usage: $(basename $0) [-h] [-n <name>] [-s <path>] [-u <path>] [-r] <path>
Use $(basename $0) -h for more info."; exit 1; }
usage() { echo "Usage: $(basename $0) [-n <name>] [-s <path>] [-e <pattern>]... [-u <path>] [-p <API key>] [-r] [-y] [-v] <path-to-backup>
Use $(basename $0) -h for additional info."; exit 1; }

pushbullet() { if [ -n "$PUSHBULLET_API_KEY" ]; then curl -u "$PUSHBULLET_API_KEY": https://api.pushbullet.com/v2/pushes -d type=note -d \
title="$PUSHBULLET_TITLE" -d body="$PUSHBULLET_MESSAGE_START$1 " >/dev/null 2>&1; fi }


ERROR=false # sets to true when there's an error for final exit code
C0='\033[0m' # ANSI - no color
Expand All @@ -51,7 +64,7 @@ C2='\033[1;31m' # ANSI - error - red
C3='\033[0;36m' # ANSI - information - cyan
C4='\033[0;33m' # ANSI - variables/paths - orange

while getopts ":n:e:s:u:rvh" arg; do
while getopts ":n:e:s:u:p:ryvh" arg; do
case $arg in
n)
BACKUP_NAME=${OPTARG}
Expand All @@ -65,28 +78,36 @@ while getopts ":n:e:s:u:rvh" arg; do
u)
RCLONE_FOLDER=${OPTARG}
;;
p)
PUSHBULLET_API_KEY=${OPTARG}
;;
r)
REMOVE_LOCAL=true
;;
y)
SKIP_WARNINGS=true
;;
v)
VERBOSE=true
;;
h)
echo " Usage: $(basename $0) [-n <name>] [-e <pattern>]... [-s <path>] [-u <path>] [-r] <path-to-backup>
echo " Usage: $(basename $0) [-n <name>] [-s <path>] [-e <pattern>]... [-u <path>] [-p <API key>] [-r] [-y] [-v] <path-to-backup>
Options:
-n <name> Sets the tar.gz file name (date is added) [default: "backup"]
-s <path> Path to which the generated backup file will be saved to [default: current working directory]
-e <pattern> Exclude a pattern (specific files / folders) from being backed up
-u <path> rclone path to which the backup file will be uploaded to (not providing one will skip the upload process)
-p <API key> Sends a Pushbullet notification once backup is done to the given API key
-r Removes local copy of backup file after it's been uploaded
-v Uses the '-v' option when running tar and rclone
-y Skip warnings (by default, warnings require user input to contine)
-v Uses '-v' option when running tar and rclone
Commands:
-h Displays this help and exists.
Examples:
$(basename $0) \"/home/user/important_stuff\"
$(basename $0) backup -u \"GDrive:/Backups\" -r \"/home/user/important_stuff/\"
$(basename $0) -u \"GDrive:/Backups\" -r -y -p \"XXXXXXXXXXXXXXXX\" \"/home/user/important_stuff/\"
$(basename $0) -n \"important-stuff-backup\" -s \"/home/user/backups\" -e \"*.pdf\" -e \"important_stuff/dont_backup_this_folder\" \"/home/user/important_stuff/\""
exit 0
;;
Expand All @@ -103,19 +124,21 @@ if [ -z "$BACKUP_FOLDER" ]; then usage; fi
# if BACKUP_FOLDER path doesn't exist, show error and exit
if [ ! -d $BACKUP_FOLDER ]; then
echo -e "${C2}Folder ${C4}$BACKUP_FOLDER ${C2}not found${C0}"
pushbullet "$PUSHBULLET_MESSAGE_FAIL"
exit 1
fi
# if SAVE_FOLDER path doesn't exist, show error and exit
if [ ! -d $SAVE_FOLDER ]; then
echo -e "${C2}Folder ${C4}$SAVE_FOLDER ${C2}not found${C0}"
pushbullet "$PUSHBULLET_MESSAGE_FAIL"
exit 1
fi
# if BACKUP_FOLDER path ends with a '/', remove it
if [ "${BACKUP_FOLDER: -1}" = "/" ]; then BACKUP_FOLDER=${BACKUP_FOLDER%?}; fi
# if SAVE_FOLDER path ends with a '/', remove it
if [ "${SAVE_FOLDER: -1}" = "/" ]; then SAVE_FOLDER=${SAVE_FOLDER%?}; fi
# if REMOVE_LOCAL is set to true, but rclone upload is not used, show a warning.
if [ "$REMOVE_LOCAL" = true ] && [ -z "$RCLONE_FOLDER" ]; then
if [ "$REMOVE_LOCAL" = true ] && [ -z "$RCLONE_FOLDER" ] && [] "$SKIP_WARNINGS" = false ]; then
read -p "WARNING: '-r' argument to remove local backup file was used,
but no rclone path to upload backup flie to beforehand is set.
This will result in generating a backup file and deleting it after without it being uploaded anywhere.
Expand All @@ -139,33 +162,32 @@ else tar -czf "$SAVE_FOLDER/$FILENAME" -C "$BACKUP_FOLDER/.." -X $EXCLUEDS_FILE
# remove temporary file
rm $EXCLUEDS_FILE

# tar commmand ran successfully (exit code = 0)
# tar commmand finished successfully (exit code = 0)
if [ "$?" = 0 ]; then
echo -e "$(date +"%Y/%m/%d %T") ${C1}Backup file generated and saved successfully to ${C4}$SAVE_FOLDER/$FILENAME ($(du -h "$SAVE_FOLDER/$FILENAME" | cut -f1))${C0}"
# tar commmand didn't run successfully (exit code != 0)
else
echo -e "$(date +"%Y/%m/%d %T") ${C2}Backup file generation failed${C0}"
# if tar file was generated during the failed tar process, remove it
if [ -f "$SAVE_FOLDER/$FILENAME" ]; then
rm "$SAVE_FOLDER/$FILENAME"
exit 1
fi
if [ -f "$SAVE_FOLDER/$FILENAME" ]; then rm "$SAVE_FOLDER/$FILENAME"; fi
pushbullet "$PUSHBULLET_MESSAGE_FAIL"
exit 1
fi

# ----- rclone -----
if [ -n "$RCLONE_FOLDER" ]; then
echo -e "$(date +"%Y/%m/%d %T") ${C3}Uploading backup file to ${C4}$RCLONE_FOLDER${C0}"
# run rclone with or without '-v', according to VERBOSE value
# run rclone with or without '-v', according to VERBOSE value
if [ "$VERBOSE" = true ]; then rclone copy "$SAVE_FOLDER/$FILENAME" -v "$RCLONE_FOLDER"; else rclone copy "$SAVE_FOLDER/$FILENAME" "$RCLONE_FOLDER"; fi
# rclone commmand ran successfully (exit code = 0)
# rclone commmand finished successfully (exit code = 0)
if [ "$?" = 0 ]; then
echo -e "$(date +"%Y/%m/%d %T") ${C1}Backup file uploaded successfully to ${C4}$RCLONE_FOLDER${C0}"
# rclone command didn't run successfully (exit code != 0)
echo -e "$(date +"%Y/%m/%d %T") ${C1}Backup file uploaded successfully${C0}"
# rclone command didn't run successfully (exit code != 0)
else
ERROR=true
echo -e "$(date +"%Y/%m/%d %T") ${C2}Upload failed${C0}"

if [ "$REMOVE_LOCAL" = true ]; then
if [ "$REMOVE_LOCAL" = true ] && [ "$SKIP_WARNINGS" = false ]; then
read -p "WARNING: Would you like to continute and remove local copy of the backup file even though upload wasn't successful (Y/n) ? " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
Expand All @@ -181,12 +203,16 @@ if [ "$REMOVE_LOCAL" = true ]; then
echo -e "$(date +"%Y/%m/%d %T") ${C1}Local file removed${C0}"
fi

# print and exit using correct code according to ERROR's value
# print, send Pushbullet notification (if used), and exit using correct code according to ERROR's value
if [ "$ERROR" = false ]; then
echo -e "$(date +"%Y/%m/%d %T") ${C1}Script finished successfully${C0}"
# send Pushbullet notification, if used
pushbullet "$PUSHBULLET_MESSAGE_FINISHED_SUCCESS"
exit 0;

else
echo -e "$(date +"%Y/%m/%d %T") ${C2}Script finished with errors${C0}"
# send Pushbullet notification, if used
pushbullet "$PUSHBULLET_MESSAGE_FINISHED_ERRORS"
exit 1
fi

0 comments on commit 2dfe202

Please sign in to comment.