mirror of
				https://github.com/mr-vercetti/bash-scripts.git
				synced 2025-11-04 09:25:48 +01:00 
			
		
		
		
	Add resend provider to send email script
This commit is contained in:
		@@ -7,59 +7,84 @@ remote_path=""
 | 
				
			|||||||
email_from=""
 | 
					email_from=""
 | 
				
			||||||
email_name=""
 | 
					email_name=""
 | 
				
			||||||
email_to=""
 | 
					email_to=""
 | 
				
			||||||
send_email_script_path="/etc/sg-send-email.sh"
 | 
					send_email_script_path="/etc/send-email.sh"
 | 
				
			||||||
 | 
					email_provider=""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
print_usage () {
 | 
					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 "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 "LOCAL_PATH - local path to sync"
 | 
					   echo ""
 | 
				
			||||||
   echo "REMOTE_PATH - remote path in format remote:bucket/path"
 | 
					   echo "Required:"
 | 
				
			||||||
   echo "EMAIL_FROM - sender's email address"
 | 
					   echo "  -l LOCAL_PATH             Local path to sync"
 | 
				
			||||||
   echo "EMAIL_NAME - sender's name"
 | 
					   echo "  -r REMOTE_PATH            Remote path in format remote:bucket/path"
 | 
				
			||||||
   echo "EMAIL_TO - recipient's email adress"
 | 
					   echo "  -f EMAIL_FROM             Sender's email address"
 | 
				
			||||||
   echo "SEND_EMAIL_SCRIPT_PATH - a path to the send email script (default: /etc/sg-send-email.sh)"
 | 
					   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
 | 
					do
 | 
				
			||||||
   case $opt in
 | 
					   case $opt in
 | 
				
			||||||
      l)
 | 
					      l) local_path="${OPTARG}" ;;
 | 
				
			||||||
         local_path="${OPTARG}";;
 | 
					      r) remote_path="${OPTARG}" ;;
 | 
				
			||||||
      r)
 | 
					      f) email_from="${OPTARG}" ;;
 | 
				
			||||||
         remote_path="${OPTARG}";;
 | 
					      n) email_name="${OPTARG}" ;;
 | 
				
			||||||
      f)
 | 
					      t) email_to="${OPTARG}" ;;
 | 
				
			||||||
         email_from="${OPTARG}";;
 | 
					      s) send_email_script_path="${OPTARG}" ;;
 | 
				
			||||||
      n)
 | 
					      p) email_provider="${OPTARG}" ;;
 | 
				
			||||||
         email_name="${OPTARG}";;
 | 
					 | 
				
			||||||
      t)
 | 
					 | 
				
			||||||
         email_to="${OPTARG}";;
 | 
					 | 
				
			||||||
      s)
 | 
					 | 
				
			||||||
         send_email_script_path="${OPTARG}";;
 | 
					 | 
				
			||||||
      h)
 | 
					      h)
 | 
				
			||||||
         print_usage
 | 
					         print_usage
 | 
				
			||||||
         exit 3
 | 
					         exit 0
 | 
				
			||||||
 | 
					         ;;
 | 
				
			||||||
 | 
					      *)
 | 
				
			||||||
 | 
					         print_usage
 | 
				
			||||||
 | 
					         exit 1
 | 
				
			||||||
         ;;
 | 
					         ;;
 | 
				
			||||||
   esac
 | 
					   esac
 | 
				
			||||||
done
 | 
					done
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Check if script for sending emails is in place
 | 
					# Validate required parameters
 | 
				
			||||||
if [[ ! -f $send_email_script_path ]]; then
 | 
					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"
 | 
					    echo "Script \"$send_email_script_path\" does not exist"
 | 
				
			||||||
    exit 1
 | 
					    exit 1
 | 
				
			||||||
fi
 | 
					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')
 | 
					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="$?"
 | 
					exit_code="$?"
 | 
				
			||||||
end_time=$(date +'%Y-%m-%d %H:%M:%S')
 | 
					end_time=$(date +'%Y-%m-%d %H:%M:%S')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Send email notification with sync status
 | 
					# Prepare email
 | 
				
			||||||
email_message="Start time: ${start_time}\nEnd time: ${end_time}"
 | 
					 | 
				
			||||||
status="SUCCESSFUL"
 | 
					status="SUCCESSFUL"
 | 
				
			||||||
 | 
					 | 
				
			||||||
if [[ $exit_code -ne 0 ]]; then
 | 
					if [[ $exit_code -ne 0 ]]; then
 | 
				
			||||||
    status="FAILED"
 | 
					    status="FAILED"
 | 
				
			||||||
