• About Us
  • Disclaimer
  • Indeks
Kebumen Update
No Result
View All Result
  • Web Hosting and Server Management
  • Monitoring & Maintenance
  • Security & Hardening
  • Panels & Tools
  • Cloud & DevOps
  • Tech
  • Web Hosting and Server Management
  • Monitoring & Maintenance
  • Security & Hardening
  • Panels & Tools
  • Cloud & DevOps
  • Tech
No Result
View All Result
Kebumen Update
No Result
View All Result
Home Monitoring & Maintenance

Automated Backups for Your Server with Rsync & Cron

awbsmed by awbsmed
April 12, 2025
in Monitoring & Maintenance
0
Backup and Restore Your Linux System with rsync - YouTube
ADVERTISEMENT

 

In today’s digital landscape, ensuring the safety and integrity of your server data is paramount. Data loss can occur due to hardware failures, cyberattacks, or human errors. Implementing an automated backup system is a proactive measure to safeguard your data. This guide will walk you through setting up automated backups on your Linux server using rsync and cron, two powerful tools that, when combined, provide a robust backup solution.

READ ALSO

How to Check CPU, RAM, and Disk Usage on Linux Servers

How to Set Up Cron Jobs for Server Automation


A. Understanding Rsync and Cron

Before diving into the setup, it’s essential to understand the tools we’ll be using:

1. Rsync

rsync is a fast and versatile command-line utility for synchronizing files and directories between two locations over a remote shell or to/from a remote rsync daemon. It offers numerous options to control its behavior, including incremental backups, compression, and file exclusion.

2. Cron

cron is a time-based job scheduler in Unix-like operating systems. It allows users to schedule scripts or commands to run automatically at specified intervals, making it ideal for automating repetitive tasks like backups.


B. Preparing Your Environment

1. Install Rsync

Most Linux distributions come with rsync pre-installed. To verify its installation:

bash
rsync --version

If it’s not installed, you can install it using your package manager:

  • For Debian/Ubuntu:

    bash
    sudo apt update
    sudo apt install rsync
  • For CentOS/RHEL:

    bash
    sudo yum install rsync

2. Create a Backup Directory

Decide where you want to store your backups. For example:

bash
mkdir -p /backup/server_data

Ensure the directory has appropriate permissions:

bash
chmod 700 /backup/server_data

Backup and Restore Your Linux System with rsync - YouTube

C. Crafting the Rsync Backup Script

Creating a script allows for flexibility and reusability.

1. Basic Rsync Command

A simple rsync command to back up /var/www to /backup/server_data:

bash
rsync -av --delete /var/www/ /backup/server_data/

Explanation of options:

  • -a: Archive mode; preserves permissions, symbolic links, and other attributes.

  • -v: Verbose output.

  • --delete: Deletes files in the destination that no longer exist in the source.

2. Enhancing the Script

To make the script more robust:

bash

#!/bin/bash

# Variables
SOURCE=“/var/www/”
DESTINATION=“/backup/server_data/”
LOGFILE=“/var/log/backup.log”
DATE=$(date +“%Y-%m-%d %H:%M:%S”)

# Rsync Command
echo “Backup started at $DATE” >> $LOGFILE
rsync -av –delete $SOURCE $DESTINATION >> $LOGFILE 2>&1
echo “Backup completed at $(date +”%Y-%m-%d %H:%M:%S”)” >> $LOGFILE
echo “—————————————-“ >> $LOGFILE

Save this script as backup.sh and make it executable:

bash
chmod +x backup.sh

D. Automating Backups with Cron

To schedule the backup script to run automatically:

  1. Open the crontab editor:

    bash
    crontab -e
  2. Add the following line to run the backup daily at 2 AM:

    bash
    0 2 * * * /path/to/backup.sh

Cron Timing Format:

  • Minute (0 – 59)

  • Hour (0 – 23)

  • Day of Month (1 – 31)

  • Month (1 – 12)

  • Day of Week (0 – 6) (Sunday=0)


E. Implementing Incremental Backups

Incremental backups save only the changes made since the last backup, saving time and storage.

1. Using Rsync’s –backup Option

bash
rsync -av --delete --backup --backup-dir=/backup/incremental/$(date +%Y-%m-%d) /var/www/ /backup/server_data/

This command:

  • Backs up /var/www/ to /backup/server_data/.

  • Stores changed or deleted files in /backup/incremental/YYYY-MM-DD/.

Ensure the incremental directory exists:

bash
mkdir -p /backup/incremental/$(date +%Y-%m-%d)

F. Securing Remote Backups

