Back to Explore
GuideVibeFix editorialUpdated Nov 26, 2026

Linux Backup from Zero: rsync Moves Only the Delta, tar Packs and Restores, Measured Live

Measured live: a 6-file source tree of 153,691 bytes; the first rsync moved all 153,691 bytes, the second only 61 bytes and deleted 1 file; the tar archive is about 427 bytes with 8 entries, and the restore diffs identical; dry run shows deleting before the real run.

Real photograph of the inside of an opened hard disk drive, metal platter and read-write head in close-up (illustrative photo)

Introduction: copying files and syncing them are two different jobs

Sooner or later every project needs a backup: the website's source files, photos, configuration — lose them and you rewrite them. The most primitive method is cp: copy the whole directory, simple and direct, but with two flaws. First, it copies everything every time, which gets slow as files grow. Second, files deleted from the source stay in the backup, and over time the backup and the source drift apart. rsync is the tool built for syncing: the first run copies everything, later runs move only what changed, and deleted files can be removed from the backup too. tar is the tool that packs a whole directory tree into a single file, which is what backups, transfers and archives rely on. Both tools ship on most Linux machines, with zero third-party packages.

This guide does not recite a parameter table. It works from a real run on a real machine: a fixed source tree of 6 files, synced twice with rsync, one run through the trailing-slash trap, then packed with tar, unpacked and restored — every number coming from the demo script demo257_g2.sh, whose full transcript is attached at the end. By the end you should be able to set up a working backup routine for your own project: rsync for daily syncing, tar for periodic archives, and a dry run before anything destructive.

1. Environment and data: fix the numbers first

First, the environment. The demo ran on one Linux machine, rsync 3.2.7 with protocol version 31, GNU tar 1.35, script demo257_g2.sh, once in Chinese and once in English, both exiting 0, with identical key numbers in both transcripts. The working directory is /tmp/rsync_demo257, wiped and rebuilt on every run, so every number below is reproducible.

The source tree src has 6 files: config.txt at 19 bytes, notes.txt at 36 bytes, report.txt at 36 bytes, data.bin at 102400 bytes (100K, written with dd to an exact size), photos/img1.jpg at 20480 bytes, photos/img2.jpg at 30720 bytes. The 6 files add up to 153,691 bytes. That number will keep coming back: it is the total file size of the first rsync, and the baseline for checking whether a backup is correct. The files are deliberately tiny, small enough to check by hand, but the mix is intentional: the two big files hold almost all the volume, so deleting data.bin drops the total dramatically, and the effect is visible at a glance.

2. The first rsync: everything gets copied

The basic sync command is rsync -av src/ backup/. -a is archive mode, meaning permissions, times, ownership and the rest are preserved and the tree is copied as it stands; -v is verbose, printing which files were copied. Measured: on the first sync, rsync's --stats report reads Number of files: 8 (reg: 6, dir: 2) — 8 entries, 6 regular files and 2 directories; Total file size: 153,691 bytes, matching the sum computed above; Total transferred file size: 153,691 bytes — on the first sync every file is new, so all of it has to move.

rsync -av --stats src/ backup/
# Number of files: 8 (reg: 6, dir: 2)
# Total file size: 153,691 bytes
# Total transferred file size: 153,691 bytes

Counting afterwards: backup holds 6 files, the same as the source. The three --stats numbers mean different things, so keep them apart: Number of files counts entries, directories included; Total file size is the sum of all file sizes in the source; Total transferred file size is how much file content actually moved this time. On the first sync the relationship is simple: everything moved, 153,691 bytes. There is also Total bytes sent, which counts protocol overhead as well — about 154 KB, slightly more than 153,691, the extra being the file list and similar overhead. That figure varies by a few bytes between runs, so this guide quotes it only approximately; the exact value is in the transcript.

Remember rsync's core decision rule: how does it know whether a file needs to be sent? By default it compares size and modification time: same size and same time means unchanged, so it skips it; a changed size or time means it sends it. That default is fast and good enough for almost all daily backups. For extra safety, add -c, which compares checksums of file contents — but that requires reading both sides fully to compute, so it is much slower. The practical split is: default rules for daily syncs, a -c check before an important archive.

3. Change three things, sync again: only the delta moves

