Ravindra BagaleCourses & study guides मराठी Track your progress

Chapter 4: Linux Advanced Commands

AWS Cloud

4.5 Archiving and compression: tar, gzip, zip

Friends, taking a backup of the website, moving logs to another server or opening a downloaded WordPress – tar is needed everywhere. Remember the flags: c create, x extract, z gzip, v verbose, f file.

demo_archive=$(mktemp -d) || exit 1
mkdir "$demo_archive/input" "$demo_archive/recovery"
printf 'Workshop handover\n' > "$demo_archive/input/memo.txt"
tar -c -z -f "$demo_archive/memo.tar.gz" -C "$demo_archive" input
tar -t -z -f "$demo_archive/memo.tar.gz"
tar -x -z -f "$demo_archive/memo.tar.gz" -C "$demo_archive/recovery"
cmp "$demo_archive/input/memo.txt" "$demo_archive/recovery/input/memo.txt" &&
  printf 'Restored memo matches\n'
rm -- "$demo_archive/input/memo.txt" "$demo_archive/recovery/input/memo.txt" "$demo_archive/memo.tar.gz"
rmdir -- "$demo_archive/input" "$demo_archive/recovery/input" "$demo_archive/recovery" "$demo_archive"

Ravindra Bagale's Tip

List the archive before extracting. Set the destination with -C to a new empty directory. In a dash-prefixed group like -czf, put f last so the next argument names the archive. Writing -f archive.tar.gz separately makes that relationship explicit.

Lab

खाली दिलेल्या workshop lab मध्ये नवीन scratch directory वापरा. Archive तयार करून त्यातल्या paths ची यादी पहा, नंतर वेगळ्या रिकाम्या directory मध्ये restore करा. cmp वापरून दोन्ही फाइल्स मूळ प्रतींशी जुळतात का ते तपासा.

Decide what must come back after a bad change

The Mauli workshop publishes two static files for the morning shift: a handover page and the worksheet linked from it. A student is preparing to replace the page with a corrected version. If something goes wrong, the student must be able to restore the old page and the attached worksheet as a pair. Keeping only a screenshot of the page will not restore the link or the file it points to. Copying the visible page text is also different from capturing the file permissions, the directory structure, and the name of the worksheet. Before choosing a command, list the things the workshop must be able to recover and practice restoring them.

An archive represents one or more files in a container. It can record member names, directory relationships, content, and certain metadata. Metadata means information about a file, such as its timestamp or access mode, alongside the bytes you would read from it. A tar archive can store a directory and its children with paths that let an extractor reconstruct where each child belongs. For example, a member named site/assets/worksheet.txt points to three successive path components within the archive. Knowing the stored path matters when checking the restored copy.

Compression is a transformation that represents bytes more economically when it can, with enough information to recover the original bytes. For the tools in this lesson, decompression is lossless, so a successfully archived worksheet can come back byte for byte. A long repeated text log is often cheap to compress. A JPEG, a video, or a compressed upload may contain little redundancy for another compressor to remove. A compression header or archive bookkeeping can even make very small samples larger. Measure a representative fixture if saving disk or network capacity is part of your decision.

Tar by itself groups multiple entries. Gzip by itself compresses data through a stream, without representing a directory tree of individually addressable tar-like paths. When you see site.tar.gz, think of two layers. The gzip layer is opened to recover a tar byte stream. The tar layer describes the paths and contents you can list or extract. Zip combines packaging and compression in one format. Its entries can be compressed individually and have a directory of member information. A recipient working with a common desktop archive reader might prefer Zip. For a Linux restore, you also need to test how the tool you chose preserves the metadata your files depend on.

A bundle of workshop site files is a useful backup for this exercise. A real service might also depend on database tables, images outside the document root, symlink targets, environment settings, certificates, and configuration backups. List those dependencies separately. A directory archive captures bytes it sees while reading. If an application is writing to several files during the capture, the result need not represent one consistent moment. Database backups and application snapshots use the method supported by that application. Confirm the scope and the restore method before treating the presence of a tar file as recovery evidence.

