Automating Hourly Server Backups with rsync, tar, and Cron
This guide details a step‑by‑step solution for hourly client backups of /etc and /var/log, naming conventions, rsync transfer, integrity checks, email notifications, and cleanup on both client and server sides using Bash scripts and cron jobs.
The task requires a client to pack /etc and /var/log every hour, store the archives in a directory named hostname_ip_timestamp, push them to a backup server via rsync, and keep only the last two days locally. The server must receive the data, verify integrity, notify an administrator, and retain backups for seven days.
Client Side
Create a base backup directory /backup.
Generate a timestamped subdirectory using
$(hostname)_$(ifconfig eth1|awk 'NR==2{print $2}')_$(date +%F_%H).
Archive /etc and /var/log with tar -czf into that subdirectory.
Move the created .tgz files into the timestamped folder.
Export RSYNC_PASSWORD and sync the /backup tree to the server with rsync -avzP /backup/ [email protected]::backup-nfs.
Delete local backups older than two days using find /backup/ -mtime +2 -delete.
Client Script (autorsync.sh)
#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
mkdir -p /backup/$(hostname)_$(ifconfig eth1|awk 'NR==2{print $2}')_$(date +%F_%H)/
cd / && tar -czf /backup/$(hostname)_$(ifconfig eth1|awk 'NR==2{print $2}')_$(date +%F_%H)/etc.tgz etc
cd / && tar -czf /backup/$(hostname)_$(ifconfig eth1|awk 'NR==2{print $2}')_$(date +%F_%H)/log.tgz var/log
export RSYNC_PASSWORD=mima666
rsync -az /backup/ [email protected]::backup-nfs
find /backup/ -mtime +2 -deleteServer Side
Install rsync and configure /etc/rsyncd.conf with a module [backup-nfs] pointing to /backup.
Create a system user www, set permissions, and store credentials in /etc/rsync.passwd.
Start the rsync daemon.
After each sync, verify file integrity with md5sum -c /backup/md5_tgz.txt.
Send the verification result via email using mailx (configured with a QQ SMTP account).
Remove backups older than seven days: find /backup/ -mtime +7 -delete.
Server Script (checkrsync.sh)
#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
md5sum -c /backup/md5_tgz.txt > /backup/check_md5_result.txt
mail -s "check-rsync-$(date +%F)" 877348***[email protected] < /backup/check_md5_result.txt
find /backup/ -mtime +7 -deleteCron Scheduling
Client:
0 * * * * /bin/bash /scripts/autorsync.sh > /tmp/backup.log 2>&1(run at every hour).
Server:
30 * * * * /bin/bash /scripts/checkrsync.sh > /tmp/backup.log 2>&1(run 30 minutes past each hour).
The provided commands and scripts form a complete automated backup solution, handling packaging, naming, transfer, integrity verification, notification, and cleanup.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