The first sync is a full copy; the second is where rsync really saves you. The demo changed three things: notes.txt gained a line (from 36 bytes to 52), a new todo.txt appeared (9 bytes), and data.bin was deleted (102400 bytes). Then rsync -av --stats --delete src/ backup/ ran again, with one extra flag: --delete, meaning files deleted from the source are deleted from the backup too.

printf 'line four added\n' >> src/notes.txt  # 36 bytes -> 52 bytes
printf 'buy milk\n' > src/todo.txt              # new file, 9 bytes
rm src/data.bin                                # delete 102400 bytes
rsync -av --stats --delete src/ backup/

Measured: the second sync's Total file size is 51,316 bytes. The arithmetic checks out: the original 153,691, minus the deleted data.bin at 102,400, plus the 16 extra bytes in notes.txt and the new todo.txt at 9 bytes — 153,691 - 102,400 + 16 + 9 = 51,316. Total transferred file size is 61 bytes: the changed notes.txt was sent whole (52 bytes) plus the new todo.txt (9 bytes), 52 + 9 = 61. The first run moved 153,691 bytes; the second moved 61 bytes — more than 2,500 times less. Number of deleted files is 1, the deleted data.bin, removed from the backup as well. Counting again afterwards: backup still holds 6 files, the same as the source; diff -r src backup reports identical, exit code 0, the two sides exactly the same.

That 61 bytes deserves a closer look. rsync's delta algorithm works per block: unchanged parts of a file are not sent, only the changed blocks. But notes.txt is only 52 bytes here, smaller than a single block, so the whole file went across. The bigger the file, the more the delta algorithm saves: a 100 MB log file with a few lines appended only sends the new blocks, not the whole 100 MB. That is the fundamental reason rsync beats cp on time: cp copies everything every time, rsync only sends the delta.

Bar chart: transferred file size of the two syncs — 153,691 bytes first, 61 bytes second, log scale on the x axis

One more detail: think before adding --delete. It keeps the backup strictly identical to the source — delete from the source and the backup deletes too. But if the source deletion was a mistake, --delete spreads the mistake to the backup, and the backup cannot save you. The practical approach: daily syncs can use --delete to keep the backup clean, but keep important archives separately without --delete, or use --backup to move deleted files aside first. A backup exists to leave you a way back; the sync command itself must not cut that way back off.

4. The trailing-slash trap: src and src/ are two different things

rsync has one trap every beginner steps into: whether the source path ends in a slash changes the result completely. rsync -a src noslash/, without the slash, means copy the src directory itself into noslash, producing noslash/src/... rsync -a src/ withslash/, with the slash, means copy the contents of src into withslash, producing withslash/config.txt, withslash/notes.txt, and so on. Measured: the first form created three levels of directories — noslash, noslash/src, noslash/src/photos; the second form put the files directly in place — withslash/config.txt, withslash/notes.txt, withslash/photos/img1.jpg, and the rest. The trailing slash on the destination does not matter; the one on the source is what counts.

rsync -a src noslash/    # produces noslash/src/...
rsync -a src/ withslash/  # produces withslash/config.txt ...

The cost of this trap is real: write a backup script with src missing its slash, and the backup directory gains an extra src layer; at restore time you look for withslash/config.txt, cannot find it, and conclude the backup is broken. Add one slash too many, and the backup loses the src layer, restoring straight over the target directory's contents — also a mess. Remember one sentence: to copy the directory itself, no slash; to copy what is inside it, add the slash. Before putting a path into a scheduled job, test it on a small directory and look at the structure with ls.

One related note: if the destination does not exist, rsync creates it — no need to mkdir first. But if the destination's parent directory does not exist, rsync errors out, so a script must make sure the parent is there. In the demo, backup, noslash and withslash were created by the script in advance; in a real backup script, mkdir -p for the destination's parent is a line you should not skip.

5. tar: pack it into one file, list it, unpack it

rsync handles syncing; tar handles packing. Packing the whole source tree into one file is tar -czf site-20261126.tar.gz -C /tmp/rsync_demo257 src. -c means create, -z means compress with gzip, -f names the archive file, and -C switches to that directory before packing, so the paths inside the archive are src/... rather than a long absolute path.

tar -czf site-20261126.tar.gz -C /tmp/rsync_demo257 src
tar -tzf site-20261126.tar.gz   # list the contents, without unpacking
tar -xzf site-20261126.tar.gz -C restore  # unpack into restore

