This script automates the process of backing up multiple directories and databases, compressing them, and sending a detailed report via email. It supports encryption, email notifications, Docker service management, and database backups.
- Multiple Directories and Databases: You can specify multiple directories and Docker-based databases to back up.
- Exclusion of Directories: Option to exclude certain directories from the backup process.
- Compression and Optional Encryption: Backups are compressed using
pigz, and encryption with AES-256 can be enabled or disabled. - Email Reports: Sends a detailed report after each backup operation, including backup size, MD5 checksum, and disk write speed.
- Docker Management: The script can stop Docker services before the backup and restart them afterward.
- Database Backup: Supports the backup of PostgreSQL, Redis, and other Docker-managed databases.
- Customizable: Fully customizable with variables for backup names, source directories, email settings, database backup options, and retention policies.
- Backup to Multiple Destinations: Option to back up files to two separate SATA disks for redundancy (1,2,3 principle), Samba and SCP!.
Before running the script, ensure that the following packages are installed on your Linux system:
tarbarpigzopensslmsmtpcoreutilscifs-utilsopenssh-clientsshpasssmbclient
The script will automatically check and install these packages if they are not available on your system.
The script includes several variables that can be customized:
- Backup name:
BACKUP_NAME– Defines the name of the backup. - Server name:
SERVER_NAME– Name of the server to include in the email report. - Encryption:
ENCRYPT_BACKUP– Set toYto enable encryption of backup files, orNto disable it. - Source directories:
SOURCE_DIRS– List of directories to include in the backup. - Exclude directories:
EXCLUDE_DIRS– List of directories to exclude from the backup. - Backup directory:
BACKUP_DIR– Where the backups will be saved. - Email settings: Set the recipient, SMTP host, port, and credentials for sending the backup report via email.
- Database backup:
BACKUP_DOCKER_DATABASE– Set toYto enable Docker database backups. - Database list:
DATABASES– Define which databases to back up, including container name, database name, and credentials. - Backup retention:
DAYS_TO_KEEP– Number of days to keep old backups. - Backup destinations:
BACKUP_123– Set toYto enable backup to additional disks. - Max CPU cores:
MAX_CPU_CORE– Set the number of CPU cores to use for compression.
Example An example of the source and excluded directories setup:
SOURCE_DIRS=(
"/home/JohnDoe"
"/etc"
)
EXCLUDE_DIRS=(
"/home/JohnDoe/Personal"
)An example of database backup setup:
DATABASES=(
"PostgreSQL|Joplin-Postgress|joplindb|joplin|password"
"Redis|immich_redis||"
)You can automate the backup process by adding it to your crontab. For example, to run the backup every day at midnight:
0 0 * * * /path/to/backup.sh- Modify the variables: Edit the script to set the directories and databases to back up, email settings, and other configuration options.
- Run the script: Make sure you are running the script as root or with
sudo:sudo ./backup.sh
To configure the script to send email notifications using iCloud's SMTP server:
-
Generate an App-Specific Password for iCloud:
- Go to appleid.apple.com and generate a password for third-party apps.
-
Update the Script with iCloud SMTP settings:
EMAIL_RECIPIENT="youraddress@icloud.com" SMTP_HOST="smtp.mail.me.com" SMTP_PORT="587" SMTP_FROM="youraddress@icloud.com" SMTP_USER="youraddress@icloud.com" SMTP_PASSWORD="app-specific-password"
- Cleanup old backups keeping only the most recent ones based on DAYS_TO_KEEP
- Removed pv and introduce to bar
- The script now supports the
BACKUP_123feature, which enables backup redundancy by copying backups to different locations such as Samba shares and remote servers via SCP. - Added 10 different email templates.
- The updated script includes enhancements for handling Docker containers and backing up databases, with improved consistency and performance.
- Added Docker-managed database backup functionality for PostgreSQL, Redis, and other databases.
- Added option to back up to multiple SATA disks for redundancy (1,2,3 principle).
- Introduced the ability to limit the number of CPU cores for compression.
- Added functionality to select different email templates (10).
- Updated the encryption option to be disabled by default.
- Initial release of the backup script.





