Script Overview
This script automates the backup of website files and databases on a Linux system. It performs the following key functions:
- Creates compressed backups of specified website directories and MySQL databases.
- Uploads the backup files to a designated FTP server.
- Sends the backup files via email as attachments for an additional layer of redundancy.
- Automatically deletes old backup files from both the local system and the FTP server based on a configurable retention period.
Prerequisites
Before using the script, ensure the following components are installed and configured on your system:
- Create a backup directory:
mkdir -p /home/backup - Install the mail sending utility:
yum install sendmail mutt - Install the zip compression tool:
yum install zip - Install cron for scheduling (if not present):
yum -y install vixie-cron crontabs
Configuration
You must edit the following variables in the script to match your environment. The core configuration section is shown below:
# === CONFIGURATION START ===
MYSQL_USER=root # MySQL username
MYSQL_PASS=your_password # MySQL password
[email protected] # Email address to receive backups
FTP_USER=ftp_user # FTP username
FTP_PASS=ftp_password # FTP password
FTP_IP=ftp.example.com # FTP server address
FTP_backup=backup # Remote FTP directory (must exist)
WEB_DATA=/home/www # Local website directory to backup
# === CONFIGURATION END ===
Complete Backup Script
Save the following script as autoback.sh.
#!/bin/bash
# === CONFIGURATION START ===
MYSQL_USER=root
MYSQL_PASS=your_password
[email protected]
FTP_USER=ftp_user
FTP_PASS=ftp_password
FTP_IP=ftp.example.com
FTP_backup=backup
WEB_DATA=/home/www
# === CONFIGURATION END ===
# Define backup filenames
DataBakName=Data_$(date +"%Y%m%d").tar.gz
WebBakName=Web_$(date +%Y%m%d).tar.gz
OldData=Data_$(date -d -5day +"%Y%m%d").tar.gz
OldWeb=Web_$(date -d -5day +"%Y%m%d").tar.gz
# Delete local backups older than 3 days
rm -rf /home/backup/Data_$(date -d -3day +"%Y%m%d").tar.gz /home/backup/Web_$(date -d -3day +"%Y%m%d").tar.gz
cd /home/backup
# Export all databases
for db in $(/usr/local/mysql/bin/mysql -u$MYSQL_USER -p$MYSQL_PASS -B -N -e 'SHOW DATABASES' | xargs); do
(/usr/local/mysql/bin/mysqldump -u$MYSQL_USER -p$MYSQL_PASS ${db} | gzip -9 - > ${db}.sql.gz)
done
# Package database files
zip -r /home/backup/data.zip /usr/local/mysql/var/
zip -r /home/backup/sqldata.zip /home/backup/*.sql.gz
tar zcf /home/backup/$DataBakName /home/backup/*.zip
rm -rf /home/backup/*.zip /home/backup/*.sql.gz
# Email database backup (comment out if file is too large)
echo "Database Backup" | mutt -a /home/backup/$DataBakName -s "Database Backup $(date +'%Y%m%d')" $MAIL_TO
# Compress website data
tar zcf /home/backup/$WebBakName $WEB_DATA
# Upload to FTP and delete old remote backups
ftp -v -n $FTP_IP << END
user $FTP_USER $FTP_PASS
type binary
cd $FTP_backup
delete $OldData
delete $OldWeb
put $DataBakName
put $WebBakName
bye
END
Usage Instructions
1. Save and Set Permissions
After saving the script, make it executable:
chmod +x autoback.sh
Note: If you encounter an error like /bin/sh^M: bad interpreter: No such file or directory, the file has DOS line endings. Fix it with:
sed -i 's/r$//' autoback.sh
2. Schedule with Cron
To run the script automatically, add a cron job. For example, to run daily at 4 AM:
crontab -e
Add the following line:
0 4 * * * /home/autoback.sh
Tips and Recommendations
- Database Selection: Create a dedicated MySQL user with permissions only for the databases you need to backup, and use it in the script for improved security.
- Selective File Backup: If your website directory is large, modify the
WEB_DATAvariable to backup only essential directories (e.g., uploads, configuration files) to save time and space. - Retention Policy: Adjust the number of days in the
OldDataandOldWebvariable definitions to match your desired backup retention period on the FTP server. - Large Backups: Email systems often have attachment size limits. For large backups, consider omitting the email step or using a cloud storage service instead of/in addition to FTP.