Use a Bash shell in a Linux environment, such as your existing practice instance. The first stages need tar, gzip, and ordinary file utilities. The Zip comparison uses zip and unzip if present, and the optional XZ comparison needs the method available to your tar. No network, administrator permission, live website directory, or existing project folder is needed. The files below are new and belong to one scratch directory. Use the same shell for each command so that the scratch variable stays available.

pack_lab=$(mktemp -d) || exit 1
printf 'Lab directory: %s\n' "$pack_lab"
mkdir -p "$pack_lab/source/site/assets"
printf '%s\n' 'Morning handover' 'See assets/worksheet.txt' > "$pack_lab/source/site/index.txt"
printf '%s\n' 'Station A: inspect four units' 'Station B: inspect ten units' \
  > "$pack_lab/source/site/assets/worksheet.txt"
cat "$pack_lab/source/site/index.txt"
cat "$pack_lab/source/site/assets/worksheet.txt"
Morning handover
See assets/worksheet.txt
Station A: inspect four units
Station B: inspect ten units

The directory name printed before these lines depends on your machine. Inside it the source directory represents the original site root's parent. Below that sits the site directory containing the page and its folder of assets. Look at the paths and the relation between them before packaging. A correct restore in the exercise recreates a directory named site with the same nested assets path. You will compare both files to the original once the restore is available.

Create, list, and restore a tar archive outside the source tree

We want the smallest useful root name in the archive to be site. You could change into the parent directory or ask tar to change its operating directory with -C before reading the following member argument. Here the latter form makes the intended source and output explicit. The output archive uses an absolute path resolved under the scratch directory, outside the tree being archived. This keeps the archive from appearing in its own input selection. It also makes it easy to move or remove the backup independently later.

tar -c -z -f "$pack_lab/site.tar.gz" -C "$pack_lab/source" site
tar -t -z -f "$pack_lab/site.tar.gz" | LC_ALL=C sort
site/
site/assets/
site/assets/worksheet.txt
site/index.txt

The create mode -c asks tar to build an archive. The compression option -z asks it to use gzip. The file option -f consumes the next argument as the archive filename here. The final operand site identifies the source relative to the working directory set by -C. Tar walks the child entries automatically when it archives a directory. The archive has neither the full scratch path nor the extra source directory in its contents. That is convenient because the recovery directory may live on another machine with different parent paths.

Listing with -t reads member names without extracting them. Sorting the names produces this predictable display regardless of the order the entries were archived in. This pipe serves only to arrange the display. In automated work make sure you check that tar itself read the archive, for example by listing it without a pipe or by using a shell status check that includes the listing stage. A simple list is evidence about member names. Add -v to see modes, sizes, and kinds of entries where the implementation supplies those details. The exact columns of a verbose listing can vary by implementation and options.

Keep a known result beside the command when you learn the flags. The four drawn paths should appear in this example. If the output contains only the top directory and no expected worksheet, stop and inspect the source and options. If a parent name is unexpectedly long, check the working directory and the path you passed after -C. A pathname in an archive and a pathname on your live system are independent concepts. A correct relative archive path lets you restore into a separate folder with known structure rather than targeting the old location.

You will often encounter a compressed group of options such as tar -czf bundle.tar.gz site. The options c and z take no argument there. Put f last in that dash-prefixed group so the next argument is the archive name. If you write -czfv, the value following the option letter within the same group can be read as the archive filename v. With separately written options, -f archive.tar -v can work because the filename has already been supplied before the next flag. GNU tar's old-style no-dash forms have their own argument ordering rules. Spelling options separately in training avoids ambiguity.

Extract into a new empty directory. Use -x for extract, keep -z for the gzip layer, and pass the archive with -f. This time -C points at the output location because we are extracting. A relative member path such as site/index.txt is created below it. The scratch destination is newly made for this demonstration, so no previous files need to be overwritten.

