Automate Hourly Server Backups with rsync: Complete Bash Script Guide
This guide details how to set up hourly client backups of /etc and /var/log, organize them into timestamped directories, securely transfer them via rsync to a backup server, verify integrity, notify administrators, and automate cleanup with cron jobs.
Task Background
Client Requirements
Client must hourly pack backups of /etc and /var/log, store them in a directory named "hostname_ip_currenttime", push the archive to the backup server via rsync, and keep only the last two days locally.
Server Requirements
Server runs rsync to receive data, validates completeness, notifies the administrator, retains backups for seven days, and deletes older files; all backup data resides under /backup.
Host List
Client: 172.16.1.110 (client-110) Server: 172.16.1.241 (rsync-test-241)
Experiment Process
Idea: decompose each requirement, devise solutions, combine them into a single script.
1. Client
1.1 Pack Backup
Use tar to archive /etc and /var/log.
# Create backup directory
mkdir /backup
# Pack /etc
cd / && tar -czf /backup/etc.tgz etc
# Pack /var/log
cd / && tar -czf /backup/log.tgz var/log1.1.1 Create Backup Directory
mkdir /backup1.1.2 Pack /etc
cd / && tar -czf /backup/etc.tgz etc1.1.3 Pack /var/log
cd / && tar -czf /backup/log.tgz var/log2. Directory Naming
Directory name format: hostname_ip_currenttime, e.g., client-110_172.16.1.110_2024-11-20_16.
2.1 Get Hostname
hostname2.2 Extract IP
ifconfig eth1 | awk 'NR==2{print $2}'2.3 Get Current Date Hour
date +%F_%H2.4 Combine Components
echo "$(hostname)_$(ifconfig eth1|awk 'NR==2{print $2}')_$(date +%F_%H)"2.5 Create Target Directory
mkdir -p /backup/$(hostname)_$(ifconfig eth1|awk 'NR==2{print $2}')_$(date +%F_%H)2.6 Move Backup Files
mv /backup/*.tgz /backup/client-110_172.16.1.110_2024-11-20_15/3. Transfer
Install rsync, set the password variable, and push the archives to the server.
# Install rsync
yum install -y rsync
# Set password
export RSYNC_PASSWORD=mima666
# Transfer
rsync -avzP /backup/ [email protected]::backup-nfs4. Delete Expired Files
find /backup/ -mtime +2 -delete5. Consolidated Script
#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
# Create directory
mkdir -p /backup/$(hostname)_$(ifconfig eth1|awk 'NR==2{print $2}')_$(date +%F_%H)/
# Pack data
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
# Transfer
export RSYNC_PASSWORD=mima666
rsync -az /backup/ [email protected]::backup-nfs
# Cleanup
find /backup/ -mtime +2 -deleteServer
1. Deploy rsyncd Service
#!/bin/bash
yum install rsync -y
cat > /etc/rsyncd.conf << 'EOF'
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
[backup-nfs]
comment = client backup!
path = /backup
EOF
useradd -u 1000 -M -s /sbin/nologin www
mkdir -p /backup
chown -R www:www /backup
echo "rsync_backup:mima666" > /etc/rsync.passwd
chmod 600 /etc/rsync.passwd
systemctl start rsyncd2. Verify Data Integrity
Generate MD5 checksums on the client and verify them on the server.
# On client (added to script)
md5sum /backup/$(hostname)_$(ifconfig eth1|awk 'NR==2{print $2}')_$(date +%F_%H)/*.tgz > /backup/md5_tgz.txt
# On server
md5sum -c /backup/md5_tgz.txt3. Email Notification
# Install mailx
yum install mailx -y
# Configure mail
cat > /etc/mail.rc << 'EOF'
set [email protected]
set smtp=smtps://smtp.qq.com:465
set [email protected]
set smtp-auth-password=your_smtp_password
set smtp-auth=login
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb/
EOF
# Generate verification result file
md5sum -c /backup/md5_tgz.txt > /backup/check_md5_result.txt
# Send email
mail -s "check-rsync-$(date +%F)" [email protected] < /backup/check_md5_result.txt4. Delete Old Files
find /backup/ -mtime +7 -delete5. Server Script
#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
# Verify integrity
md5sum -c /backup/md5_tgz.txt > /backup/check_md5_result.txt
# Notify admin
mail -s "check-rsync-$(date +%F)" [email protected] < /backup/check_md5_result.txt
# Cleanup old backups
find /backup/ -mtime +7 -delete3. Schedule Tasks
Client Cron
0 * * * * /bin/bash /scripts/autorsync.sh > /tmp/backup.log 2>&1Server Cron
30 * * * * /bin/bash /scripts/checkrsync.sh > /tmp/backup.log 2>&1Original blog link: https://www.cnblogs.com/funlyp/p/18558670
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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
