Add resend provider to send email script

This commit is contained in:
mr-vercetti
2025-06-21 14:23:49 +02:00
parent dbac55889d
commit f86ea3ee62
3 changed files with 184 additions and 100 deletions

View File

@@ -7,59 +7,84 @@ remote_path=""
email_from=""
email_name=""
email_to=""
send_email_script_path="/etc/sg-send-email.sh"
send_email_script_path="/etc/send-email.sh"
email_provider=""
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/sg-send-email.sh)"
echo "Usage: $0 -l LOCAL_PATH -r REMOTE_PATH -f EMAIL_FROM -n EMAIL_NAME -t EMAIL_TO -s SEND_EMAIL_SCRIPT_PATH -p EMAIL_PROVIDER"
echo ""
echo "Required:"
echo " -l LOCAL_PATH Local path to sync"
echo " -r REMOTE_PATH Remote path in format remote:bucket/path"
echo " -f EMAIL_FROM Sender's email address"
echo " -n EMAIL_NAME Sender's name"
echo " -t EMAIL_TO Recipient's email address"
echo " -p EMAIL_PROVIDER Email provider: resend or sendgrid"
echo ""
echo "Optional:"
echo " -s SEND_EMAIL_SCRIPT_PATH Path to the send email script (default: /etc/send-email.sh)"
}
while getopts "l:r:f:n:t:s:h" opt
while getopts "l:r:f:n:t:s:p: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}";;
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}" ;;
p) email_provider="${OPTARG}" ;;
h)
print_usage
exit 3
exit 0
;;
*)
print_usage
exit 1
;;
esac
done
# Check if script for sending emails is in place
if [[ ! -f $send_email_script_path ]]; then
# Validate required parameters
if [[ -z "$local_path" || -z "$remote_path" || -z "$email_from" || -z "$email_name" || -z "$email_to" || -z "$email_provider" ]]; then
echo "Error: Missing required parameter(s)"
print_usage
exit 1
fi
# Validate email provider
if [[ "$email_provider" != "resend" && "$email_provider" != "sendgrid" ]]; then
echo "Error: Invalid email provider '$email_provider'. Use 'resend' or 'sendgrid'"
exit 1
fi
# Check if send-email script exists
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
# Run sync and capture start/end time and status
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
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}"
# Prepare email
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}"
email_message="Start time: ${start_time}\nEnd time: ${end_time}"
# Send email
"$send_email_script_path" \
-f "$email_from" \
-n "$email_name" \
-t "$email_to" \
-s "Backup sync to S3 status: $status" \
-m "$email_message" \
-p "$email_provider"