Sending Backup Files via Command Line (Headless Debian Server) Offsite: Workaround for Mutt, Redmine Database, and Outlook Account
In today's interconnected world, it is essential to have a reliable and secure method to send backup files from a headless Debian server to an offsite location. For many system administrators, this task involves using the Mutt command-line email client to send attachments from a Redmine database backup to an Outlook account. However, recent security changes have made this process more challenging. This article will explore a workaround for this issue, providing a detailed explanation of the key concepts involved and enriched with code examples.
The Challenge: Security Changes Affecting Mutt, Redmine, and Outlook Integration
Due to increasing cybersecurity threats, many email services have implemented strict security measures. This move has impacted the seamless integration of Mutt, Redmine, and Outlook, which was previously a widely used method to send backup files offsite.
Understanding Mutt, Redmine, and Outlook Integration
Mutt is a lightweight and flexible command-line email client that can send emails from the terminal. Redmine is a popular open-source project management tool with built-in database capabilities, while Outlook is one of the most widely used email clients from Microsoft.
The traditional workflow to send Redmine database backups using Mutt and Outlook involved using the following command:
mutt -s "Database Backup" [email protected] -a Why the Method Stops Working
Recent security changes implemented by email providers might involve enforcing TLS encryption, using OAuth2 authentication, and the implementation of DMARC, SPF, and DKIM policies. These changes have disrupted the previous Mutt-Redmine-Outlook workflow, as Mutt does not natively support modern authentication methods. The solution, therefore, lies in setting up a workaround to enable Mutt to send emails authenticated properly, without compromising security.
The Workaround: Using a Custom Script
To resolve the challenges brought by the recent security changes, a custom script is needed to act as an intermediary between Mutt and your email provider. This custom script will use modern authentication methods such as OAuth2, thus enabling Mutt to send emails with verified credentials.
Here's a snippet of a custom script, written in Python:
import os
import base64
import requests
email_address = "[email protected]"
email_password = "your_email_password"
message = "Subject: Database Backup\r
\r
Attached is the latest Redmine database backup."
filename = ""
mail_server = 'smtp.office365.com'
mail_port = 587
session = requests.Session()
base64_byte_email = base64.b64encode(email\_address.encode('ascii')).decode()
base64_byte_password = base64.b64encode(email\_password.encode('ascii')).decode()
# Create OAuth2 token
def create\_oauth2\_token():
url = "https://login.microsoftonline.com/common/oauth2/token"
data = {
'grant\_type': 'password',
'client\_id': 'your\_client\_id',
'client\_secret': 'your\_client\_secret',
'resource': 'https://graph.microsoft.com',
'username': base64\_byte\_email,
'password': base64\_byte\_password
}
result = session.post(url, data=data)
access\_token = result.json().get('access\_token')
return access\_token
# Construct header
def get\_header(access\_token):
return {
'Authorization': \'Bearer %s\' % access\_token,
'Content-Type': 'application/json'
}
# Send email
def send\_email(mail\_server, mail\_port, message, filename, header):
with smtplib.SMTP(mail\_server, mail\_port) as server:
server.starttls()
server.login("your\_email\_address", "your\_app\_password", headers=header)
server.sendmail(email\_address, email\_address, message, smtplib.MIMEMultipart.MIMEText(message))
access\_token = create\_oauth2\_token()
header = get\_header(access\_token)
send\_email(mail\_server, mail\_port, message, filename, header)
Note that you will need to replace the placeholders with your specific email credentials and app-specific passwords. You can generate the app-specific password from your Microsoft account's security settings. More information on generating app-specific passwords can be found here.
Updating the Backup Script
Once you have set up the custom script, the final step is to modify the Redmine backup script to use the new script and pass the backup file as an argument:
python3 custom\_send\_mail.py -a The Mutt, Redmine, and Outlook integration once used to send Redmine database backups offsite needs updating due to recent security changes. Setting up a custom script acting as a secure intermediary between Mutt and email providers permits you to send attachments using authenticated and encrypted credentials.
References
- Mutt Command-Line Email Client: http://www.mutt.org
- Redmine Project Management Tool: https://www.redmine.org
- Microsoft Outlook Email Client: https://www.microsoft.com/en-us/microsoft-365/outlook/email-and-calendar-app
- Microsoft Graph API Documentation: https://docs.microsoft.com/en-us/graph/
- How to Create and Use an App Password: https://support.microsoft.com/en-us/account-billing/sign-in-and-security-details-c4a38566-71be-8b8c-05b0-448f6cbe264d