feat: rework aws-backup and add --install command

This commit is contained in:
2025-07-30 18:39:46 +02:00
parent 6398fd54b8
commit ad6239741e
3 changed files with 67 additions and 38 deletions

View File

@@ -91,7 +91,7 @@ done
info_print "\n\ info_print "\n\
==================================================\n\ ==================================================\n\
Installation Complete\n\ Installation Complete\n\
--------------------------------------------------" -- false --------------------------------------------------"
info_print "All config files are in $ETC_DIR" info_print "All config files are in $ETC_DIR"
info_print "All scripts are in $SCRIPT_FILE" info_print "All scripts are in $SCRIPT_FILE"

View File

@@ -1,40 +1,44 @@
#!/bin/bash #!/bin/bash
source /usr/local/bin/notifications.sh
source /usr/local/bin/common.sh source /usr/local/bin/common.sh
source /etc/serverconfig/.env
INSTALLED=$1
DIR="$(cd "$(dirname "$0")" && pwd)" DIR="$(cd "$(dirname "$0")" && pwd)"
BACKUP="$DIR/aws-bakup.bak" BACKUP="$ETC_DIR/aws-bakup.bak"
SYNCED_FILES="" if [[ "--install" == $INSTALLED ]]; then
info_print "\n\
==================================================\n\
AWS-backup Installation\n\
--------------------------------------------------"
read -p "Enter aws server: " AWS_client
create_env_variable "AWS" "$AWS_client"
touch "$BACKUP"
info_print "$BACKUP created."
if [[ "$1" == "clean" ]] then while true; do
info_print "Cleaning the S3 bucket: s3://$AWS" | tee -a "$LOG" read -p "Add backup directory or file name (key or leave empty to quit): " key
aws s3 rm "s3://$AWS/" --recursive | tee -a "$LOG" [[ -z "$key" ]] && break
read -p "Enter value for $key: " value
create_env_variable "$key" "$value" $BACKUP
done
info_print "You can add more later by editing $BACKUP."
if [ $? -ne 0 ]; then CRON_JOB="0 0 * * * $SCRIPT_FILE/aws-backup.sh"
info_print "Failed to clean the S3 bucket" 3 crontab -l | grep -F "$CRON_JOB" > /dev/null 2>&1
exit 1 if ! crontab -l | grep -Fq "$CRON_JOB"; then
fi (crontab -l 2>/dev/null; echo "$CRON_JOB") | crontab -
info_print "Cron job added." 6
info_print "Bucket cleaned successfully."
exit 0
else
info_print "Purge aws-bak files."
rm -f $BACKUP $LOG $DIR/aws-bak.sh
exit 0
fi fi
fi fi
if [ -n "$1" ]; then if [[ "$1" == "clean" ]] then
AWS="$1" info_print "Purge aws-bak files."
fi rm -f $BACKUP
exit 0
if [ ! -e "$BACKUP" ]; then
touch "$BACKUP"
info_print "$BACKUP created. Please only include dirnames." 5
exit 1
fi fi
while IFS= read -r SOURCE_PATH || [ -n "$SOURCE_PATH" ]; do while IFS= read -r SOURCE_PATH || [ -n "$SOURCE_PATH" ]; do
@@ -49,18 +53,7 @@ while IFS= read -r SOURCE_PATH || [ -n "$SOURCE_PATH" ]; do
info_print "Error while syncing $SOURCE_PATH to the AWS server." 3 info_print "Error while syncing $SOURCE_PATH to the AWS server." 3
exit 1 exit 1
fi fi
SYNCED_FILES="$SYNCED_FILES\n$SOURCE_PATH"
else else
info_print "$SOURCE_PATH not found or inaccessible." 3 info_print "$SOURCE_PATH not found or inaccessible." 3
fi fi
done < "$BACKUP" done < "$BACKUP"
if [ -n "$SYNCED_FILES" ]; then
info_print "Files synced:$SYNCED_FILES"
else
info_print "No files synced." 3
exit 0;
fi
info_print "All files synced to AWS."

View File

@@ -36,4 +36,40 @@ info_print() {
if [ "$write_log" = true ]; then echo -e "$output" | tee -a "$LOG" if [ "$write_log" = true ]; then echo -e "$output" | tee -a "$LOG"
else echo -e "$output" else echo -e "$output"
fi fi
}
create_env_variable() {
local key="$1"
local value="$2"
local file="${3:-$ENV_FILE}"
if [[ -z "$value" ]]; then
if grep -q "^$key=*" "$file" 2>/dev/null; then
info_print "$key not updated."
return
else
info_print "$key not set (empty input)."
return
fi
fi
if grep -q "^$key=" "$file" 2>/dev/null; then
if $AUTO_CONFIRM; then
yn="y"
else
read -p "$key already set, overwrite? (y/N): " yn
fi
case "$yn" in
[Yy]*)
sed -i "s/^$key=.*/$key=$value/" "$file"
info_print "$key updated."
;;
*)
info_print "$key not changed."
;;
esac
else
echo "$key=$value" >> "$file"
info_print "$key \e[32mset\e[0m."
fi
} }