mkdir "$pack_lab/restored"
tar -x -z -f "$pack_lab/site.tar.gz" -C "$pack_lab/restored"
cmp "$pack_lab/source/site/index.txt" "$pack_lab/restored/site/index.txt" \
  && cmp "$pack_lab/source/site/assets/worksheet.txt" \
    "$pack_lab/restored/site/assets/worksheet.txt" \
  && printf 'Both restored files match the source bytes\n'
cat "$pack_lab/restored/site/assets/worksheet.txt"
Both restored files match the source bytes
Station A: inspect four units
Station B: inspect ten units

The equality command cmp normally says nothing when the bytes match and returns status zero. The chained success message appears only if both comparisons passed. Printing the worksheet makes the content and path visible to a beginner. If you remove or replace a source file before comparing, you are testing the archive against a different source state. Preserve the intended expected copy or a checked manifest, which is a list of files and independently recorded verification values. A byte comparison confirms content and leaves other checks, such as expected modes and file ownership, to separate measurements.

Compress a single log and keep the source

Suppose the same workshop has a text log that contains many nearly identical lines. It needs to move that one file between two approved practice workspaces. There is no directory tree to reconstruct. A gzip stream around the log is sufficient for the content. A small archive operation often has a different goal from compressing this single file. Keep the two names and procedures distinct so another student will know whether to run an archive tool or just a decompressor.

awk 'BEGIN {
  for (i = 1; i <= 400; i++)
      printf "entry=%03d station=press-a status=passed\n", i
}' > "$pack_lab/inspection.log"
gzip -c "$pack_lab/inspection.log" > "$pack_lab/inspection.log.gz"
wc -c < "$pack_lab/inspection.log"
wc -c < "$pack_lab/inspection.log.gz"
gzip -t "$pack_lab/inspection.log.gz" && printf 'gzip stream integrity check passed\n'

The first command produces 400 log lines with incrementing entry numbers and otherwise repeated text. The three digits keep the row length consistent. gzip -c sends the compressed representation to standard output and keeps the input file. The shell redirects those compressed bytes into the separate name. Under ordinary options, calling gzip on a file without -c or another retention option can replace the source with a compressed name. Practicing with the streaming form lets you keep both versions for comparison.

The wc -c checks report byte counts. The original log is 16,000 bytes in the fixed lab input. The compressed byte count varies by gzip implementation and settings and should be considerably smaller for this repeated sample. A leading blank before a number is just display formatting. gzip -t reads and checks the compressed stream. It can detect certain kinds of corruption or incompleteness, and a successful test does not create a decompressed output file. Use the next step for separate evidence that this stream represents the intended source.

gzip -d -c "$pack_lab/inspection.log.gz" > "$pack_lab/inspection.recovered.log"
cmp "$pack_lab/inspection.log" "$pack_lab/inspection.recovered.log" \
  && printf 'Recovered log matches\n'
head -n 2 "$pack_lab/inspection.recovered.log"
Recovered log matches
entry=001 station=press-a status=passed
entry=002 station=press-a status=passed

The -d option selects decompression and -c keeps using stdout as the destination. The shell writes the recovered representation to a fresh path. The integrity check and the comparison answer distinct questions. The check shows the stream can be decoded and passes its built-in checks. The comparison shows the decoded output matches the source that the workshop expected. A valid gzip stream containing yesterday's log could pass its integrity check while failing this byte comparison.

For a very small source the compressed overhead may dominate, and the result might be bigger than the original. For numerous related files, compressing a whole tar stream can take advantage of repeated structures in the stream as a whole. Individual ZIP members can be compressed separately. Those different properties affect which files can be located or decoded independently, and why no one tool always wins the size comparison. In the workshop case, portability and the ability to restore a checked copy are required. A few extra bytes in a tiny training fixture make little difference.

An optional comparison uses XZ, another compression format. If your tar supports -J and its XZ compression is available, create a second compressed tar and list its members. The mode choices are the same as for the gzip-wrapped tar. Substitute -J for -z and use a consistent suffix. A command is successful here when the listing matches the same four paths and the archive can be read back on the target system with the appropriate tool.

