From ae94c23b878121b9290948bb0cb6301a1e092a9e Mon Sep 17 00:00:00 2001
From: Joel Grunbaum <joelgrun@gmail.com>
Date: Sun, 09 May 2021 12:13:48 +0000
Subject: [PATCH] sync database with gdrive

---
 backup.sh |   79 +++++++++++++++++++++------------------
 1 files changed, 42 insertions(+), 37 deletions(-)

diff --git a/backup.sh b/backup.sh
index 546a867..83a2b73 100755
--- a/backup.sh
+++ b/backup.sh
@@ -2,37 +2,31 @@
 
 BACKUP_DIR="/backup/work/"
 FILES_DIR="/home/joel/OneDrive/"
-RCLONE_BACKUP=("OneDrive_Personal" "NextCloud" "GoogleDrive" "Mega" "unimelb" "sout" "pCloud")
-RCLONE_FILES=("sout" "Mega" "unimelb")
+RCLONE_BACKUP=("GoogleDrive" "Mega" "unimelb" "pCloud" "gdrive_unimelb" "NC_home" "NextCloud")
+RCLONE_FILES=("Mega" "unimelb" "gdrive_unimelb" "NC_home")
+EXCLUDES="--exclude '*~' --exclude '*#*' --exclude '.DS_Store' --exclude '*.7z' "
 
 function sync() {
-	if [ $2 != "p" ]; then
-		case $1 in
-			"sout")
-				rclone sync $BACKUP_DIR $1:/data/Cloud/backup $(! [ -z $3 ] && echo "-P")
-				;;
-			*)
-				rclone sync $BACKUP_DIR $1:/Uni/duplicacy  $(! [ -z $3 ] && echo "-P")
-				;;
-		esac
-	else
-		case $1 in
-			"sout")
-				rclone sync $FILES_DIR $1:/data/Cloud/files  $(! [ -z $3 ] && echo "-P")
-				;;
-			*)
-				rclone sync $FILES_DIR $1:/Uni/files  $(! [ -z $3 ] && echo "-P")
-				;;
-			esac
-	fi
+	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
-			sync $i &
+			#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
@@ -41,7 +35,7 @@
 			sync $i n p
 		done
 		for i in ${RCLONE_FILES[*]}; do
-			echo '\033[0;31m'$i files'\033[0m'
+			echo -e '\033[0;31m'$i files'\033[0m'
 			sync $i p p
 		done
 	fi
@@ -53,23 +47,34 @@
 	echo -e '\033[0;32m'Syncing'\033[0m'
 	sync_all p
 	exit
+elif [ "$1" = "cron" ]; then
+    borg create \
+	 --compression zstd,22 \
+	 $EXCLUDES \
+	 \
+	 $BACKUP_DIR::"$(date +%F+%R)" \
+	 $FILES_DIR
+else
+    borg create \
+	 --verbose \
+	 --stats \
+	 --compression zstd,22 \
+	 $EXCLUDES \
+	 \
+	 $BACKUP_DIR::"$(date +%F+%R)" \
+	 $FILES_DIR
 fi
 
-LAST="$(duplicacy list | tail -n1 | cut -d' '  -f4)"
-CURR="$(duplicacy backup | tail -n1 | sed 's/[^0-9]//g')"
-#LAST=2
-#CURR=4
-DIFF="$(duplicacy diff -r $CURR -r $LAST | grep '-')"
+borg prune \
+	 --keep-hourly 12 \
+	 --keep-daily 8 \
+	 --keep-monthly 2 \
+	 $BACKUP_DIR
 
-#echo "curr - $CURR"
-#echo "last - $LAST"
-#echo "diff - $DIFF"
-
-duplicacy prune -keep 7:30 -keep 1:7 -exclusive > /dev/null 2>&1
-
-if [ -z "$DIFF" ]; then
-	duplicacy prune -r $CURR -exclusive > /dev/null 2>&1
+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)
 else
-	echo "New backup $CURR"
+	echo "New backup $(echo $LAST | cut -d' ' -f2)"
 	sync_all
 fi

--
Gitblit v1.9.3