How to back up files with tar and rsync

Topic: Servers linux

Summary

Create full or incremental backups with tar (archives) and rsync (mirror or incremental), store them off the host, and verify with listing or checksum. Use this for config and data backup before changes or for scheduled retention.

Intent: How-to

Quick answer

  • Full archive: tar -czvf backup.tar.gz /etc /var/important; list with tar -tvf; extract with tar -xzvf -C /restore. Exclude large or transient dirs.
  • Mirror or incremental: rsync -avz --delete /source/ /dest/ or to remote user@host:/dest; use --link-dest for incremental snapshots.
  • Verify: list archive or run rsync --dry-run; checksum or compare size; test restore to a scratch dir and spot-check files.

Prerequisites

Steps

  1. Choose scope and destination

    Decide what to back up (/etc, /var/app, /home) and where (another disk, NFS, remote host); ensure destination has space and is not on the same physical disk if possible.

  2. Create tar archive

    tar -czvf backup-$(date +%F).tar.gz --exclude=/var/cache --exclude=/tmp /etc /var/important; move or copy to destination; verify with tar -tvf backup.tar.gz | head.

  3. Use rsync for mirror or incremental

    rsync -avz --delete /source/ user@host:/backup/path/; for incremental use --link-dest=../prev /source/ /backup/current/; cron as needed.

  4. Verify and test restore

    Extract tar to /tmp/restore-test or run rsync --dry-run; compare a few files; document the restore procedure.

Summary

You will back up files using tar (compressed archives) and rsync (mirror or incremental), store backups off the host, and verify by listing or test restore. Use this for config and data protection and before major changes.

Prerequisites

  • Root or sudo for reading system paths; destination writable (local or remote via SSH).

Steps

Step 1: Choose scope and destination

  • Include /etc and application config and data; exclude /tmp, /var/cache, and large transient data unless needed.
  • Destination: another partition, NFS, or remote server (rsync over SSH).

Step 2: Create tar archive

sudo tar -czvf /backup/config-$(date +%F).tar.gz \
  --exclude=/var/cache --exclude=/tmp \
  /etc /var/lib/important_app
sudo tar -tvf /backup/config-*.tar.gz | head -20

Copy or move the archive to the final destination.

Step 3: Use rsync for mirror or incremental

rsync -avz --delete /source/ user@backup-host:/backups/hostname/
# Incremental with hardlinks
rsync -avz --link-dest=/backups/prev /source/ /backups/curr/

Use cron or systemd timer for scheduled runs.

Step 4: Verify and test restore

tar -xzvf backup.tar.gz -C /tmp/restore-test
diff -r /tmp/restore-test/etc/nginx /etc/nginx | head

Document: path to archive, extract command, and where to restore.

Verification

  • Archive lists correctly; rsync —dry-run shows expected changes; test restore produces readable files in a scratch dir.

Troubleshooting

Tar fails: file changed during read — Normal for live system; exclude volatile files or stop the app briefly; consider rsync for live backup.

Rsync permission denied — Ensure SSH key or password works; destination path writable by the user; use sudo on source with rsync —rsync-path=“sudo rsync” if needed.

Next steps

Continue to