tar -c -J -f "$pack_lab/site.tar.xz" -C "$pack_lab/source" site
tar -t -J -f "$pack_lab/site.tar.xz" | LC_ALL=C sort

The option is common with GNU tar and also available on other implementations, but check an unfamiliar machine's help. XZ can produce a smaller result on many inputs, and it can require more compression time or memory depending on settings. For a workload where both properties matter, compare the same representative data with explicitly chosen options. A filename suffix is a convention for identifying the format, not a command changing the bytes. Renaming a gzip stream to end in .xz would not run a new compressor.

Create a Zip for a receiver who needs individual files

A trainee needs the handover and worksheet on a desktop machine where a Zip reader is available. The two text files can be sent in a common archive format without relying on that machine to understand a compressed tar. The exercise uses Info-ZIP-style commands. Check for both programs before proceeding. If either is absent from the workshop environment, use another prepared machine for this optional portion. The tar and gzip checks can be finished independently.

command -v zip
command -v unzip

The paths printed depend on where your distribution installed the tools. The next block executes the cd and archive creation inside parentheses, a subshell. That preserves your working directory after the block finishes. The conditional operator following cd ensures the archive command runs only if the source directory was opened successfully. Because output names are under the absolute scratch path, names inside the archive can be kept relative without making the archive itself difficult to find.

(
  cd "$pack_lab/source" && zip -q -r "$pack_lab/site.zip" site
)
unzip -l "$pack_lab/site.zip"
unzip -t "$pack_lab/site.zip"

-r tells zip to include children of the specified directory. -q quiets the creation display. A listing with unzip -l should include the four paths you saw in the tar listing. It also includes sizes and dates. The sizes for the two text members in this fixture are 42 and 59 bytes. Directory entries may be displayed as zero length. The integrity test with unzip -t should report that the entries pass, with wording provided by your implementation. Existence of a previously created archive is weak evidence for a successful rerun, and a zip utility can update an existing archive depending on how it is invoked. Use a fresh output for a backup that must represent a new complete source set.

mkdir "$pack_lab/zip-restored"
unzip -q "$pack_lab/site.zip" -d "$pack_lab/zip-restored"
cmp "$pack_lab/source/site/index.txt" "$pack_lab/zip-restored/site/index.txt" \
  && cmp "$pack_lab/source/site/assets/worksheet.txt" \
    "$pack_lab/zip-restored/site/assets/worksheet.txt" \
  && printf 'ZIP restore matches both text files\n'
ZIP restore matches both text files

Make the extraction destination beforehand so you can inspect its path and know it was empty. Avoid extraction options that silently overwrite existing files when learning. If the destination contains another worksheet, you might mix the test recovery with previous content or overwrite someone else's training work. A separate fresh location gives a simpler success check and makes cleanup predictable. A useful way to verify a receiver's experience is to test extraction with the exact kind of reader they will use, then see whether the expected files open under the expected path.

A Zip can store certain protection or platform metadata through optional fields. Different tools and operating systems interpret that information differently. Passing a text-file comparison does not prove that a program stays executable or that ownership and access controls will be the same on a different operating system. State the needs of your receiver first. In this exercise the criterion is faithful normal text in two known locations. For a Linux service restore, check the permissions and owner requirements explicitly before you replace production files.

Check what each piece of evidence actually establishes

The workshop wants to hand over a backup with three assurances. It should be possible to read the container, find the exact paths needed by the restore, and confirm the restored data is the data that was captured at handover time. Each assurance needs separate evidence. A listing is quick and shows paths. A built-in integrity test checks a format's ability to decode and verify its contents. A restored-file comparison checks the output's meaning for the recipient. A report should say which assurances were tested instead of recording only that the bundle existed.

Try a deliberate failure on a disposable copy. Take only the first twelve bytes of the training gzip file and save them under a different name. This file has too little data to contain the complete original log and its compressed stream's trailer. Never use this exercise on an archive being kept for recovery. The test checks whether your command visibly distinguishes these bytes from the complete training stream. The diagnostic is captured into a separate scratch file for inspection.

head -c 12 "$pack_lab/inspection.log.gz" > "$pack_lab/truncated.log.gz"
if gzip -t "$pack_lab/truncated.log.gz" 2> "$pack_lab/truncated-diagnostic.txt"; then
  printf 'Unexpectedly accepted the incomplete gzip copy\n'
else
  printf 'Incomplete gzip copy was rejected\n'
fi
cat "$pack_lab/truncated-diagnostic.txt"

The expected first message is Incomplete gzip copy was rejected. A diagnostic follows, with exact wording depending on the gzip implementation. If it reported a failure opening the destination instead of a failure reading the compressed stream, fix the lab path and run it again. The complete archive is still available for the good case. Practicing a deliberate failure makes it easier to recognize the difference between a validation step that ran and one that actually verified the right data.

A cryptographic digest is a value computed from bytes that can be compared with an independently known value. A SHA-256 digest of the submitted tar file can verify that the same archive arrived at a second location. It has a fixed 64 hexadecimal characters in its usual display, representing 256 bits. In routine operations it detects accidental changes with high confidence. The expected digest must come from a place you trust. Generating a new digest from whatever file arrives and comparing that digest with itself does not establish which file was sent.

command -v sha256sum
(
  cd "$pack_lab" && sha256sum site.tar.gz > site.tar.gz.sha256
)
mkdir "$pack_lab/received"
cp "$pack_lab/site.tar.gz" "$pack_lab/site.tar.gz.sha256" "$pack_lab/received/"
(
  cd "$pack_lab/received" && sha256sum -c site.tar.gz.sha256
)
site.tar.gz: OK

The first command prints the installed executable path, which is omitted above. If sha256sum is unavailable, treat this as an optional comparison exercise and use the raw cmp check on the two files you have locally. In the Linux digest example, generating the manifest from within the directory records the relative name site.tar.gz. The received copy has that same name, which lets the check read it from its new parent directory. If you generate the manifest with an absolute pathname, a later verification command could read the original archive by mistake. The test is meant to verify the copy, not the original.

For a real handover the archive and the manifest in this lab would not magically confer authenticity on each other. If someone replaced both, a matching check could be produced for different content. Transfer the expected hash over an authenticated channel or validate a signature when that is part of the sender's process. A dropped transfer, an out-of-date archive, and an untrusted source are different problems. Comparing hashes is helpful when the expected hash is tied to the intended source. A container's internal corruption check is useful evidence about the container itself and has a different purpose.

To test a negative result, copy a different valid archive under the same candidate name in a new scratch copy and keep the original expected digest. The check should fail because the bytes are different. Here the mismatch would mean the files differ, while inspecting the source is needed to know why. Repackaging the same two text files could produce different archive bytes due to timestamps, mode metadata, entry order, or compressor choices. If the requirement is equivalence of restored content rather than identical archive bytes, extract to separate directories and compare the files or a manifest of original member hashes.

Plan recovery before choosing where to extract

A useful recovery step starts with a known archive and an empty destination. Ask who created it, when those source bytes were captured, and whether it includes what the workshop needs. In the lab you created the archive yourself from two plain text files. In a real environment, only use files from an approved source, keep archive tools patched, and verify any signed digest supplied by that source. List the names and types before you extract. Unexpected files, links, special types, or a layout that does not match the provider's instructions are reasons to stop and investigate rather than promote the extraction. The filename suffix alone offers no assurance about either source or contents.

Archive extractors can create more than plain files. A symbolic link can point to another filesystem path, and a hard link can create another name for a file's underlying data. A restore can need those features, which is why the extraction behavior and policy should be explicit. For this small workshop fixture the expected entries are two directories and two ordinary text files. Open untrusted content only in an appropriately isolated, least-privilege environment intended for that purpose, and use supported extraction protections for the tool and platform. Listing members is a review aid. It does not certify the safety of interpreting every file or of running anything extracted.

