When working with command-line utilities like MySQL dump, we often encounter situations where we need to attach the output directly to an email without creating temporary files. The core issue is that most email clients (including mutt) require file paths for attachments rather than accepting raw streams.
Bash provides a powerful feature called process substitution that creates temporary FIFO (named pipe) files automatically:
mutt -a <(mysqldump -u username -p database_name) admin@example.org
The <(command)
syntax creates a temporary file descriptor that mutt can read from. This appears as a regular file to the recipient program while avoiding actual disk storage.
For systems where process substitution isn't available, consider these methods:
Using a Pipe with mkfifo
mkfifo /tmp/mysqlpipe
mysqldump -u username -p database_name > /tmp/mysqlpipe &
mutt -a /tmp/mysqlpipe admin@example.org
rm /tmp/mysqlpipe
In-Memory Temporary Files
Modern Linux systems can use RAM-based tempfs:
mutt -a /dev/shm/mysqldump.tmp admin@example.org
When dealing with database dumps:
- Use secure connection methods for both MySQL and email
- Set proper permissions on temporary files
- Consider encrypting sensitive data before transmission
Combine with compression for large dumps:
mutt -a <(mysqldump -u username -p database_name | gzip) \
-s "Compressed DB backup" admin@example.org
When working with command-line tools in Linux, we often encounter situations where we need to:
- Process command output as if it were a physical file
- Pass this output to programs that only accept file arguments (like email clients)
- Avoid creating temporary physical files when possible
Most sysadmins initially approach this with temporary files:
mysqldump -u root -p mydatabase > /tmp/dump.sql
mutt -a /tmp/dump.sql admin@example.com -s "Database Backup"
rm /tmp/dump.sql
While functional, this method has several drawbacks:
- Requires cleanup (potential for forgotten temp files)
- Disk I/O overhead
- Permission issues in shared environments
- Security concerns with sensitive data
Bash provides process substitution which creates temporary FIFO files or /dev/fd entries:
mutt -a <(mysqldump -u root -p mydatabase) admin@example.com -s "DB Backup"
How this works:
- The
<(command)
syntax creates a temporary file descriptor - The command output becomes readable as a file
- The descriptor automatically closes after use
Using Named Pipes
mkfifo /tmp/mypipe
mysqldump -u root -p mydatabase > /tmp/mypipe &
mutt -a /tmp/mypipe admin@example.com
rm /tmp/mypipe
With gzip Compression
mutt -a <(mysqldump -u root -p mydatabase | gzip) admin@example.com -s "Compressed DB Backup"
These techniques shine in:
- Automated backup scripts
- CI/CD pipelines
- Data processing workflows
- Secure handling of sensitive outputs
- Not all programs handle FIFOs well (test first)
- Large outputs may cause memory issues
- Some email clients may require complete files before sending
- Command output isn't seekable (can't rewind)
mutt -a <(mysqldump -u root -p mydatabase | gpg --encrypt --recipient admin@example.com) \
admin@example.com -s "Encrypted DB Backup"