Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions immutable-backups.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@

set -e

function do_rclone() {
"$@"
ret=$?
if [[ $ret -eq 0 ]]
then
if [ "${verbose_flag}" != "" ]
then
echo "Successfully ran [ $@ ]"
fi
else
echo "Error: Command [ $@ ] returned $ret"
fi
}

function showHelpCommands {
echo ""
echo "Usage: $(basename $0) [command]"
Expand Down Expand Up @@ -33,6 +47,7 @@ function showHelpBackup {
echo " optional:"
echo ""
echo " --incremental If specified, the backup is incremental instead of full"
echo " --rclone-params If specified, these parameters will be applied to the rclone call"
echo " --dry-run If specified, dont copy anything, just test"
echo " --verbose If specified, output is more verbose"
echo ""
Expand All @@ -43,6 +58,9 @@ function showHelpBackup {
echo " # Take an full backup of a local directory to offsite"
echo " $(basename $0) backup --local=/path/to/files --remote=your-remote: --dry-run"
echo ""
echo " # Take full backup with rclone parameters, with progress bar and upload limit to 1M and download limit to unlimited"
echo " $(basename $0) backup --local=/path/to/files --remote=your-remote: --rclone-params=\" -P --bwlimit 1M:off\""
echo ""
echo "Notes:"
echo " * This requires rclone to be installed and the remotes to be configured on this system. See the rclone"
echo " docs for more info: https://rclone.org/"
Expand All @@ -63,6 +81,7 @@ function showHelpRestore {
echo ""
echo " --date A date in the format Y-M-d-Hms to restore, default is to use the latest backup date found"
echo " --incremental Should be specified if the backup being restored was incremental"
echo " --rclone-params If specified, these parameters will be applied to the rclone call"
echo " --dry-run If specified, dont copy anything, just test"
echo " --verbose If specified, output is more verbose"
echo ""
Expand All @@ -73,6 +92,9 @@ function showHelpRestore {
echo " # Restore the latest version of a full backup"
echo " $(basename $0) restore --remote=your-remote: --local=/path/to/put/restored --dry-run"
echo ""
echo " # Restore the latest version of a full backup with rclone parameters, with progress bar and upload limit to 1M and download limit to 10M"
echo " $(basename $0) restore --local=/path/to/files --remote=your-remote: --rclone-params=\" -P --bwlimit 1M:10M\""
echo ""
echo " # Restore a specific point in time incremental backup"
echo " $(basename $0) restore --remote=your-remote: --local=/path/to/put/restored --date=2021-04-21-183015 --dry-run"
echo ""
Expand All @@ -90,6 +112,7 @@ function showHelpRestore {
# Set default options
operation=""
incremental=false
rclone_params=""
local=""
remote=""
date=""
Expand Down Expand Up @@ -117,6 +140,11 @@ if [ "$operation" == "backup" ]; then
export incremental
shift
;;
--rclone-params*)
rclone_params=$(echo $1 | sed -e 's/^[^=]*=//g')
export rclone_params
shift
;;
--dry-run*)
dry_run_flag="--dry-run"
export dry_run_flag
Expand Down Expand Up @@ -169,12 +197,12 @@ if [ "$operation" == "backup" ]; then
compareDestFlags[i]=" --compare-dest=${remote}/incremental/${path}"
done

$RCLONE_BIN copy --no-traverse --immutable ${local} ${remote}/incremental/$(date +"%Y-%m-%d-%H%M%S") ${dry_run_flag} ${compareDestFlags[*]} ${verbose_flag}
do_rclone $RCLONE_BIN copy --no-traverse --immutable ${local} ${remote}/incremental/$(date +"%Y-%m-%d-%H%M%S") ${dry_run_flag} ${compareDestFlags[*]} ${verbose_flag} ${rclone_params}

else
echo "$(date) Starting full backup"
echo "$(date) Starting full backup"

$RCLONE_BIN copy --no-traverse --immutable ${local} ${remote}/full/$(date +"%Y-%m-%d-%H%M%S") ${dry_run_flag} ${verbose_flag}
do_rclone $RCLONE_BIN copy --no-traverse --immutable ${local} ${remote}/full/$(date +"%Y-%m-%d-%H%M%S") ${dry_run_flag} ${verbose_flag} ${rclone_params}
fi
elif [ "$operation" == "restore" ]; then

Expand All @@ -188,6 +216,11 @@ elif [ "$operation" == "restore" ]; then
export incremental
shift
;;
--rclone-params*)
rclone_params=$(echo $1 | sed -e 's/^[^=]*=//g')
export rclone_params
shift
;;
--dry-run*)
dry_run_flag="--dry-run"
export dry_run_flag
Expand Down Expand Up @@ -296,7 +329,7 @@ elif [ "$operation" == "restore" ]; then

echo "$(date) processing backup: $prevBackup $counter/$cnt ($percentComplete%)"

$RCLONE_BIN copy --no-traverse ${remote}/incremental/${prevBackup} ${local} ${dry_run_flag} ${verbose_flag}
do_rclone $RCLONE_BIN copy --no-traverse ${remote}/incremental/${prevBackup} ${local} ${dry_run_flag} ${verbose_flag} ${rclone_params}
done

else
Expand Down Expand Up @@ -337,7 +370,7 @@ elif [ "$operation" == "restore" ]; then
remote="$remote/full/${prevBackups[0]}"
fi

$RCLONE_BIN copy --no-traverse --immutable ${remote} ${local} ${dry_run_flag} ${verbose_flag}
do_rclone $RCLONE_BIN copy --no-traverse --immutable ${remote} ${local} ${dry_run_flag} ${verbose_flag} ${rclone_params}
fi
else
# Invalid operation
Expand Down