Skip to content
-
Subscribe to our newsletter & never miss our best posts. Subscribe Now!
Network Former

Domain For Sale

Network Former

Domain For Sale

  • Home
  • Sample Page
  • Home
  • Sample Page
Close

Search

  • https://www.facebook.com/
  • https://twitter.com/
  • https://t.me/
  • https://www.instagram.com/
  • https://youtube.com/
Subscribe
Automating Server Backups with Advanced Shell Scripting
Article

Automating Server Backups with Advanced Shell Scripting

By jasabacklink
June 11, 2026 2 Min Read
Comments Off on Automating Server Backups with Advanced Shell Scripting

Introduction to Backup Automation

Manual server backups are error-prone and time-consuming. Automating this process with advanced shell scripting ensures consistent, scheduled data protection. This guide provides a practical, production-ready approach using Bash scripts, cron, and common Linux utilities. You’ll learn to create incremental backups, manage retention, and handle restoration verification.

Prerequisites for Shell Script Backup Automation

  • Linux server (Ubuntu 20.04+ or CentOS 7+ recommended)
  • Root or sudo access
  • Basic Bash familiarity
  • rsync installed (package: rsync)
  • tar and gzip
  • cron daemon running

Step 1: Create the Backup Script

Navigate to /usr/local/bin/ and create backup.sh:

sudo nano /usr/local/bin/backup.sh

Insert the following advanced script:

#!/bin/bash
# Advanced server backup script - Version 2.1
# Variables
BACKUP_DIR="/var/backups/server"
SOURCE_DIR="/var/www/html"   # Change as needed
LOG_FILE="/var/log/backup.log"
DATE=$(date +%Y-%m-%d_%H%M%S)
RETENTION_DAYS=7
REMOTE_SERVER="user@backup.example.com:/remote/path/"
# Create backup directory if not exists
mkdir -p "$BACKUP_DIR"
# Full backup using tar
tar -czf "$BACKUP_DIR/full_$DATE.tar.gz" "$SOURCE_DIR" 2>> "$LOG_FILE"
# Verify backup integrity
tar -tzf "$BACKUP_DIR/full_$DATE.tar.gz" > /dev/null 2>> "$LOG_FILE"
if [ $? -eq 0 ]; then
    echo "Full backup successful: $DATE" >> "$LOG_FILE"
else
    echo "Backup verification failed at $DATE" >> "$LOG_FILE"
    exit 1
fi
# Incremental backup with rsync
rsync -avz --delete "$SOURCE_DIR" "$BACKUP_DIR/incremental_$DATE/" >> "$LOG_FILE" 2>&1
# Sync to remote server (optional)
rsync -avz --delete "$BACKUP_DIR/" "$REMOTE_SERVER" >> "$LOG_FILE" 2>&1
# Retention policy: remove old backups
find "$BACKUP_DIR" -name "*.tar.gz" -type f -mtime +"$RETENTION_DAYS" -exec rm {} ;
find "$BACKUP_DIR" -type d -mtime +"$RETENTION_DAYS" -exec rm -rf {} ;
echo "Backup cycle completed at $DATE" >> "$LOG_FILE"

Step 2: Set Permission and Make Executable

sudo chmod +x /usr/local/bin/backup.sh
sudo chown root:root /usr/local/bin/backup.sh

Step 3: Configure Log Rotation

To prevent log overflow, create /etc/logrotate.d/backup:

/var/log/backup.log {
    daily
    rotate 7
    compress
    missingok
    notifempty
}

Step 4: Schedule with Cron

Open crontab editor: sudo crontab -e
Add for daily backup at 2 AM:

0 2 * * * /usr/local/bin/backup.sh

For hourly incremental syncing:

0 * * * * /usr/local/bin/backup.sh --incremental-only
(Modify script to accept flag for incremental-only mode)

Step 5: Implement Disaster Recovery Testing

Create a testing script /usr/local/bin/test-restore.sh:

#!/bin/bash
# Test restore from latest backup
LATEST=$(ls -t /var/backups/server/full_*.tar.gz | head -1)
tar -xzf "$LATEST" -C /tmp/backup_test/ && echo "Restore test passed" || echo "Restore test failed"

Step 6: Monitor and Alert

Add email alert on failure by inserting before exit 1:

mail -s "Backup failed on $(hostname)" admin@example.com < "$LOG_FILE"
Ensure mailutils or sendmail is installed.

Best Practices for Shell Scripted Backups

  • Use absolute paths in cron to avoid environment issues.
  • Separate critical data into multiple backup jobs (database, application, configuration).
  • Encrypt backups for remote transfer using GPG: gpg --encrypt --recipient key backup.tar.gz.
  • Validate checksums with md5sum or sha256sum after each backup.
  • Document your script with comments for team handover.

Conclusion

This advanced shell scripting approach automates server backups with integrity verification, retention management, remote syncing, and monitoring. Customize the SOURCE_DIR and retention policy for your environment. Regularly test restoration to ensure your backup automation works when needed.

Tags:

advanced shell scriptingautomate backupsbackup automationbackup best practicesbackup schedulingbackup scriptsbackup securitybash scriptscron jobsdata protectiondisaster recoveryfull backupsincremental backupsLinux backupsLinux server toolslog monitoringrsync backupsscript debuggingserver backup strategiesserver backupsserver maintenanceshell script examplesshell scriptingsystem administrationtar backups
Author

jasabacklink

Follow Me
Other Articles
How to Set Up a Virtual Private Server for Web Hosting
Previous

How to Set Up a Virtual Private Server for Web Hosting

The Role of Content Delivery Networks in Web Performance
Next

The Role of Content Delivery Networks in Web Performance

Recent Posts

  • How to Set Up a Secure Virtual Private Network for Remote Work
  • The Fundamentals of Network Topology and System Design
  • Scaling Your Web Infrastructure for Global Audiences
  • How to Implement Continuous Integration in Web Development
  • Understanding the Core Differences Between Apache and Nginx

Recent Comments

No comments to show.

Archives

  • June 2026

Categories

  • Article

NetworkFormer.com

domain for sale

https://www.dynadot.com/market/user-listings/networkformer.com

Recent Posts

  • How to Set Up a Secure Virtual Private Network for Remote Work
  • The Fundamentals of Network Topology and System Design
  • Scaling Your Web Infrastructure for Global Audiences
  • How to Implement Continuous Integration in Web Development
  • Understanding the Core Differences Between Apache and Nginx

Tags

access control API integration backlink monitoring caching CDN cybersecurity DDoS protection domain age domain analysis domain appraisal domain authority domain flipping domain intelligence domain investing domain metrics domain monetization Domain Rating domain research expired domains latency reduction link building link quality load balancing network infrastructure network monitoring network performance network reliability network security network segmentation niche domains premium domains reverse proxy scalability secure networking SEO SEO domains SEO impact SEO strategy SEO tools server security technical SEO web development web scraping website authority website optimization

Partner Links

Belum ada link terpasang.

Copyright 2026 — Network Former. All rights reserved. Blogsy WordPress Theme