From f55d64f86c99a83e7fdaed676cf516867512205c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20GUEZO?= Date: Wed, 4 Dec 2024 17:49:19 +0100 Subject: [PATCH] add clean option --- aws-backup/aws-bak.sh | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/aws-backup/aws-bak.sh b/aws-backup/aws-bak.sh index fa38eec..6cc61a2 100644 --- a/aws-backup/aws-bak.sh +++ b/aws-backup/aws-bak.sh @@ -3,7 +3,8 @@ # change to your aws-server name AWS="" -BACKUP="$(cd "$(dirname "$0")" && pwd)/aws-bakup.bak" +DIR="$(cd "$(dirname "$0")" && pwd)" +BACKUP="$DIR/aws-bakup.bak" LOG="/var/log/aws-bak.log" SYNCED_FILES="" @@ -21,6 +22,25 @@ error_print() { fi } +if [ "$1" == "clean" ]; then + if [ "$2" == "aws" ]; then + echo "Cleaning the S3 bucket: s3://$AWS" | tee -a "$LOG" + aws s3 rm "s3://$AWS/" --recursive | tee -a "$LOG" + + if [ $? -ne 0 ]; then + error_print true "Failed to clean the S3 bucket" + exit 1 + fi + + echo "Bucket cleaned successfully." | tee -a "$LOG" + exit 0 + else + echo "Purge aws-bak files." + rm -f $BACKUP $LOG $DIR/aws-bak.sh + exit 0 + fi +fi + if [ -n "$1" ]; then AWS="$1" fi @@ -38,7 +58,7 @@ while IFS= read -r SOURCE_PATH || [ -n "$SOURCE_PATH" ]; do fi if [ -d "$SOURCE_PATH" ] || [ -f "$SOURCE_PATH" ]; then - aws s3 sync "$SOURCE_PATH" "s3://$AWS/$(basename "$SOURCE_PATH")" --delete + aws s3 sync "$SOURCE_PATH" "s3://$AWS/$(basename "$SOURCE_PATH")" --delete > /dev/null 2>&1 if [ $? -ne 0 ]; then error_print true "$SOURCE_PATH"