Backing up data to a remote server adds an extra layer of protection.

1. Setting Up SSH Key Authentication

On the source server:

bash
ssh-keygen -t rsa -b 4096 -f ~/.ssh/backup_key
ssh-copy-id -i ~/.ssh/backup_key.pub user@remote-server

2. Rsync Over SSH

bash
rsync -av -e "ssh -i ~/.ssh/backup_key" /var/www/ user@remote-server:/backup/server_data/

This command securely transfers data to the remote server using SSH.


G. Managing Backup Retention

Over time, backups can consume significant storage. Implementing a retention policy helps manage disk space.

1. Deleting Old Backups

To delete backups older than 30 days:

bash
find /backup/incremental/ -type d -mtime +30 -exec rm -rf {} \;

Schedule this cleanup with cron:

bash
0 3 * * * find /backup/incremental/ -type d -mtime +30 -exec rm -rf {} \;

H. Monitoring and Alerts

Regular monitoring ensures backups are running as expected.

1. Email Notifications

Modify the backup script to send an email upon completion:

bash
mail -s "Backup Completed" user@example.com < /var/log/backup.log

Ensure the mail utility is installed and configured on your server.

2. Log Rotation

Prevent log files from growing indefinitely by setting up log rotation:

Create a file /etc/logrotate.d/backup with the following content:

lua
/var/log/backup.log {
weekly
rotate 4
compress
missingok
notifempty
}

How To Backup Your Entire Linux System Using Rsync - OSTechNix

I. Testing Your Backup System

Regularly test your backups to ensure data integrity.

1. Restore Test

Attempt to restore files from your backup to a test directory:

bash
mkdir /tmp/restore_test
rsync -av /backup/server_data/ /tmp/restore_test/

Verify the files are intact and accessible.


Conclusion

Automating server backups using rsync and cron is an effective strategy to protect your data against unforeseen events. By following this guide, you’ve set up a system that:

  • Performs regular backups.

  • Maintains incremental changes.

  • Secures data on remote servers.

  • Manages storage through retention policies.

  • Provides monitoring and alerts

Tags: automated backupscrondata protectionincremental backupsLinux serverremote backupsrsync
ADVERTISEMENT

Related Posts

RAM & CPU Usage PHP Script | Server Health Check – JamesBachini.com
Monitoring & Maintenance

How to Check CPU, RAM, and Disk Usage on Linux Servers

April 12, 2025
Automating Tasks in Linux with Cron Jobs
Monitoring & Maintenance

How to Set Up Cron Jobs for Server Automation

April 12, 2025
The 9 Best Server Monitoring Tools To Use in 2019
Monitoring & Maintenance

Top 5 Tools to Monitor Server Performance in Real-Time

April 12, 2025
Next Post
What is Cyber Panel

Installing and Using CyberPanel on a Cloud VPS

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

High-Performance Servers Ensure Future-Proof Tech

High-Performance Servers Ensure Future-Proof Tech

by Salsabilla Yasmeen Yunanta
August 26, 2025
0

The year 2025 marks a new era in the world of computing, where the demand for speed, efficiency, and scalability...

Server Boosts Become A Strong Mechanism for Communities

Server Boosts Become A Strong Mechanism for Communities

by Salsabilla Yasmeen Yunanta
July 31, 2025
0

In the rapidly expanding digital landscape, where online communities thrive as hubs for connection, collaboration, and shared interests, the concept...

Advance Server Allows Game Developers to Optimize Feedback

Advance Server Allows Game Developers to Optimize Feedback

by Salsabilla Yasmeen Yunanta
July 31, 2025
0

In the fast-paced, ever-evolving world of online gaming, where user experience and competitive balance are paramouthe purpose, operational mechanics, and...

Powering Digital: Backend Innovations Surge

Powering Digital: Backend Innovations Surge

by Salsabilla Yasmeen Yunanta
July 25, 2025
0

The visible elegance of a website or mobile application, the smooth user experience, and the instant responsiveness we've come to...

Kebumen Update

KebumenUpdate.com diterbitkan oleh PT BUMI MEDIA PUBLISHING dengan sertifikat pendirian Kementerian Hukum dan Hak Asasi Manusia Republik Indonesia Nomor: AHU-012340.AH.01.30.Tahun 2022

  • About Us
  • Editor
  • Code of Ethics
  • Privacy Policy
  • Cyber Media Guidelines

Copyright © 2025 Kebumen Update. All Right Reserved

No Result
View All Result
  • Homepages
    • Home Page 1
    • Home Page 2
  • Tech

Copyright © 2025 Kebumen Update. All Right Reserved