How to restore files from a backup

Topic: Servers linux

Summary

Restore from a tar archive or rsync backup to the original or a new path; verify permissions and service config; bring services back up and confirm the app works. Use this after data loss or to roll back a bad change.

Intent: How-to

Quick answer

  • Tar: extract to a temp dir first (tar -xzvf backup.tar.gz -C /tmp/restore); compare or copy into place; fix ownership (chown -R) if needed.
  • Rsync: reverse direction (rsync -avz user@host:/backup/path/ /local/); use --dry-run first; ensure services are stopped if restoring over live data.
  • After restore: restart services; verify config and connectivity; run a quick functional test; document what was restored and when.

Prerequisites

Steps

  1. Locate the backup

    Identify the backup file or rsync path; verify it is intact (tar -tf or rsync --list-only); ensure you have enough disk at the restore destination.

  2. Extract or copy to a staging area

    tar -xzvf backup.tar.gz -C /tmp/restore; or rsync -av user@host:/backup/ /tmp/restore/; list and spot-check files before overwriting production.

  3. Restore to target

    Stop the service if it uses the files; cp -a or rsync from staging to real path; chown -R correct user:group; start service and check config.

  4. Verify and document

    Service starts; config is correct (e.g. nginx -t); app responds; note what was restored and from which backup and time.

Summary

You will restore files from a tar or rsync backup to a staging area then to the real path, fix ownership, and confirm services and the app work. Use this after accidental deletion or a bad config change.

Prerequisites

  • The backup (tar file or rsync destination) accessible and verified.
  • Root or sudo; knowledge of which paths and services are affected.

Steps

Step 1: Locate the backup

ls -la /backup/config-2026-02-01.tar.gz
tar -tf /backup/config-2026-02-01.tar.gz | head
# or
rsync --list-only user@host:/backups/hostname/

Confirm the backup is from the right date and contains the paths you need.

Step 2: Extract or copy to a staging area

mkdir -p /tmp/restore
tar -xzvf /backup/config-2026-02-01.tar.gz -C /tmp/restore
ls -la /tmp/restore/etc

Or rsync from backup host to /tmp/restore. Do not overwrite live paths yet.

Step 3: Restore to target

sudo systemctl stop nginx
sudo cp -a /tmp/restore/etc/nginx/* /etc/nginx/
sudo chown -R root:root /etc/nginx
sudo systemctl start nginx
sudo nginx -t

Use cp -a to preserve permissions and timestamps; adjust paths and service name.

Step 4: Verify and document

  • Service status; config test (nginx -t, etc.); one HTTP request or app check.
  • Note: “Restored /etc/nginx from config-2026-02-01.tar.gz on DATE.”

Verification

  • Service is running; config valid; app responds; no permission errors in logs.

Troubleshooting

Restore overwrote wrong path — Restore again from backup to correct path; if backup was wrong, restore from an older backup and document.

Service fails after restore — Config may be for a different version or host; compare with a known-good backup; fix paths and restart.

Next steps

Continue to