This project automates the backup of a directory using rsync
and sends email notifications upon successful or failed backups. The backups are scheduled using crontab
, and an SMTP setup is configured for sending email alerts.
- An AWS EC2 instance (or any Linux server)
- SSH access to the instance
- Installed rsync tool for efficient file transfer
- Postfix for sending emails via SMTP
- A Google account with an app-specific password for email notifications
-
Launch an AWS EC2 instance and name it “Backup_project”.
-
Connect to your EC2 instance via SSH using the following command:
ssh -i "backup_project.pem" ubuntu@<instance-public-dns>
-
Install
rsync
, which is efficient for transferring files as it only copies the differences between the source and destination.sudo apt-get install rsync
-
Create a directory named
backup
to store your backup script and log files.mkdir ~/backup
-
Inside the
backup
directory, create a shell script namedbackup.sh
to automate the backup process.nano ~/backup/backup.sh
-
Add the necessary commands in
backup.sh
, including the username, IP, source, and destination directories.
Example script:
#!/bin/bash
SOURCE_DIR="/home/ubuntu/code/"
DESTINATION_DIR="/home/ubuntu/backup/"
LOG_FILE="/home/ubuntu/backup/backup.log"
TIMESTAMP=$(date +"%Y-%m-%d-%H-%M-%S")
EMAIL="your-email@gmail.com"
rsync -avz --delete $SOURCE_DIR $DESTINATION_DIR >> $LOG_FILE 2>&1
if [ $? -eq 0 ]; then
echo "$TIMESTAMP: BACKUP SUCCESSFUL" >> $LOG_FILE
echo "Backup was successful at $TIMESTAMP" | mail -s "Backup Success" $EMAIL
else
echo "$TIMESTAMP: BACKUP FAILED" >> $LOG_FILE
echo "Backup failed at $TIMESTAMP" | mail -s "Backup Failed" $EMAIL
fi
-
Create the directory that you want to back up automatically. For this example, the directory is named
code
.mkdir ~/code
-
Use
crontab
to schedule automatic backups every day at 5:40 PM.crontab -e
-
Add the following line to the crontab file to run the backup script daily at 5:40 PM:
40 17 * * * /home/ubuntu/backup/backup.sh
-
If you want to manually view the crontab list or schedule, use:
crontab -l
- After each backup (whether successful or failed), the log will be stored in
backup.log
located in thebackup
directory.
-
In the
/etc/postfix
folder, change the method for SMTP protocol and setinet_protocols
toipv4
only. Openmain.cf
and modify the configuration:sudo nano /etc/postfix/main.cf
-
Add the following lines to configure Gmail SMTP:
relayhost = [smtp.gmail.com]:587 smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd smtp_sasl_security_options = noanonymous smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt smtp_use_tls = yes
- Create a Google App-specific password for Gmail (since direct login using Gmail is not allowed). Use this password in the SMTP configuration.
-
Create a file named
sasl_passwd
in the/etc/postfix
folder to store your Gmail SMTP credentials:sudo nano /etc/postfix/sasl_passwd
-
Add the following content to the
sasl_passwd
file (replace with your actual email and app-specific password):[smtp.gmail.com]:587 your-email@gmail.com:your-app-specific-password
-
Secure the
sasl_passwd
file and postmap it:sudo chmod 600 /etc/postfix/sasl_passwd sudo postmap /etc/postfix/sasl_passwd
-
Restart the Postfix service to apply the changes:
sudo systemctl restart postfix
-
Use the following command to monitor mail logs:
sudo tail -f /var/log/mail.log
-
Use the following command to check the mail queue list:
postqueue -p
-
To remove a particular mail from the queue by its post ID:
postsuper -d post_id
-
To remove all mails from the queue:
postsuper -d all
This project is licensed under the MIT License.
This is a comprehensive `README.md` file detailing the steps taken for setting up the automatic backup, configuring email notifications, and troubleshooting the setup.