From 5ce9b0c04ee5b5e9dc5b2ae9b99a70a4857ae666 Mon Sep 17 00:00:00 2001 From: Joel Grunbaum <joelgrun@gmail.com> Date: Tue, 31 Aug 2021 06:18:52 +0000 Subject: [PATCH] added boilerplate for cronjob --- backup.sh | 99 ++++++++++++++++++++++++------------------------- 1 files changed, 49 insertions(+), 50 deletions(-) diff --git a/backup.sh b/backup.sh index 5cbe8b5..fd5e64e 100755 --- a/backup.sh +++ b/backup.sh @@ -2,80 +2,79 @@ BACKUP_DIR="/backup/work/" FILES_DIR="/home/joel/OneDrive/" -RCLONE_BACKUP=("GoogleDrive" "Mega" "unimelb" "pCloud" "gdrive_unimelb" "NC_home" "NextCloud") -RCLONE_FILES=("Mega" "unimelb" "gdrive_unimelb" "NC_home") +RCLONE_BACKUP=("GoogleDrive" "Mega" "unimelb" "pCloud" "gdrive_unimelb") +RCLONE_FILES=("Mega" "unimelb" "gdrive_unimelb") +EXCLUDES="/home/joel/Scripts/backup_patterns.txt" function sync() { - case $1 in - "sout") - rclone sync $([ $2 != "p" ] && echo "$BACKUP_DIR" || echo "$FILES_DIR") \ - $1:/data/Cloud/$([ $2 != "p" ] && echo "backup" || echo "files") $(! [ -z $3 ] && echo "-P") - ;; - *) - rclone sync $([ $2 != "p" ] && echo "$BACKUP_DIR" || echo "$FILES_DIR") \ - $1:/Uni/$([ $2 != "p" ] && echo "borg" || echo "files") $(! [ -z $3 ] && echo "-P") - ;; - esac + case $1 in + "sout") + rclone sync $([ $2 != "p" ] && echo "$BACKUP_DIR" || echo "$FILES_DIR") \ + $1:/data/Cloud/$([ $2 != "p" ] && echo "backup" || echo "files") $(! [ -z $3 ] && echo "-P") + ;; + *) + rclone sync $([ $2 != "p" ] && echo "$BACKUP_DIR" || echo "$FILES_DIR") \ + $1:/Uni/$([ $2 != "p" ] && echo "borg" || echo "files") $(! [ -z $3 ] && echo "-P") + ;; + esac } function sync_all() { - if [ -z $1 ]; then - for i in ${RCLONE_BACKUP[*]}; do - #echo -e '\033[0;31m'$i backup'\033[0m' - sync $i n & - done - for i in ${RCLONE_FILES[*]}; do - #echo -e '\033[0;31m'$i files'\033[0m' - sync $i p & - done - else - for i in ${RCLONE_BACKUP[*]}; do - echo -e '\033[0;31m'$i backup'\033[0m' - sync $i n p - done - for i in ${RCLONE_FILES[*]}; do - echo -e '\033[0;31m'$i files'\033[0m' - sync $i p p - done - fi + if [ -z $1 ]; then + for i in ${RCLONE_BACKUP[*]}; do + #echo -e '\033[0;31m'$i backup'\033[0m' + sync $i n & + done + for i in ${RCLONE_FILES[*]}; do + #echo -e '\033[0;31m'$i files'\033[0m' + sync $i p & + done + else + for i in ${RCLONE_BACKUP[*]}; do + echo -e '\033[0;31m'$i backup'\033[0m' + sync $i n p + done + for i in ${RCLONE_FILES[*]}; do + echo -e '\033[0;31m'$i files'\033[0m' + sync $i p p + done + fi } -cd /home/joel/OneDrive - if [ "$1" = "sync" ]; then - echo -e '\033[0;32m'Syncing'\033[0m' - sync_all p - exit + echo -e '\033[0;32m'Syncing'\033[0m' + sync_all p + exit elif [ "$1" = "cron" ]; then - borg create \ + borg create \ --compression zstd,22 \ - --exclude '*~' \ - --exclude '*#*' \ + --exclude-from $EXCLUDES \ \ $BACKUP_DIR::"$(date +%F+%R)" \ $FILES_DIR else - borg create \ + borg create \ --verbose \ --stats \ --compression zstd,22 \ - --exclude '*~' \ - --exclude '*#*' \ + --exclude-from $EXCLUDES \ \ $BACKUP_DIR::"$(date +%F+%R)" \ $FILES_DIR fi borg prune \ - --keep-hourly 12 \ - --keep-daily 8 \ - --keep-monthly 2 \ - $BACKUP_DIR + --keep-hourly 12 \ + --keep-daily 8 \ + --keep-monthly 2 \ + $BACKUP_DIR LAST="$(borg list $BACKUP_DIR | tail -n2 | cut -d' ' -f1)" -if [ -z "$(borg diff $BACKUP_DIR::$(echo $LAST | cut -d' ' -f1) $(echo $LAST | cut -d' ' -f2))" ]; then - borg delete $BACKUP_DIR::$(echo $LAST | cut -d' ' -f2) +TOP="$(echo $LAST | cut -d' ' -f1)" +BOTTOM="$(echo $LAST | cut -d' ' -f2)" +if [ -z "$(borg diff $BACKUP_DIR::$TOP $BOTTOM)" ]; then + borg delete $BACKUP_DIR::$(echo $LAST | cut -d' ' -f2) else - echo "New backup $(echo $LAST | cut -d' ' -f2)" - sync_all + echo "New backup $(echo $LAST | cut -d' ' -f2)" + sync_all fi -- Gitblit v1.9.3