Skip to content

Instantly share code, notes, and snippets.

@dazeb
Last active January 25, 2024 17:40
Show Gist options
  • Select an option

  • Save dazeb/c0269f4a1ad79e64850332e23614eaa6 to your computer and use it in GitHub Desktop.

Select an option

Save dazeb/c0269f4a1ad79e64850332e23614eaa6 to your computer and use it in GitHub Desktop.
BASH Script for PostalServer.io Installation - DNS RECORDS
#!/bin/bash
# Ask the user for their Cloudflare API key, email, and zone
read -p "Enter your Cloudflare API key: " API_KEY
read -p "Enter your Cloudflare email: " EMAIL
read -p "Enter your Cloudflare zone: " ZONE
# Get the IP addresses
IPV4=$(curl -s -X GET -4 https://ifconfig.co)
IPV6=$(curl -s -X GET -6 https://ifconfig.co)
# Set the DNS records
POSTAL_RECORD="postal.$ZONE"
SPF_RECORD="spf.postal.$ZONE"
RP_RECORD="rp.postal.$ZONE"
ROUTE_RECORD="routes.postal.$ZONE"
# Function to create a DNS record and validate the response
create_dns_record() {
local RECORD_TYPE=$1
local NAME=$2
local CONTENT=$3
local TTL=$4
local PROXYED=$5
# Create the DNS record
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE/dns_records" \
-H "Content-Type: application/json" \
-H "X-Auth-Email: $EMAIL" \
-H "Authorization: Bearer $API_KEY" \
--data "{\"type\":\"$RECORD_TYPE\",\"name\":\"$NAME\",\"content\":\"$CONTENT\",\"ttl\":$TTL,\"proxied\":$PROXYED}")
# Validate the response
if [ $RESPONSE -eq 200 ]; then
echo -e "\033[0;32mSuccessfully created $NAME record.\033[0m"
else
echo -e "\033[0;31mFailed to create $NAME record.\033[0m"
exit 1
fi
}
# Create the A records
echo -e "\n\nCreating A records for $POSTAL_RECORD..."
create_dns_record "A" "$POSTAL_RECORD" "$IPV4" 1 false
create_dns_record "AAAA" "$POSTAL_RECORD" "$IPV6" 1 false
# Create the SPF record
echo -e "\n\nCreating SPF record for $SPF_RECORD..."
create_dns_record "TXT" "$SPF_RECORD" "v=spf1 ip4:$IPV4 ip6:$IPV6 ~all" 1 false
# Create the Return Path records
echo -e "\n\nCreating Return Path records for $RP_RECORD..."
create_dns_record "A" "$RP_RECORD" "$IPV4" 1 false
create_dns_record "AAAA" "$RP_RECORD" "$IPV6" 1 false
create_dns_record "TXT" "$RP_RECORD" "v=spf1 a mx include:spf.postal.$ZONE ~all" 1 false
# Create the Route domain record
echo -e "\n\nCreating Route domain record for $ROUTE_RECORD..."
create_dns_record "MX" "$ROUTE_RECORD" "10 postal.$ZONE" 1 false
# Final message
echo -e "\n\nAll DNS entries have been successfully completed. You can now proceed with the rest of the Postal setup. Remember to enter the record names you chose in your postal.yml configuration file."
#!/bin/bash
# Check if the script is being run as root
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit
fi
# Update packages
echo -e "\nUpdating system packages..."
apt update && apt upgrade -y
# Install necessary system utilities
echo -e "\nInstalling system utilities..."
apt install -y git curl jq
# Clone the Postal installation helper repository
echo -e "\nCloning Postal installation helper repository..."
git clone https://postalserver.io/start/install /opt/postal/install
ln -s /opt/postal/install/bin/postal /usr/bin/postal
# Run MariaDB in a container
echo -e "\nRunning MariaDB in a container..."
docker run -d \
--name postal-mariadb \
-p 127.0.0.1:3306:3306 \
--restart always \
-e MARIADB_DATABASE=postal \
-e MARIADB_ROOT_PASSWORD=postal \
mariadb
# Run RabbitMQ in a container
echo -e "\nRunning RabbitMQ in a container..."
docker run -d \
--name postal-rabbitmq \
-p 127.0.0.1:5672:5672 \
--restart always \
-e RABBITMQ_DEFAULT_USER=postal \
-e RABBITMQ_DEFAULT_PASS=postal \
-e RABBITMQ_DEFAULT_VHOST=postal \
rabbitmq:3.8
# Ask the user for their Cloudflare API key, email, and zone
read -p "Enter your Cloudflare API key: " API_KEY
read -p "Enter your Cloudflare email: " EMAIL
read -p "Enter your Cloudflare zone: " ZONE
# Get the IP addresses
IPV4=$(curl -s -X GET -4 https://ifconfig.co)
IPV6=$(curl -s -X GET -6 https://ifconfig.co)
# Set the DNS records
POSTAL_RECORD="postal.$ZONE"
SPF_RECORD="spf.postal.$ZONE"
RP_RECORD="rp.postal.$ZONE"
ROUTE_RECORD="routes.postal.$ZONE"
# Function to create a DNS record and validate the response
create_dns_record() {
local RECORD_TYPE=$1
local NAME=$2
local CONTENT=$3
local TTL=$4
local PROXYED=$5
# Create the DNS record
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE/dns_records" \
-H "Content-Type: application/json" \
-H "X-Auth-Email: $EMAIL" \
-H "Authorization: Bearer $API_KEY" \
--data "{\"type\":\"$RECORD_TYPE\",\"name\":\"$NAME\",\"content\":\"$CONTENT\",\"ttl\":$TTL,\"proxied\":$PROXYED}")
# Validate the response
if [ $RESPONSE -eq 200 ]; then
echo -e "\033[0;32mSuccessfully created $NAME record.\033[0m"
else
echo -e "\033[0;31mFailed to create $NAME record.\033[0m"
exit 1
fi
}
# Create the A records
echo -e "\n\nCreating A records for $POSTAL_RECORD..."
create_dns_record "A" "$POSTAL_RECORD" "$IPV4" 1 false
create_dns_record "AAAA" "$POSTAL_RECORD" "$IPV6" 1 false
# Create the SPF record
echo -e "\n\nCreating SPF record for $SPF_RECORD..."
create_dns_record "TXT" "$SPF_RECORD" "v=spf1 ip4:$IPV4 ip6:$IPV6 ~all" 1 false
# Create the Return Path records
echo -e "\n\nCreating Return Path records for $RP_RECORD..."
create_dns_record "A" "$RP_RECORD" "$IPV4" 1 false
create_dns_record "AAAA" "$RP_RECORD" "$IPV6" 1 false
create_dns_record "TXT" "$RP_RECORD" "v=spf1 a mx include:spf.postal.$ZONE ~all" 1 false
# Create the Route domain record
echo -e "\n\nCreating Route domain record for $ROUTE_RECORD..."
create_dns_record "MX" "$ROUTE_RECORD" "10 postal.$ZONE" 1 false
# Final message
echo -e "\n\nAll DNS entries have been successfully completed. You can now proceed with the rest of the Postal setup. Remember to enter the record names you chose in your postal.yml configuration file."
#!/bin/bash
# Check if we are running inside a screen session
if [ -z "$STY" ]; then
# Check if screen is installed
if ! command -v screen &> /dev/null; then
echo "Screen is not installed. Would you like to install it now? (y/n)"
read -r install_screen
if [[ $install_screen == "y" ]]; then
# Install screen (Debian/Ubuntu example)
sudo apt-get install screen -y
else
echo "Screen is required to safely run this script."
exit 1
fi
fi
# Start a new screen session with a unique name based on the current timestamp
SESSION_NAME="script_session_$(date +%s)"
screen -dmS "$SESSION_NAME" "$0" "$@"
echo "Started a new screen session named $SESSION_NAME"
echo "If you get disconnected, log back in and type 'screen -r $SESSION_NAME' to reattach."
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment