Can you please share your backup strategies for linux? I’m curious to know what tools you use and why?How do you automate/schedule backups? Which files/folders you back up? What is your prefered hardware/cloud storage and how do you manage storage space?
Example of a Bash script that performs the following tasks
Example script:
#!/bin/bash # Settings WEB_SERVER="https://example.com" BACKUP_DIR="/backup" TARGET_DIRS="/var/www /etc" DISK_USAGE_THRESHOLD=90 ADMIN_EMAIL="admin@example.com" DATE=$(date +"%Y-%m-%d") BACKUP_FILE="$BACKUP_DIR/backup-$DATE.tar.gz" # Checking web server availability echo "Checking web server availability..." if curl -s --head $WEB_SERVER | grep "200 OK" > /dev/null; then echo "Web server is available." else echo "Warning: Web server is unavailable!" | mail -s "Problem with web server" $ADMIN_EMAIL fi # Checking disk space echo "Checking disk space..." DISK_USAGE=$(df / | grep / | awk '{ print $5 }' | sed 's/%//g') if [ $DISK_USAGE -gt $DISK_USAGE_THRESHOLD ]; then echo "Warning: Disk space usage exceeded $DISK_USAGE_THRESHOLD%!" | mail -s "Problem with disk space" $ADMIN_EMAIL else echo "There is enough disk space." fi # Creating backup echo "Creating backup..." tar -czf $BACKUP_FILE $TARGET_DIRS if [ $? -eq 0 ]; then echo "Backup created successfully: $BACKUP_FILE" else echo "Error creating backup!" | mail -s "Error creating backup" $ADMIN_EMAIL fi # Sending report echo "Sending report to $ADMIN_EMAIL..." REPORT="Report for $DATE\n\n" REPORT+="Web server status: $(curl -s --head $WEB_SERVER | head -n 1)\n" REPORT+="Disk space usage: $DISK_USAGE%\n" REPORT+="Backup location: $BACKUP_FILE\n" echo -e $REPORT | mail -s "Daily system report" $ADMIN_EMAIL echo "Done."
Description:
curl
command to check if the site is available.df
andawk
to check disk usage. If the threshold (90%) is exceeded, a notification is sent.tar
command archives and compresses the directories specified in theTARGET_DIRS
variable.mail
.How to use:
chmod +x /path/to/your/script.sh
cron
to run on a regular basis:Example to run every day at 00:00:
0 0 * * * /path/to/your/script.sh