Automated Website Backup
Comprehensive backup script that automatically backs up website files and database, compresses and transfers to remote server.
Published: March 15, 2024
Detailed Information
This script automatically backs up your website files and database, compresses them, and optionally transfers them to a remote server. It is a critical backup solution for websites.
What Does This Script Do?
This script performs a comprehensive website backup process:
- Backs up website files (all files and directories)
- Backs up MySQL database
- Creates an info file containing backup information
- Combines all backups into a single archive file
- Transfers to remote server (with rsync)
- Automatically cleans old backups
- Cleans temporary files
Why Should You Use It?
Website backup protects against data loss:
- Full Backup: Files and database together
- Automation: Full automation with cron job
- Remote Storage: Sends backups to remote server
- Disk Management: Automatically cleans old backups
- Disaster Recovery: Quick restore capability
How to Use
Step-by-Step Usage Guide
1. Configuration Settings
Edit variables at the beginning of the script:
SITE_PATH="/var/www/html" # Website directory
BACKUP_PATH="/backup/websites" # Backup directory
MYSQL_USER="backup_user" # MySQL username
MYSQL_PASS="your_password" # MySQL password
DB_NAME="your_database" # Database name
REMOTE_SERVER="[email protected]" # Remote server
REMOTE_PATH="/backups" # Remote server directory
RETENTION_DAYS=30 # Backup retention (days)
2. SSH Key Setup (Passwordless Transfer)
ssh-keygen -t rsa
ssh-copy-id [email protected]
3. Run Script
chmod +x website_backup.sh
sudo ./website_backup.sh
4. Automation
# Daily backup with crontab (at 03:00)
sudo crontab -e
# Add: 0 3 * * * /path/to/website_backup.sh Requirements
Requirements
- Root Privileges: Required for file backup
- mysqldump: For database backup
- tar, gzip: For compression
- rsync: For remote server transfer (optional)
- SSH Access: For remote server connection
Use Cases
Use Cases
1. Daily Automated Backup
Automatically back up your production websites every day.
2. Disaster Recovery
Quickly restore in case of server crash.
3. Data Migration
Use backups when moving your website to another server.
Examples
Usage Examples
Example 1: Basic Usage
sudo ./website_backup.sh
Example 2: Automated Backup
# Daily at 03:00
0 3 * * * /path/to/website_backup.sh Code
#!/bin/bash
# Automated Website Backup Script
SITE_PATH="/var/www/html"
BACKUP_PATH="/backup/websites"
MYSQL_USER="backup_user"
MYSQL_PASS="your_password"
DB_NAME="your_database"
REMOTE_SERVER="[email protected]"
REMOTE_PATH="/backups"
RETENTION_DAYS=30
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_NAME="website_backup_${DATE}"
TEMP_DIR="/tmp/$BACKUP_NAME"
mkdir -p "$TEMP_DIR"
mkdir -p "$BACKUP_PATH"
echo "======================================"
echo " WEBSITE BACKUP PROCESS"
echo "======================================"
echo "Started: $(date)"
echo ""
echo "1. Backing up website files..."
cd "$SITE_PATH" || exit 1
tar -czf "$TEMP_DIR/files.tar.gz" .
if [ $? -eq 0 ]; then
SIZE=$(du -h "$TEMP_DIR/files.tar.gz" | cut -f1)
echo "✓ Files backed up ($SIZE)"
else
echo "✗ File backup failed!"
exit 1
fi
echo ""
echo "2. Backing up database..."
mysqldump -u"$MYSQL_USER" -p"$MYSQL_PASS" \
--single-transaction \
--routines \
--triggers \
"$DB_NAME" | gzip > "$TEMP_DIR/database.sql.gz"
if [ $? -eq 0 ]; then
SIZE=$(du -h "$TEMP_DIR/database.sql.gz" | cut -f1)
echo "✓ Database backed up ($SIZE)"
else
echo "✗ Database backup failed!"
exit 1
fi
echo ""
echo "3. Creating backup info..."
cat > "$TEMP_DIR/backup_info.txt" << EOF
Backup Date: $(date)
Server: $(hostname)
Site Path: $SITE_PATH
Database: $DB_NAME
Files Size: $(du -sh "$SITE_PATH" | cut -f1)
Database Size: $(mysql -u"$MYSQL_USER" -p"$MYSQL_PASS" -e "SELECT ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS Size_MB FROM information_schema.tables WHERE table_schema = \"$DB_NAME\";" -sN) MB
EOF
echo "✓ Info file created"
echo ""
echo "4. Creating final archive..."
cd /tmp || exit 1
tar -czf "$BACKUP_PATH/${BACKUP_NAME}.tar.gz" "$BACKUP_NAME"
if [ $? -eq 0 ]; then
FINAL_SIZE=$(du -h "$BACKUP_PATH/${BACKUP_NAME}.tar.gz" | cut -f1)
echo "✓ Final archive created ($FINAL_SIZE)"
else
echo "✗ Archive creation failed!"
exit 1
fi
echo ""
echo "5. Transferring to remote server..."
if command -v rsync &> /dev/null; then
rsync -avz "$BACKUP_PATH/${BACKUP_NAME}.tar.gz" "${REMOTE_SERVER}:${REMOTE_PATH}/"
if [ $? -eq 0 ]; then
echo "✓ Backup transferred to remote server"
else
echo "⚠️ Remote transfer failed (backup saved locally)"
fi
else
echo "⚠️ rsync not found, skipping remote transfer"
fi
echo ""
echo "6. Cleaning up..."
rm -rf "$TEMP_DIR"
echo "✓ Temporary files removed"
echo ""
echo "7. Removing backups older than $RETENTION_DAYS days..."
find "$BACKUP_PATH" -name "website_backup_*.tar.gz" -mtime +$RETENTION_DAYS -delete
echo "✓ Old backups cleaned"
echo ""
echo "======================================"
echo " BACKUP SUMMARY"
echo "======================================"
echo "Backup Name: ${BACKUP_NAME}.tar.gz"
echo "Backup Size: $FINAL_SIZE"
echo "Backup Location: $BACKUP_PATH"
echo "Completed: $(date)"
echo ""
echo "Recent backups:"
ls -lth "$BACKUP_PATH" | head -n 6
Usage
# Edit configuration
nano website_backup.sh
chmod +x website_backup.sh
# Manual backup
sudo ./website_backup.sh
# Automated backup (daily at 3:00 AM)
sudo crontab -e
# Add: 0 3 * * * /path/to/website_backup.sh
# SSH key setup (for passwordless transfer)
ssh-keygen -t rsa
ssh-copy-id [email protected]
Troubleshooting
Troubleshooting
Problem: "Permission denied"
Solution: Run script as root:
sudo ./website_backup.sh
Problem: "mysqldump: command not found"
Solution: Install MySQL client:
sudo apt-get install mysql-client