Estimate the restored size as well as the download size. A compressed stream may fit comfortably in a small directory while producing far more bytes when decoded. Depending on the number of members, available file entries on the filesystem can also be a limit. Make sure your recovery environment has the expected space and privileges. Avoid using a root shell for a preliminary restore, because more privileged extraction can apply ownership and modes or affect locations the normal account cannot. Keep the output within a private recovery area, then inspect the content and metadata the application will actually use.

For a static website, open the page or read its source in the recovery copy and verify that each linked resource can be found in the expected location. In this lab that means reading index.txt and confirming that its relative reference resolves to assets/worksheet.txt under the same site directory. In a new recovery environment, an application may also depend on owner, directory traversal access, and which path the web server is configured to serve. Review those before moving anything into service. An exact byte comparison is strong evidence for these two text files, and running the application in a staging copy tests a further layer.

Record the boundary of any actual change. A site archive created from one document root may omit the web server's configuration, environment, media outside that directory, or database. If two websites share a machine, restoring a global web root or a full server config for one site can affect the other. Use the actual site-specific paths for the change and retain the current versions. Back up necessary configuration separately, including the dependencies that make that particular service work, and test it using its documented validation command. This lab intentionally limits the operation to known scratch files so the recovery procedure can be practiced on scope you completely understand.

File modes, ownership, access-control lists, and extended attributes are different parts of metadata. Environment and tool options determine what tar captures and what the extractor can apply. A user without privileges may not be able to restore the original owner. The permission mask and chosen extraction flags can affect the final mode as well. Review tar -t -v for stored values and inspect the extracted filesystem separately. Avoid assuming two paths are operationally equivalent simply because their bytes match. A document root whose directories cannot be traversed by the intended service account can still be unavailable even when every page was recovered.

Compare the two restored paths with ls -ld before assuming they behave as the originals. The letter at the start of each mode display describes the type, and the next nine characters traditionally show permissions for owner, group, and everyone else. A directory needs traversal access for an account to follow a path through it. The page and worksheet files themselves need the intended read access. A training directory produced by mktemp -d is normally private to its owner, so it can intentionally block another account at a parent directory even when the file is otherwise readable. Keep that distinction in mind when testing a recovery in a different account. In this lab the student performing the comparison owns the scratch directory.

Record the time and version independently of the members' timestamps. A worksheet file last edited on Monday might be captured in a Thursday backup along with a Thursday page, and that is a valid Thursday archive if the page still links to that worksheet. The archive file's timestamp might instead mark when a copy was received. An explicit capture time, source scope, and any stopped writers are clearer recovery evidence than choosing the newest filesystem time. If you need to preserve links or extended metadata, test a representative example before agreeing to a migration and record the particular tool and options that passed.

For an exercise in scope, write an inventory of the complete workshop recovery and mark which item each command provides. The page and worksheet belong to the file archive. The manifest describes the archive bytes. The inspection log uses its own gzip stream and does not appear in the site member listing. If the supervisor requests that log during a recovery, the correct action is to consult the inventory and retrieve it separately. Neither archive should claim to include everything on the machine. This inventory also explains why successfully restoring the site tree does not produce the log or create a running web service automatically.

Use the member name the archive actually stores

Imagine that the updated workshop page is correct and the mistakenly altered file is only the worksheet. To prepare a candidate for restoring that one file, start with the listing you already reviewed. The stored member name is site/assets/worksheet.txt. A tar extraction selecting that name reads the archive and emits the indicated member. Using -O with GNU tar in extraction mode sends file contents to standard output. The next command selects a single known regular file and redirects those bytes to a new candidate. It does not use a live worksheet path.

tar -x -O -z -f "$pack_lab/site.tar.gz" site/assets/worksheet.txt \
  > "$pack_lab/worksheet.candidate.txt"
