Compare commits

..

3 Commits

2 changed files with 97 additions and 26 deletions

64
s3-sync-backup.sh Executable file
View File

@ -0,0 +1,64 @@
#!/usr/bin/bash
# Script for syncing backup to remote object storage (S3 based) using rclone.
# Default values
local_path=""
remote_path=""
email_from=""
email_name=""
email_to=""
send_email_script_path=""
print_usage () {
echo "Usage: "$0" -l LOCAL_PATH -r REMOTE_PATH -f EMAIL_FROM -n EMAIL_NAME -t EMAIL_TO -s SEND_EMAIL_SCRIPT_PATH"
echo "LOCAL_PATH - local path to sync"
echo "REMOTE_PATH - remote path in format remote:bucket/path"
echo "EMAIL_FROM - sender's email address"
echo "EMAIL_NAME - sender's name"
echo "EMAIL_TO - recipient's email adress"
echo "SEND_EMAIL_SCRIPT_PATH - a path to the send email script (default: /etc/send-email.sh)"
}
while getopts "l:r:f:n:t:s:h" opt
do
case $opt in
l)
local_path="${OPTARG}";;
r)
remote_path="${OPTARG}";;
f)
email_from="${OPTARG}";;
n)
email_name="${OPTARG}";;
t)
email_to="${OPTARG}";;
s)
send_email_script_path="${OPTARG}";;
h)
print_usage
exit 3
;;
esac
done
# Check if script for sending emails is in place
if [[ ! -f $send_email_script_path ]]; then
echo "Script \"$send_email_script_path\" does not exist"
exit 1
fi
# Run script and capture time and exit code
start_time=$(date +'%Y-%m-%d %H:%M:%S')
rclone sync ${local_path} ${remote_path} --log-file=/var/log/rclone-backup.log --log-level INFO
exit_code="$?"
end_time=$(date +'%Y-%m-%d %H:%M:%S')
# Send email notification with sync status
email_message="Start time: ${start_time}\nEnd time: ${end_time}"
status="SUCCESSFUL"
if [[ $exit_code -ne 0 ]]; then
status="FAILED"
fi
${send_email_script_path} -f ${email_from} -n ${email_name} -t ${email_to} -s "Backup sync to S3 status: ${status}" -m "${email_message}"

View File

@ -1,62 +1,69 @@
#!/usr/bin/bash
# Simple script to send email notifications via the Sendgrid API.
#!/usr/bin/bash
sg_key_file="./sg_key"
# Default values
email_from=""
email_name=""
email_to=""
subject=""
message=""
script_dir="$(dirname $0)"
key_file="$script_dir/email_api_key"
print_usage () {
echo "Usage: "$0" -f EMAIL_FROM -n EMAIL_NAME -t EMAIL_TO -s SUBJECT -m MESSAGE [-k SG_KEY_FILE]"
echo "Usage: "$0" -f EMAIL_FROM -n EMAIL_NAME -t EMAIL_TO -s SUBJECT -m MESSAGE [-k KEY_FILE]"
echo "EMAIL_FROM - sender's email address"
echo "EMAIL_NAME - sender's name"
echo "EMAIL_TO - recipient's email adress"
echo "SG_KEY_FILE - a path to the file with Sendgrid API key (default: ./sg_key)"
echo "KEY_FILE - a path to the file with API key (default: ./email_api_key)"
}
send_email () {
maildata='{"personalizations": [{"to": [{"email": "'${email_to}'"}]}],"from": {"email": "'${email_from}'",
"name": "'${email_name}'"},"subject": "'${subject}'","content": [{"type": "text/plain", "value": "'${message}'"}]}'
curl -X "POST" "https://api.sendgrid.com/v3/mail/send" \
-H "Authorization: Bearer $sg_key" \
curl -X "POST" "https://api.resend.com/emails" \
-H "Authorization: Bearer $api_key" \
-H "Content-Type: application/json" \
-d "$maildata"
}
while getopts "f:n:t:s:m:k:h" opt
while getopts "k:t:n:s:m:f:h" opt
do
case $opt in
f)
email_from="${OPTARG}";;
n)
email_name="${OPTARG}";;
k)
key_file="${OPTARG}";;
t)
email_to="${OPTARG}";;
n)
email_name="${OPTARG}";;
s)
subject="${OPTARG}";;
m)
message="${OPTARG}";;
k)
sg_key_file="${OPTARG}";;
f)
email_from="${OPTARG}";;
h)
print_usage
exit 3
;;
print_usage
exit 0;
;;
esac
done
if [[ ! -f $sg_key_file ]]; then
echo "API key file \"$sg_key_file\" does not exist"
exit 1
if [[ ! -f $key_file ]]; then
echo "API key file "${OPTARG}" does not exist"
exit 1;
fi
if [[ "$(stat -c "%a" $sg_key_file)" != "400" ]]; then
if [[ "$(stat -c "%a" $key_file)" != "400" ]]; then
echo "Unsafe API key file permissions"
exit 1
exit 1;
fi
if [[ "$(wc -l $sg_key_file | cut -d" " -f1)" != "1" ]]; then
echo "Wrong API key format"
exit 1
if [[ "$(wc -l $key_file | cut -d" " -f1)" != 1 ]]; then
echo "Wrong API key file format"
exit 1;
fi
sg_key="$(cat $sg_key_file)"
api_key="$(cat $key_file)"
send_email