initial commit

This commit is contained in:
Pratik
2019-10-05 21:12:09 +05:30
commit 98f3047381

122
sync-all-backups-with-log.sh Executable file
View File

@@ -0,0 +1,122 @@
#!/bin/sh
if [ -z "$1" ]
then
sync_freq_hours=24
else
sync_freq_hours=$1
fi
readonly sync_freq_hours
if [ -z "$2" ]
then
backup_dir=/backups/borg/
else
backup_dir=$2
fi
readonly backup_dir
# Exit if no backup directory exists
if [ ! -d "$backup_dir" ]; then
echo "Specified backup directory ($backup_dir) does not exist."
echo "Provide a valid directory."
exit 2
fi
if [ -z "$3" ]
then
log_dir=/backups/log/
else
log_dir=$3
fi
readonly log_dir
if [ ! -d "$log_dir" ]; then
echo "Specified log directory ($log_dir) does not exist."
echo "Provide a valid log directory."
exit 2
fi
if [ -z "$4" ]
then
log_file_prefix=$(cat /proc/sys/kernel/hostname)-sync-all-
else
log_file_prefix=$4
fi
readonly log_file_prefix
# if borg is currently running - stop sync
if [ $(pidof borg | wc -w) -eq 1 ]
then
echo "Backup is currently in process - exiting sync."
exit 2
fi
# set up log file
log_file_name="$log_file_prefix$(date +%d%m%Y-%H%M%S)"
log_file_path="$log_dir$log_file_name"
readonly log_file_name
readonly log_file_path
# check log files to determine if a successful sync happened in last 24 hours
sync_freq_minutes=$(($sync_freq_hours * 60))
if [ $(find $log_dir -name "$log_file_prefix*" -mmin -$sync_freq_minutes -exec awk 'END {print $NF}' {} \; | grep 0 | wc -l) -gt 0 ]
then
echo "Sync was successfully run within last $sync_freq_hours hours."
echo "Sync would exit now without doing anything."
exit 0
fi
# some helpers and error handling:
info() { printf "\n%s %s\n\n" "$( date )" "$*" &>> $log_file_path; }
trap 'echo $( date ) Sync interrupted &>> $log_file_path; exit 2' INT TERM
info "Starting sync to pcloud over rclone"
sync_target=mega-gmail
sync_target_dir=DesktopBackups/Manjaro/
rclone copy \
borg-local:$backup_dir \
$sync_target:$sync_target_dir \
--checksum \
--delete-after \
--verbose \
--log-file $log_file_path \
# mega-sync $backup_dir $sync_target_dir -vv
sync_exit=$?
info "Syncing Log Files"
rclone copy \
borg-local:$log_dir \
$sync_target:$sync_target_dir"backup-logs" \
--checksum \
--delete-after \
--local-no-check-updated \
--verbose \
--exclude $log_file_name \
--log-file $log_file_path \
# mega-sync $log_dir $sync_target_dir"backup-logs" -vv
log_sync_exit=$?
# use highest exit code as global exit code
global_exit=$(( sync_exit > log_sync_exit ? sync_exit : log_sync_exit ))
if [ ${global_exit} -eq 1 ];
then
echo "Backup Sync and/or Log Sync finished with a warning" &>> $log_file_path
fi
if [ ${global_exit} -gt 1 ];
then
echo "Backup Sync and/or Log Sync finished with an error" &>> $log_file_path
fi
echo Sync status - $global_exit &>> $log_file_path
exit ${global_exit}