cat "$pack_lab/worksheet.candidate.txt"
cmp "$pack_lab/worksheet.candidate.txt" "$pack_lab/source/site/assets/worksheet.txt" \
  && printf 'Selected member matches the original worksheet\n'
Station A: inspect four units
Station B: inspect ten units
Selected member matches the original worksheet

The redirect can create an empty candidate even if tar cannot locate a member. Read the command's status and verify the result before using it. Requesting worksheet.txt alone would give a different member name. The request must include the stored prefix, or you need reviewed options for selecting a pattern. Using the single exact name from the listing gives a repeatable choice. Avoid bulk matching when your task concerns one file. Some archive utilities can encounter more than one entry with a repeated member name, so a real reviewed archive should also have an unambiguous listing for any item you plan to restore.

The top-level path is chosen when you create the archive. Try a whole directory archive that places the site contents directly at the extraction root by changing into that site and packaging the current directory. The relative names now start under a dot directory representing the chosen root. This is a different arrangement from packaging the named site directory from its parent, even with identical text file bytes inside. Decide which layout the restore expects and record that as part of the backup instructions.

tar -c -z -f "$pack_lab/contents.tar.gz" -C "$pack_lab/source/site" .
tar -t -z -f "$pack_lab/contents.tar.gz" | LC_ALL=C sort
./
./assets/
./assets/worksheet.txt
./index.txt

This distinction explains a common apparent disappearance after extraction. A recipient may look for the page one level too high because they expected the packaging to store only the contents. The archive listing tells you how to set that recovery directory. Keeping a named top-level directory can make it easier to identify what was recovered. Packaging the contents through the dot operand can make the destination a ready-made folder of files, and it includes ordinary hidden entries within the tree as part of traversing that directory. If you use a shell wildcard instead, its expansion rules determine which names tar is given.

Binary packaging and compression tools work on byte streams rather than interpreting the worksheets as reports. They are usually agnostic to the meaning of accepted files. Your recovery test supplies the meaning by naming what must be present and interpretable. For example, a five kilobyte tar file could be a perfectly readable archive that has the wrong image folder. If you capture only terse archive success messages, a future operator might not know which directory was actually handed to the command. A saved listing alongside the archive helps connect that evidence to the plan.

Run a restore drill and clean the test data

For the first practice task, decide which package you would share for a desktop trainee and which you would keep to test a Linux restore. Make the decision based on the intended reader and verify both by restoring the same two files when the matching tools are available. State what your checks demonstrate. In this lab a correct report includes the two expected file paths, equality of both text files, and the exact top-level directory that appeared after extraction. A correct Zip report does not need to claim that every kind of Linux metadata was preserved.

For the second task, make a distinct new version of the worksheet in a fresh scratch copy, changing the station B count from ten to eight. Keep the original fixture and archive. Pack the updated copy into a different archive name and compare its restored content with the two source states. A comparison against the new source should pass, and a comparison against the original should show a difference. List both archives and observe that their names can describe the same layout even while their contents represent different points in time. There is no need to remove the old archive or overwrite a recovered copy to make that distinction visible.

For the third task, have one student write a short restore note for a partner who has only the archive and the expected digest. Include the expected member paths, required utilities, which new directory to choose, and what to compare. The other student should be able to tell which file is the handover root and where its worksheet belongs. If the partner receives a corrupted file, a missing optional tool, or a valid bundle with unexpected paths, the note should tell them where to stop and what evidence to inspect. A clearly stated success condition is more useful than a long chain of flags with no explanation.

Diagnose a failing drill in the order evidence becomes available. If creation fails, check access to the input tree, the existence of the output directory, and free space. If listing fails, read the error and check the actual compression format and transfer integrity. If extraction succeeds and a compare fails, find which member differs and verify that the test refers to the intended source version. If the bytes match but a service cannot read the captured files, inspect relevant permissions and paths from the perspective of its account. In each case a new archive produced without understanding the symptom can hide the root cause.