Measured: the archive came out at about 427 bytes. The source tree is 51,316 bytes and the archive is 427 bytes — a compression ratio that looks startling, but do not be fooled: the demo files are all zeros written by dd, and gzip compresses those to almost nothing. Real files do not compress like that; photos and videos are already compressed, and packing them again barely shrinks them. tar -tzf listed 8 entries: src/, src/todo.txt, src/config.txt, src/report.txt, src/notes.txt, src/photos/, src/photos/img2.jpg, src/photos/img1.jpg — 6 files plus 2 directories, matching the 8 entries rsync counted. Unpacking into a restore directory and running diff -r src restore/src gave identical, exit code 0 — the restored copy is exactly the same as the source.

Three lessons here. First, think about the paths inside the archive before packing: use -C to switch directories first, and the archive holds clean relative paths; pack /tmp/rsync_demo257/src directly and the archive holds a long absolute path that unpacks to the wrong place. Second, list the contents with -t before unpacking, to confirm what is inside and where it will land. Third, put the date in the archive name — site-20261126.tar.gz — so you can tell at a glance which day's archive it is and never grab the wrong one at restore time.

Keep tar's three main letters apart: -c creates an archive, -x extracts one, -t lists one; -z is gzip compression, -v is verbose, -f names the archive file. -f must be followed immediately by the archive name — tar -czf archive directory, in that order. Get the order wrong and tar treats the directory name as the archive name, with an error message that is hard to read. On formats, .tar.gz is the most universal, .tar.xz compresses smaller but slower, .tar.zst is the newer fast option; for everyday backups .tar.gz is enough.

6. Dry run: look before you sync

rsync has one more life-saving option: -n, or --dry-run. With -n, rsync only calculates — it does not really transfer anything — and prints which files it would send and which it would delete, so you can look first. In the demo, one more line was appended to notes.txt and then rsync -avn --stats src/ backup/ ran: the report said 1 file would transfer. The dry run itself sent nothing; the real backup stayed untouched until the -n was dropped and the command ran again.

rsync -avn --stats src/ backup/  # dry run: 1 file would move, nothing really did
rsync -av --stats src/ backup/   # drop -n, and the sync really happens

The rule is: any sync with --delete gets a dry run first, then the real run. --delete removes files, and a wrong deletion cannot be undone — the dry run is your chance to change your mind. When a rsync command first goes into a scheduled job, dry-run it once to confirm the source and destination are not swapped. Swapping them is the worst accident: rsync -av --delete backup/ src/ — a one-word difference — and the backup overwrites the source. In a dry run, one look at "which files would move" exposes a reversed direction immediately.

Dry runs also work for estimation. To know roughly how much a sync will move, dry-run with --stats: Total transferred file size is the answer. In the demo the dry run reported 1 file, and the real sync would move exactly that 1 file. Put a dry run at the top of a backup script and log one line — "this run is expected to move this much" — and when something goes wrong, the log shows which step it was.

Dry run plus --delete together is where the effect is clearest. Verified separately in a small probe: sync two files keep.txt and gone.txt, delete gone.txt from the source, then run rsync -avn --delete. The dry run output contains one line, deleting gone.txt — meaning "a real run would delete it". After the dry run, the backup still has gone.txt, and keep.txt too; the dry run changed nothing. That deleting line is what you check: is it the right file, the one you meant to delete? In a real run the same line appears, but by then the file is already gone, and looking is too late. Dry run first, see deleting, confirm it, then run for real — that is the correct order for --delete.

7. One complete backup drill: sync to archive, end to end

The sections above were taken apart; here they run as one complete backup drill, so you can see how daily use looks. Suppose your project source is the demo's src: 6 files, 153,691 bytes. Step one, rsync -av --stats src/ backup/: the first sync moves all 153,691 bytes, and backup now holds 6 files. Step two, edit code as usual, then sync again: this time notes.txt changed, todo.txt is new, data.bin is gone — rsync moves only 61 bytes, deletes 1 file, and backup matches the source again. Step three, at the weekend or before a release, pack a dated archive with tar -czf site-20261126.tar.gz -C /tmp/rsync_demo257 src: 8 entries in the listing, unpack it and diff -r says it is identical — that archive is this week's copy, stored somewhere else. Step four, dry-run before the next sync, confirming the direction is right and the deletions are the ones you expect. Syncing is the daily habit, archiving is the periodic one, dry runs are the insurance — the three together are a backup routine that works.

