/var/www/html
site_backup
rsync -avz /var/www/html/ /backup/site_backup/
rsync -avz /var/www/html/ /backup/site_backup/
rsync -avz /var/www/html/ /backup/site_backup/
rsync -avz -e ssh /var/www/html/ user@remote_server:/path/to/remote/backup/
rsync -avz -e ssh /var/www/html/ user@remote_server:/path/to/remote/backup/
rsync -avz -e ssh /var/www/html/ user@remote_server:/path/to/remote/backup/
tar -czvf /backup/etc_backup_$(date +%Y%m%d).tar.gz /etc/
tar -czvf /backup/etc_backup_$(date +%Y%m%d).tar.gz /etc/
tar -czvf /backup/etc_backup_$(date +%Y%m%d).tar.gz /etc/
$(date +%Y%m%d)
crontab -e
minute hour day_of_month month day_of_week command_to_run
minute hour day_of_month month day_of_week command_to_run
minute hour day_of_month month day_of_week command_to_run
/usr/local/bin/backup_script.sh
0 3 * * * /usr/local/bin/backup_script.sh >> /var/log/backup.log 2>&1
0 3 * * * /usr/local/bin/backup_script.sh >> /var/log/backup.log 2>&1
0 3 * * * /usr/local/bin/backup_script.sh >> /var/log/backup.log 2>&1
>> /var/log/backup.log 2>&1
sudo mkdir -p /mnt/backup/daily
sudo chown your_user:your_user /mnt/backup/daily
sudo mkdir -p /mnt/backup/daily
sudo chown your_user:your_user /mnt/backup/daily
sudo mkdir -p /mnt/backup/daily
sudo chown your_user:your_user /mnt/backup/daily
/usr/local/bin/backup_script.sh
#!/bin/bash # --- Configuration ---
BACKUP_SOURCE="/var/www/html /etc /home/your_user/data" # Directories to back up
BACKUP_DEST="/mnt/backup/daily/" # Destination directory
LOG_FILE="/var/log/backup.log"
DATE_FORMAT=$(date +%Y-%m-%d_%H-%M-%S)
RETENTION_DAYS=7 # How many days of backups to keep # --- Functions ---
log_message() { echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE"
} # --- Main Backup Logic ---
log_message "--- Starting backup ---" # Create timestamped directory for today's backup
mkdir -p "${BACKUP_DEST}${DATE_FORMAT}" for source_dir in $BACKUP_SOURCE; do log_message "Backing up: $source_dir" rsync -avz --delete "$source_dir" "${BACKUP_DEST}${DATE_FORMAT}/" >> "$LOG_FILE" 2>&1 if [ $? -ne 0 ]; then log_message "ERROR: rsync failed for $source_dir" fi
done # --- Cleanup Old Backups ---
log_message "Cleaning up old backups (older than $RETENTION_DAYS days)..."
find "$BACKUP_DEST" -maxdepth 1 -type d -mtime +"$RETENTION_DAYS" -exec rm -rf {} \; >> "$LOG_FILE" 2>&1
log_message "Old backup cleanup complete." log_message "--- Backup finished ---"
exit 0
#!/bin/bash # --- Configuration ---
BACKUP_SOURCE="/var/www/html /etc /home/your_user/data" # Directories to back up
BACKUP_DEST="/mnt/backup/daily/" # Destination directory
LOG_FILE="/var/log/backup.log"
DATE_FORMAT=$(date +%Y-%m-%d_%H-%M-%S)
RETENTION_DAYS=7 # How many days of backups to keep # --- Functions ---
log_message() { echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE"
} # --- Main Backup Logic ---
log_message "--- Starting backup ---" # Create timestamped directory for today's backup
mkdir -p "${BACKUP_DEST}${DATE_FORMAT}" for source_dir in $BACKUP_SOURCE; do log_message "Backing up: $source_dir" rsync -avz --delete "$source_dir" "${BACKUP_DEST}${DATE_FORMAT}/" >> "$LOG_FILE" 2>&1 if [ $? -ne 0 ]; then log_message "ERROR: rsync failed for $source_dir" fi
done # --- Cleanup Old Backups ---
log_message "Cleaning up old backups (older than $RETENTION_DAYS days)..."
find "$BACKUP_DEST" -maxdepth 1 -type d -mtime +"$RETENTION_DAYS" -exec rm -rf {} \; >> "$LOG_FILE" 2>&1
log_message "Old backup cleanup complete." log_message "--- Backup finished ---"
exit 0
#!/bin/bash # --- Configuration ---
BACKUP_SOURCE="/var/www/html /etc /home/your_user/data" # Directories to back up
BACKUP_DEST="/mnt/backup/daily/" # Destination directory
LOG_FILE="/var/log/backup.log"
DATE_FORMAT=$(date +%Y-%m-%d_%H-%M-%S)
RETENTION_DAYS=7 # How many days of backups to keep # --- Functions ---
log_message() { echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE"
} # --- Main Backup Logic ---
log_message "--- Starting backup ---" # Create timestamped directory for today's backup
mkdir -p "${BACKUP_DEST}${DATE_FORMAT}" for source_dir in $BACKUP_SOURCE; do log_message "Backing up: $source_dir" rsync -avz --delete "$source_dir" "${BACKUP_DEST}${DATE_FORMAT}/" >> "$LOG_FILE" 2>&1 if [ $? -ne 0 ]; then log_message "ERROR: rsync failed for $source_dir" fi
done # --- Cleanup Old Backups ---
log_message "Cleaning up old backups (older than $RETENTION_DAYS days)..."
find "$BACKUP_DEST" -maxdepth 1 -type d -mtime +"$RETENTION_DAYS" -exec rm -rf {} \; >> "$LOG_FILE" 2>&1
log_message "Old backup cleanup complete." log_message "--- Backup finished ---"
exit 0
log_message
RETENTION_DAYS
chmod +x /usr/local/bin/backup_script.sh
chmod +x /usr/local/bin/backup_script.sh
chmod +x /usr/local/bin/backup_script.sh
crontab -e
0 3 * * * /usr/local/bin/backup_script.sh
0 3 * * * /usr/local/bin/backup_script.sh
0 3 * * * /usr/local/bin/backup_script.sh
# Example for offsite rsync backup script
REMOTE_USER="backupuser"
REMOTE_HOST="your_remote_server.com"
REMOTE_DEST="/path/to/remote/backups/"
BACKUP_SOURCE="/var/www/html /etc" # Directories to back up rsync -avz -e ssh $BACKUP_SOURCE $REMOTE_USER@$REMOTE_HOST:$REMOTE_DEST
# Example for offsite rsync backup script
REMOTE_USER="backupuser"
REMOTE_HOST="your_remote_server.com"
REMOTE_DEST="/path/to/remote/backups/"
BACKUP_SOURCE="/var/www/html /etc" # Directories to back up rsync -avz -e ssh $BACKUP_SOURCE $REMOTE_USER@$REMOTE_HOST:$REMOTE_DEST
# Example for offsite rsync backup script
REMOTE_USER="backupuser"
REMOTE_HOST="your_remote_server.com"
REMOTE_DEST="/path/to/remote/backups/"
BACKUP_SOURCE="/var/www/html /etc" # Directories to back up rsync -avz -e ssh $BACKUP_SOURCE $REMOTE_USER@$REMOTE_HOST:$REMOTE_DEST
restic init --repo s3:your-s3-bucket-url:path/to/repo
restic backup /var/www/html --repo s3:your-s3-bucket-url:path/to/repo
restic init --repo s3:your-s3-bucket-url:path/to/repo
restic backup /var/www/html --repo s3:your-s3-bucket-url:path/to/repo
restic init --repo s3:your-s3-bucket-url:path/to/repo
restic backup /var/www/html --repo s3:your-s3-bucket-url:path/to/repo
/etc/apache2
/var/www/html - Pros: Simplest to restore. You have everything in one place.
- Cons: Can be very time-consuming and require significant storage space. - Pros: Much faster and require less storage space than full backups.
- Cons: Restoring requires the last full backup plus all subsequent incremental backups, making the process more complex. - Pros: Faster than full backups and require less storage. Restoration is simpler than incremental backups, needing only the last full backup and the latest differential backup.
- Cons: Storage requirements grow over time compared to incremental backups. - -a: Archive mode, which preserves permissions, timestamps, ownership, etc.
- -v: Verbose output, showing you what's being transferred.
- -z: Compress file data during the transfer. - -c: Create an archive.
- -z: Compress the archive using gzip.
- -v: Verbose output.
- -f: Specify the archive file name.
- $(date +%Y%m%d): This part dynamically inserts the current date into the filename, creating a unique backup file for each day. - 0 3 * * *: This specifies the schedule: 0 minutes past the 3rd hour, any day of the month, any month, any day of the week.
- >> /var/log/backup.log 2>&1: This redirects both standard output and standard error to a log file, helping you track the backup process and troubleshoot any issues. - Configuration: Defines which directories to back up, where to store them, the log file location, and how long to keep old backups.
- log_message function: A helper to write timestamped messages to the log file.
- Main Backup Logic: Iterates through each source directory and uses rsync to copy it to a new, timestamped directory within the backup destination. The --delete flag ensures that files deleted from the source are also removed from the destination, effectively mirroring the source.
- Cleanup Old Backups: Uses find to locate directories older than RETENTION_DAYS and removes them. This prevents your backup drive from filling up. - 3 copies of your data: The original data plus at least two backups.
- 2 different media: Store backups on at least two different types of storage (e.g., local disk, external drive, cloud storage).
- 1 offsite copy: Keep at least one backup copy in a geographically separate location. - Verify the integrity of your backup files.
- Ensure you understand the restoration procedure.
- Identify any potential issues before a real disaster strikes. - Databases: For databases like MySQL or PostgreSQL, simply copying the data files might not be sufficient due to ongoing transactions. Use database-specific tools (e.g., mysqldump, pg_dump) to create consistent logical backups.
- Application Data: Configuration files (/etc, /etc/nginx, /etc/apache2), user data (/home), and web server content (/var/www/html) are typically high priority.
- System State: For disaster recovery, you might consider full system image backups or snapshots, especially if you're using virtual machines.