When performing full server backups using rsync, it's crucial to understand which directories contain transient or hardware-specific data that shouldn't be preserved. The Linux filesystem hierarchy contains several special directories that either:
- Represent virtual filesystems (/proc, /sys)
- Conttain temporary files (/tmp)
- House volatile runtime data (/run)
- Store hardware-specific information (/dev)
Here's the comprehensive list of directories you should exclude from your rsync backup:
--exclude=/proc \
--exclude=/sys \
--exclude=/tmp \
--exclude=/run \
--exclude=/dev \
--exclude=/mnt \
--exclude=/media \
--exclude=/var/run \
--exclude=/var/lock \
--exclude=/lost+found
Here's the enhanced backup command with proper exclusions:
rsync -aPh --delete \
--exclude=/proc \
--exclude=/sys \
--exclude=/tmp \
--exclude=/run \
--exclude=/dev \
--exclude=/mnt \
--exclude=/media \
--exclude=/var/run \
--exclude=/var/lock \
--exclude=/lost+found \
server.example.com:/ /mnt/backup
/proc and /sys: These are virtual filesystems that provide interfaces to kernel data structures and hardware information. Backing them up is not only pointless but potentially harmful if restored on different hardware.
/tmp and /var/tmp: While you might exclude these by default, consider if any critical applications store data here temporarily. In most cases, exclusion is safe.
Device Files (/dev): The contents are created dynamically by udev and should never be backed up.
For more granular control, you can use pattern matching:
--exclude={'/proc/*','/sys/*','/tmp/*','/run/*','/dev/*','/mnt/*'} \
--exclude={'/media/*','/var/run/*','/var/lock/*','*.tmp','*.swp'}
Always verify your exclusions by performing a dry run first:
rsync -aPhn --delete \
[exclusion patterns] \
server.example.com:/ /mnt/backup
This will show what would be transferred without actually copying any files.
Instead of excluding problematic directories, you might specify only what to include:
rsync -aPh --delete \
--include=/etc \
--include=/home \
--include=/var/www \
--include=/opt \
--include=/usr/local \
--exclude=* \
server.example.com:/ /mnt/backup
When performing full system backups of Linux servers using rsync, blindly copying all directories can cause serious issues during restoration. The virtual filesystems and temporary data locations don't belong in backups.
These directories should always be excluded from your rsync backups:
--exclude=/proc
--exclude=/sys
--exclude=/dev
--exclude=/tmp
--exclude=/run
--exclude=/mnt
--exclude=/media
--exclude=/var/run
--exclude=/var/lock
--exclude=/lost+found
/proc
and /sys
contain runtime system information that's dynamically generated by the kernel. Backing them up is not just unnecessary - restoring these files to another system could cause kernel panics or hardware detection issues.
For databases and applications with temporary files, you might also want to exclude:
--exclude='*.tmp'
--exclude='*.swp'
--exclude='*.sock'
Here's a production-grade rsync command I've used for years:
rsync -aAXvPh --delete \
--exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} \
--exclude={"/var/run/*","/var/lock/*","/var/tmp/*"} \
--exclude={"/var/cache/*","/var/lib/dhcp/*"} \
server.example.com:/ /mnt/backup
For database servers, remember to either:
- Stop the database service before backup
- Use database-specific dump tools (mysqldump, pg_dump)
- Utilize filesystem snapshots
Always test your backup strategy by:
# Check most recent backup integrity
ls -l /mnt/backup/etc/passwd
# Verify database dumps if applicable
head -n 20 /mnt/backup/var/backups/mysql/dump.sql
Remember that backup strategies should be regularly tested through restoration drills to ensure they work when needed most.