fi
 | 
					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"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										129
									
								
								send-email.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										129
									
								
								send-email.sh
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,129 @@
 | 
				
			|||||||
 | 
					#!/usr/bin/bash
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					script_dir="$(dirname "$0")"
 | 
				
			||||||
 | 
					default_resend_key_file="$script_dir/rs_key"
 | 
				
			||||||
 | 
					default_sendgrid_key_file="$script_dir/sg_key"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Default values
 | 
				
			||||||
 | 
					email_from=""
 | 
				
			||||||
 | 
					email_name=""
 | 
				
			||||||
 | 
					email_to=""
 | 
				
			||||||
 | 
					subject=""
 | 
				
			||||||
 | 
					message=""
 | 
				
			||||||
 | 
					provider=""
 | 
				
			||||||
 | 
					key_file=""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					print_usage () {
 | 
				
			||||||
 | 
					   echo "Usage: $0 -f EMAIL_FROM -n EMAIL_NAME -t EMAIL_TO -s SUBJECT -m MESSAGE -p PROVIDER [-k KEY_FILE]"
 | 
				
			||||||
 | 
					   echo ""
 | 
				
			||||||
 | 
					   echo "Required:"
 | 
				
			||||||
 | 
					   echo "  -f EMAIL_FROM     Sender's email address"
 | 
				
			||||||
 | 
					   echo "  -n EMAIL_NAME     Sender's name"
 | 
				
			||||||
 | 
					   echo "  -t EMAIL_TO       Recipient's email address"
 | 
				
			||||||
 | 
					   echo "  -s SUBJECT        Email subject"
 | 
				
			||||||
 | 
					   echo "  -m MESSAGE        Email message"
 | 
				
			||||||
 | 
					   echo "  -p PROVIDER       Email provider: 'resend' or 'sendgrid'"
 | 
				
			||||||
 | 
					   echo ""
 | 
				
			||||||
 | 
					   echo "Optional:"
 | 
				
			||||||
 | 
					   echo "  -k KEY_FILE       Path to API key file (defaults: ./rs_key for resend, ./sg_key for sendgrid)"
 | 
				
			||||||
 | 
					   echo ""
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					send_email_resend () {
 | 
				
			||||||
 | 
					   full_from="${email_name} <${email_from}>"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					   maildata='{
 | 
				
			||||||
 | 
					      "to": ["'"${email_to}"'"],
 | 
				
			||||||
 | 
					      "from": "'"${full_from}"'",
 | 
				
			||||||
 | 
					      "subject": "'"${subject}"'",
 | 
				
			||||||
 | 
					      "text": "'"${message}"'"
 | 
				
			||||||
 | 
					   }'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					   curl -X POST "https://api.resend.com/emails" \
 | 
				
			||||||
 | 
					      -H "Authorization: Bearer $api_key" \
 | 
				
			||||||
 | 
					      -H "Content-Type: application/json" \
 | 
				
			||||||
 | 
					      -d "$maildata"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					send_email_sendgrid () {
 | 
				
			||||||
 | 
					   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 $api_key" \
 | 
				
			||||||
 | 
					      -H "Content-Type: application/json" \
 | 
				
			||||||
 | 
					      -d "$maildata"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					while getopts "f:n:t:s:m:p:k:h" opt
 | 
				
			||||||
 | 
					do
 | 
				
			||||||
 | 
					   case $opt in
 | 
				
			||||||
 | 
					      f) email_from="${OPTARG}" ;;
 | 
				
			||||||
 | 
					      n) email_name="${OPTARG}" ;;
 | 
				
			||||||
 | 
					      t) email_to="${OPTARG}" ;;
 | 
				
			||||||
 | 
					      s) subject="${OPTARG}" ;;
 | 
				
			||||||
 | 
					      m) message="${OPTARG}" ;;
 | 
				
			||||||
 | 
					      p) provider="${OPTARG}" ;;
 | 
				
			||||||
 | 
					      k) key_file="${OPTARG}" ;;
 | 
				
			||||||
 | 
					      h)
 | 
				
			||||||
 | 
					         print_usage
 | 
				
			||||||
 | 
					         exit 0
 | 
				
			||||||
 | 
					         ;;
 | 
				
			||||||
 | 
					      *)
 | 
				
			||||||
 | 
					         print_usage
 | 
				
			||||||
 | 
					         exit 1
 | 
				
			||||||
 | 
					         ;;
 | 
				
			||||||
 | 
					   esac
 | 
				
			||||||
 | 
					done
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Validate required parameters
 | 
				
			||||||
 | 
					if [[ -z "$email_from" || -z "$email_name" || -z "$email_to" || -z "$subject" || -z "$message" || -z "$provider" ]]; then
 | 
				
			||||||
 | 
					   echo "Error: Missing required parameter(s)"
 | 
				
			||||||
 | 
					   print_usage
 | 
				
			||||||
 | 
					   exit 1
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Validate provider
 | 
				
			||||||
 | 
					if [[ "$provider" != "resend" && "$provider" != "sendgrid" ]]; then
 | 
				
			||||||
 | 
					   echo "Error: Invalid provider '$provider'. Use 'resend' or 'sendgrid'"
 | 
				
			||||||
 | 
					   exit 1
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Default key file per provider if not specified
 | 
				
			||||||
 | 
					if [[ -z "$key_file" ]]; then
 | 
				
			||||||
 | 
					   if [[ "$provider" == "resend" ]]; then
 | 
				
			||||||
 | 
					      key_file="$default_resend_key_file"
 | 
				
			||||||
 | 
					   else
 | 
				
			||||||
 | 
					      key_file="$default_sendgrid_key_file"
 | 
				
			||||||
 | 
					   fi
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Check key file validity
 | 
				
			||||||
 | 
					if [[ ! -f "$key_file" ]]; then
 | 
				
			||||||
 | 
					   echo "API key file \"$key_file\" does not exist"
 | 
				
			||||||
 | 
					   exit 1
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if [[ "$(stat -c "%a" "$key_file")" != "400" ]]; then
 | 
				
			||||||
 | 
					   echo "Unsafe API key file permissions (should be 400)"
 | 
				
			||||||
 | 
					   exit 1
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if [[ "$(wc -l < "$key_file")" != "1" ]]; then
 | 
				
			||||||
 | 
					   echo "Wrong API key format (file must contain exactly one line)"
 | 
				
			||||||
 | 
					   exit 1
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					api_key="$(cat "$key_file")"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Dispatch to appropriate send function
 | 
				
			||||||
 | 
					if [[ "$provider" == "resend" ]]; then
 | 
				
			||||||
 | 
					   send_email_resend
 | 
				
			||||||
 | 
					else
 | 
				
			||||||
 | 
					   send_email_sendgrid
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -1,70 +0,0 @@
 | 
				
			|||||||
