mirror of
https://github.com/guezoloic/serverconfig.git
synced 2026-01-25 03:34:22 +00:00
feat: rework aws-backup and add --install command
This commit is contained in:
@@ -91,7 +91,7 @@ done
|
||||
info_print "\n\
|
||||
==================================================\n\
|
||||
Installation Complete\n\
|
||||
--------------------------------------------------" -- false
|
||||
--------------------------------------------------"
|
||||
info_print "All config files are in $ETC_DIR"
|
||||
info_print "All scripts are in $SCRIPT_FILE"
|
||||
|
||||
|
||||
@@ -1,40 +1,44 @@
|
||||
#!/bin/bash
|
||||
|
||||
source /usr/local/bin/notifications.sh
|
||||
source /usr/local/bin/common.sh
|
||||
source /etc/serverconfig/.env
|
||||
|
||||
INSTALLED=$1
|
||||
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"
|
||||
|
||||
if [[ "$1" == "clean" ]] then
|
||||
info_print "Cleaning the S3 bucket: s3://$AWS" | tee -a "$LOG"
|
||||
aws s3 rm "s3://$AWS/" --recursive | tee -a "$LOG"
|
||||
touch "$BACKUP"
|
||||
info_print "$BACKUP created."
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
info_print "Failed to clean the S3 bucket" 3
|
||||
exit 1
|
||||
fi
|
||||
while true; do
|
||||
read -p "Add backup directory or file name (key or leave empty to quit): " key
|
||||
[[ -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."
|
||||
|
||||
info_print "Bucket cleaned successfully."
|
||||
exit 0
|
||||
else
|
||||
info_print "Purge aws-bak files."
|
||||
rm -f $BACKUP $LOG $DIR/aws-bak.sh
|
||||
exit 0
|
||||
CRON_JOB="0 0 * * * $SCRIPT_FILE/aws-backup.sh"
|
||||
crontab -l | grep -F "$CRON_JOB" > /dev/null 2>&1
|
||||
if ! crontab -l | grep -Fq "$CRON_JOB"; then
|
||||
(crontab -l 2>/dev/null; echo "$CRON_JOB") | crontab -
|
||||
info_print "Cron job added." 6
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ -n "$1" ]; then
|
||||
AWS="$1"
|
||||
fi
|
||||
|
||||
if [ ! -e "$BACKUP" ]; then
|
||||
touch "$BACKUP"
|
||||
info_print "$BACKUP created. Please only include dirnames." 5
|
||||
exit 1
|
||||
if [[ "$1" == "clean" ]] then
|
||||
info_print "Purge aws-bak files."
|
||||
rm -f $BACKUP
|
||||
exit 0
|
||||
fi
|
||||
|
||||
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
|
||||
exit 1
|
||||
fi
|
||||
|
||||
SYNCED_FILES="$SYNCED_FILES\n$SOURCE_PATH"
|
||||
else
|
||||
info_print "$SOURCE_PATH not found or inaccessible." 3
|
||||
fi
|
||||
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."
|
||||
@@ -37,3 +37,39 @@ info_print() {
|
||||
else echo -e "$output"
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user