Restoring runs in reverse. First list the archive, tar -tzf site-20261126.tar.gz, to confirm it holds the day you want; then unpack into an empty directory, tar -xzf site-20261126.tar.gz -C restore; compare with diff -r, confirm it matches the archive exactly, and only then move it into place. Never unpack straight into the live location: if the archive holds a file with the same name, unpacking overwrites the current file with no confirmation. Unpack into an empty directory, compare, then move — two extra steps, but each one leaves you a chance to change your mind. A backup exists to leave you a way back, and every step of restoring should leave one too.

One more point that is easy to overlook: backups themselves need regular checks. An archive made and left in a corner, only opened six months later when it is needed, may turn out to be broken — which is the same as having no backup. Once a month, pick an old archive, unpack it, compare with diff -r: a few minutes to confirm the archive is good and the routine works. In the demo, diff -r returned identical with exit code 0 every time — that is the result you want from a check. A backup is not done when the archive is made; it is done when it unpacks and matches the source exactly.

8. Cheat sheet and checklist

The whole guide in one cheat sheet. rsync -av source/ destination/: daily sync, archive mode plus verbose. rsync -av --stats: check the statistics after syncing; Total transferred file size is how much moved this time. rsync -av --delete: delete from the backup what was deleted from the source — dry-run first. rsync -avn: dry run, look but do not touch. rsync -avc: compare by content checksum, safer but slower. Trailing slash on the source: with a slash it copies the contents, without it copies the directory itself. tar -czf archive.tar.gz -C directory source: pack it. tar -tzf archive.tar.gz: list the contents without unpacking. tar -xzf archive.tar.gz -C restore-dir: unpack it. diff -r source backup: check whether the two sides match.

Work through this checklist when setting up a backup routine. First, write down the source and the destination — do not type paths from memory. Second, on the first sync use --stats and check the file count and total size: in the demo, 6 files and 153,691 bytes — if they do not match, stop before going further. Third, change a few files and sync again, confirming the second run moved only the delta: in the demo, 61 bytes with 1 file deleted. Fourth, get the slash right: contents or the directory itself — check the structure with ls. Fifth, pack an archive with tar, list it, unpack it and compare with diff -r: in the demo, 8 entries in the listing and an identical restore. Sixth, dry-run any command with --delete, confirming the direction is right and only the right files are deleted. Seventh, put the date in the archive name and log one line per backup run, recording how much moved and how much was deleted.

One last note for vibe-coded projects. Many personal projects hit backup trouble on their first deployment: the source files exist only on the cloud host, and a bad edit, a wrong deletion or an expired host means the code is gone. Two lines in a deploy script are enough: rsync a copy to local storage, and tar a dated archive regularly. Backups are not a technical problem; they are a habit problem, and a habit is a checklist.

9. What this guide did not verify

An honest boundary. First, remote syncing was not demoed here: rsync to a remote host goes over ssh, rsync -av src/ user@host:destination/, and that command's form follows the official rsync documentation — this guide only ran locally, and keys, ports and bandwidth were not covered. Second, delta performance on large files was not measured: the biggest demo file is 102,400 bytes, and how much a 100 MB log file with a few appended lines saves is, in principle, only the changed blocks — the exact numbers need a real file to run on. Third, advanced options such as --backup and --exclude were not demoed: excluding node_modules and cache directories from a backup is essential in real projects, and the option forms follow the official docs. Fourth, the demo files are all zeros written by dd, so the archive's 427 bytes say nothing about real files — real photos and videos compress far less. Fifth, scheduled jobs, off-site backups and encrypted backups are operations topics this guide does not cover; when a backup needs to be regular, off-site and encrypted, follow operations practice from there.

Still, the core backup routine never changes: a full sync the first time, only the delta afterwards, a dry run before deleting, clean paths before packing, a listing before unpacking, and a diff check after restoring. rsync plus tar, two commands, one checklist — enough to set up backups for a small project. The rest is writing that routine into your own deploy script, and confirming the backup is there before you touch the code next time.

Views 0Comments 0

Comments (0)

ME
0/1000
Loading comments...

Sources

Browse projectsPublish your project

Related articles