Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
tar -czf site-2026-11-17.tar.gz -C /var/www shop
tar -tzf site-2026-11-17.tar.gz | head
tar -xzf site-2026-11-17.tar.gz -C /tmp/restore
tar -xzf site-2026-11-17.tar.gz -C /tmp/restore shop/config/app.phpRemember the letters: c create, t list, x extract, z gzip, f file name follows, C change to this folder first.
Step by step
- Create an archive. Use
-Cso the paths inside are short and relative (not/var/www/...):
tar -czf /backup/etc-$(date +%F).tar.gz -C / etc
tar -czf /backup/shop.tar.gz -C /var/www --exclude='shop/cache' --exclude='*.log' shop- Choose the compression:
| Option | Ending | Use |
|---|---|---|
-z | .tar.gz | Fast, works everywhere. The default choice. |
-j | .tar.bz2 | Smaller, slower. Old style. |
-J | .tar.xz | Much smaller, slow to create. Releases, long-term archives. |
--zstd | .tar.zst | Very fast and small. Good for big backups (tar 1.31+). |
When you extract, modern GNU tar finds the compression itself: tar -xf file.tar.xz works.
- List before you extract (see what is inside and the paths):
tar -tvzf shop.tar.gz | head -5
drwxr-xr-x nginx/nginx 0 2026-11-17 09:12 shop/
-rw-r--r-- nginx/nginx 4212 2026-11-17 09:10 shop/index.php- Extract to a test folder first, never over live files by mistake:
mkdir -p /tmp/restore
tar -xzf /backup/shop.tar.gz -C /tmp/restore
tar -xzpf /backup/shop.tar.gz -C /var/www --same-ownerAs root, tar keeps owners and modes by default (-p, --same-owner). Add --acls --xattrs --selinux when you create and extract system files on RHEL.
- Copy a folder to another server in one stream (no big temp file):
- Check the archive and save a checksum with it:
Check that it worked
A backup is only good when the restore works. Extract into /tmp/restore, compare with the source, then delete the test folder:
diff -r /var/www/shop /tmp/restore/shop && echo "same"Common problems
| Problem | Cause | Fix |
|---|---|---|
Removing leading '/' from member names | You gave absolute paths. | Only a note — tar stores relative paths (safe). Use -C / to avoid it. |
gzip: stdin: not in gzip format | The file is not gzip (maybe xz, zstd, or plain tar). | file archive; then tar -xf without -z. |
Unexpected EOF in archive | The file is cut (disk full, copy stopped). | Check free space; copy again; compare sha256sum. |
| Files have the wrong owner after extract | Extracted as a normal user. | Extract as root, or chown -R after. |
Cannot open: Permission denied | No write right in the target folder. | Use a folder you own, or root for system paths. |
| The archive is huge | It took caches, logs or node_modules. | --exclude=PATTERN, or --exclude-vcs for .git. |
Go further
- Command pages: tar, gzip, ssh, rsync.
- Next how-tos: Copy files between servers, Schedule a job with cron, Check disk space, Encrypt a file with GPG or age.
- Learn it: RHCSA — tar, RH134 — Archive and compress, TLCL — tar, TLCL — tar over SSH.
- Real backups with versions: Install restic.
- Practise: Lab: organize files, make links and back up a folder.
1Why use -C /var/www shop instead of /var/www/shop?
The archive then holds short relative paths (shop/...), so you can extract it anywhere.