#!/usr/bin/bash 
 | 
					 | 
				
			||||||
# Simple script to send email notifications via the Sendgrid API.
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# Default values
 | 
					 | 
				
			||||||
email_from=""
 | 
					 | 
				
			||||||
email_name=""
 | 
					 | 
				
			||||||
email_to=""
 | 
					 | 
				
			||||||
subject=""
 | 
					 | 
				
			||||||
message=""
 | 
					 | 
				
			||||||
sg_key_file="./sg_key"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
print_usage () {
 | 
					 | 
				
			||||||
   echo "Usage: "$0" -f EMAIL_FROM -n EMAIL_NAME -t EMAIL_TO -s SUBJECT -m MESSAGE [-k SG_KEY_FILE]"
 | 
					 | 
				
			||||||
   echo "EMAIL_FROM - sender's email address"
 | 
					 | 
				
			||||||
   echo "EMAIL_NAME - sender's name"
 | 
					 | 
				
			||||||
   echo "EMAIL_TO - recipient's email adress"
 | 
					 | 
				
			||||||
   echo "SUBJECT - email subject"
 | 
					 | 
				
			||||||
   echo "MESSAGE - email message"
 | 
					 | 
				
			||||||
   echo "SG_KEY_FILE - a path to the file with Sendgrid API key (default: ./sg_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" \
 | 
					 | 
				
			||||||
      -H "Content-Type: application/json" \
 | 
					 | 
				
			||||||
      -d "$maildata"
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
while getopts "f:n:t:s:m:k:h" opt
 | 
					 | 
				
			||||||
do
 | 
					 | 
				
			||||||
   case $opt in
 | 
					 | 
				
			||||||
      f)
 | 
					 | 
				
			||||||
         email_from="${OPTARG}";;
 | 
					 | 
				
			||||||
      n)
 | 
					 | 
				
			||||||
         email_name="${OPTARG}";;
 | 
					 | 
				
			||||||
      t)
 | 
					 | 
				
			||||||
         email_to="${OPTARG}";;
 | 
					 | 
				
			||||||
      s)
 | 
					 | 
				
			||||||
         subject="${OPTARG}";;
 | 
					 | 
				
			||||||
      m)
 | 
					 | 
				
			||||||
         message="${OPTARG}";;
 | 
					 | 
				
			||||||
      k)
 | 
					 | 
				
			||||||
         sg_key_file="${OPTARG}";;
 | 
					 | 
				
			||||||
      h)
 | 
					 | 
				
			||||||
         print_usage
 | 
					 | 
				
			||||||
         exit 3
 | 
					 | 
				
			||||||
         ;;
 | 
					 | 
				
			||||||
   esac
 | 
					 | 
				
			||||||
done
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
if [[ ! -f $sg_key_file ]]; then
 | 
					 | 
				
			||||||
    echo "API key file \"$sg_key_file\" does not exist"
 | 
					 | 
				
			||||||
    exit 1
 | 
					 | 
				
			||||||
fi
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
if [[ "$(stat -c "%a" $sg_key_file)" != "400" ]]; then
 | 
					 | 
				
			||||||
    echo "Unsafe API key file permissions"
 | 
					 | 
				
			||||||
    exit 1
 | 
					 | 
				
			||||||
fi
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
if [[ "$(wc -l $sg_key_file | cut -d" " -f1)" != "1" ]]; then
 | 
					 | 
				
			||||||
   echo "Wrong API key format"
 | 
					 | 
				
			||||||
   exit 1
 | 
					 | 
				
			||||||
fi
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
sg_key="$(cat $sg_key_file)"
 | 
					 | 
				
			||||||
send_email
 | 
					 | 
				
			||||||
		Reference in New Issue
	
	Block a user