02 / FILES, ACCOUNTS, AND SERVICES · LESSON 06

Copy and verify a configuration backup

Create a sandbox-local configuration copy and verify its saved bytes with a manifest.

15 min · learn at your pace

A copy deserves verification

This exercise makes real copies inside the browser's virtual filesystem. Enumerate the three supplied files in live-config, create a separate backup-run directory, and copy each file under the same basename. Include the empty file; zero bytes is still a valid file to preserve.

Use literal source paths when copying and hashing the bracketed filename. A failure to read or copy an input should stop this small backup operation rather than continue to a misleading success report.

Get-ChildItem '/home/user/live-config' -File | Select-Object Name, Length

Record what was saved

Hash both source and destination with SHA-256 after the copy. Matching hashes establish that those bytes agree at this point in the lab. Write a manifest outside both folders with Name, Bytes, and the destination's SHA256. Export-Csv handles quoting without hand-built comma-separated strings.

A verified copy is not a complete backup strategy. It does not prove that an application can restore successfully, that permissions and dependencies were preserved, or that a second failure cannot destroy both copies. The browser filesystem is temporary and shares the same device as your lesson.

This task uses a separate destination and leaves the source untouched. In a real workflow, agree on overwrite behaviour, independent storage, retention, access, consistency, and restore testing. Here you practise the narrower guarantee that the recorded copied bytes match the supplied configuration files.

Try an example.ps1
Get-FileHash -LiteralPath '/home/user/live-config/app.json' -Algorithm SHA256
YOUR TURN

Put it into practice.

Copy every file directly inside live-config into /home/user/backup-run using the same filename. Write /home/user/backup-manifest.csv with Name, Bytes, and SHA256 for each copy. Verify that copied bytes match their sources and leave sources unchanged. Include the empty file and bracketed filename; keep the manifest outside both directories.

See an explained solution
One way to solve it.ps1
New-Item -ItemType Directory -Path '/home/user/backup-run' -Force | Out-Null
$manifest = foreach ($file in Get-ChildItem '/home/user/live-config' -File) {
    $destination = Join-Path '/home/user/backup-run' $file.Name
    Copy-Item -LiteralPath $file.FullName -Destination $destination -ErrorAction Stop
    $sourceHash = (Get-FileHash -LiteralPath $file.FullName -Algorithm SHA256).Hash
    $copyHash = (Get-FileHash -LiteralPath $destination -Algorithm SHA256).Hash
    if ($sourceHash -ne $copyHash) { throw 'Copied bytes do not match.' }
    [pscustomobject]@{ Name=$file.Name; Bytes=$file.Length; SHA256=$copyHash }
}
$manifest | Export-Csv '/home/user/backup-manifest.csv' -NoTypeInformation

Literal paths preserve the exact filenames, and destination hashes verify the saved content. The manifest remains separate from the input and copied files so a later enumeration does not accidentally include its own report.