Clean up only after checking the generated directory corresponds to this lesson. The cleanup covers the explicitly named files, including optional demonstrations. Each directory is removed only if it is empty. If you completed extra practice tasks and made additional directories, list the scratch contents, identify what you created, and remove those known additions separately. Keep any verification notes you intend to hand over.

if [ -n "${pack_lab:-}" ] && [ -d "$pack_lab" ]; then
  printf 'Cleaning lesson fixtures in %s\n' "$pack_lab"
  rm -f -- "$pack_lab/source/site/index.txt" "$pack_lab/source/site/assets/worksheet.txt" \
    "$pack_lab/restored/site/index.txt" "$pack_lab/restored/site/assets/worksheet.txt" \
    "$pack_lab/zip-restored/site/index.txt" "$pack_lab/zip-restored/site/assets/worksheet.txt" \
    "$pack_lab/site.tar.gz" "$pack_lab/site.tar.xz" "$pack_lab/contents.tar.gz" \
    "$pack_lab/inspection.log" "$pack_lab/inspection.log.gz" "$pack_lab/inspection.recovered.log" \
    "$pack_lab/site.zip" "$pack_lab/truncated.log.gz" "$pack_lab/truncated-diagnostic.txt" \
    "$pack_lab/worksheet.candidate.txt" "$pack_lab/site.tar.gz.sha256" \
    "$pack_lab/received/site.tar.gz" "$pack_lab/received/site.tar.gz.sha256"
  for name in source restored zip-restored; do
    if [ -d "$pack_lab/$name/site/assets" ]; then rmdir -- "$pack_lab/$name/site/assets"; fi
    if [ -d "$pack_lab/$name/site" ]; then rmdir -- "$pack_lab/$name/site"; fi
    if [ -d "$pack_lab/$name" ]; then rmdir -- "$pack_lab/$name"; fi
  done
  if [ -d "$pack_lab/received" ]; then rmdir -- "$pack_lab/received"; fi
  rmdir -- "$pack_lab"
fi

Explain your backup choices

Interview question. Which operation is archiving and which is compression in a tar.gz file?

Tar records the site directory, member paths and content. Gzip represents that tar stream as compressed bytes that can be decoded back into the stream. The order matters when reading: open the gzip layer and then interpret the resulting entries as tar. A plain compressed log can be decoded by gzip to get a log, without any tar members to list.

Interview question. What would convince you a backup can recover the service?

Name the required files, data stores, configuration, and intended version. Restore the archive into a separate approved location, check member content against a known manifest or source, and verify the metadata and dependencies the service needs. For a complete service, test it in its recovery environment. Integrity tests help detect damaged containers, while the restore test checks the actual recovery objective.

Quiz. Why did extracting into a new folder create restored/site/index.txt?

The archive stored site/index.txt rather than just index.txt. Extraction places that relative path below the destination directory. The create-time choice to run tar with its directory set to the parent and name site produced that prefix. The listing is the evidence to consult before you assume a path.

Quiz. A downloaded archive passes a local integrity test. Can it still be the wrong backup?

Yes. A complete archive for a different version can satisfy its internal checks. Compare it against the expected archive digest from a trustworthy source and verify the restored file paths and contents. Use the checksum to check identity of bytes and the restore test to check whether those bytes support the intended recovery.

Quiz. What does switching from gzip to XZ change in the tar exercise?

It changes how the tar stream is compressed. The source directory and relative member paths remain the same if the tar input selection is unchanged. The resulting archive can have a different size, compression time, and compatibility with the target environment. Measure those characteristics against the actual data and required reader.

Revision cards

Archive names are a restore map

List before extracting. Decide where each stored relative path should land and use a new empty destination for the test.

Keep content and storage checks distinct

Read a valid container, check the expected bytes, then inspect the restored files and required metadata. Each check answers a different recovery question.

Compress representative data

Repetitive logs often shrink. Already compressed files may gain little. Choose the format using actual data and tools available to the receiver.

Primary references