Leanpub Header

Skip to main content

Backup Cookbook with Bash

A bash cookbook for one nightly backup script that refuses, loudly, instead of failing quietly

Backup Cookbook with Bash
This book is 100% completeLast updated on 2026-11-04

What if your backups have been lying to you? A script can copy to a missing drive every night and still report success. This cookbook teaches you to write a bash backup script that refuses loudly the moment anything goes wrong — so the first night it fails is the night you find out.

Minimum price

$50.00

$69.99

You pay

Author earns

$

Also available for 2 book credits with a Reader Membership

PDF
EPUB
WEB
APP
About

About

About the Book

Your backup script can lie to you. It can copy files to a drive that was unplugged eleven weeks ago, report success every single night, and never say a word.

Backups That Tell You the Truth is a hands-on cookbook that teaches you to write a bash backup script that refuses, loudly, the moment anything is wrong. In twenty-four short chapters you build the script one guard at a time: each recipe adds a check that stops a real failure — a missing drive, a full disk, a wrong destination — and shows you how to prove the check works by breaking it on purpose.

Written for beginners, and for readers whose first language is not English, the book puts every line you type on the page and every command's real output in front of you. By the end you own a backup you can trust — because it tells you the truth.

Share this book

Categories

Author

About the Author

Muhammad Yaseen Rasheed

Muhammad Y. Rasheed is a systems administrator working in Linux system administration, virtualization, and network infrastructure, with a long-running homelab practice spanning Proxmox, Docker, and VPN/tunneling setups, ..etc. He was born and raised in Iraq — ancient Mesopotamia, the cradle of civilization, where writing and the first codified laws began — and traces a direct line from that tradition to his own work: turning operational knowledge earned the hard way into something written down, tested, and repeatable. Zero to Hero Bash began as backup_docs.sh, a production backup tool he built for his own infrastructure, with a Git history designed from the start to double as a teaching path. He treats "boring" infrastructure work — backups, logging, error handling — as the most important code in any system.

Contents

Table of Contents

Backups That Tell You the Truth

  1. The night nothing went wrong
  2. Why the folder was still there
  3. The one idea
  4. What this book is
  5. Who this book is for
  6. What you will be able to do
  7. The one rule
  8. How to use this book
  9. About the language in this book
  10. A note on honesty
  11. Where to start
  12. Part 1 — Start here

Run it, and know whether it worked

  1. Learning objectives
  2. Key terms
  3. Why this matters
  4. The lesson
  5. Step 1. A command answers twice
  6. Step 2. Make it fail on purpose
  7. Step 3. The number is replaced every time
  8. Step 4. Your first script
  9. Step 5. Say no yourself
  10. Common mistakes
  11. Check yourself
  12. Lab — predict, then run
  13. Summary

One word or two

  1. Learning objectives
  2. Key terms
  3. Why this matters
  4. The lesson
  5. Step 1. A variable holds a piece of text
  6. Step 2. Watch the shell chop it
  7. Step 3. What the chopping does to a real check
  8. Step 4. Three answers, three meanings
  9. Step 5. Why nobody caught it
  10. Step 6. Your first tool
  11. Common mistakes
  12. Check yourself
  13. Lab — predict, then run
  14. Summary

Ask before you act

  1. Learning objectives
  2. Key terms
  3. Why this matters
  4. The lesson
  5. Step 1. if runs a command
  6. Step 2. [ is a command with a one-character name
  7. Step 3. Give check.sh both branches
  8. Step 4. Your first test file
  9. Step 5. Two details doing real work
  10. Step 6. Build the print-shop bug
  11. Step 7. The form this book’s tool uses
  12. Step 8. The fix reads the words
  13. Common mistakes
  14. Check yourself
  15. Lab — predict, then run
  16. Summary

Stop when something is wrong

  1. Learning objectives
  2. Key terms
  3. Why this matters
  4. The lesson
  5. Step 1. A failed line does not stop the script
  6. Step 2. set -e stops it
  7. Step 3. The failure set -e must ignore
  8. Step 4. Or else — || and &&
  9. Step 5. set -u and the name you mistyped
  10. Step 6. pipefail and the failure at the front of the queue
  11. Step 7. The header goes in — and what it does not fix
  12. Step 8. What the header cannot see, your tests can
  13. Common mistakes
  14. Check yourself
  15. Lab — predict, then run
  16. Summary

Name the check

  1. Learning objectives
  2. Key terms
  3. Why this matters
  4. The lesson
  5. Step 1. A block of commands with a name
  6. Step 2. A function answers like a command
  7. Step 3. The name you did not mean to share
  8. Step 4. local keeps the name inside
  9. Step 5. die — the guards’ refusal, built
  10. Step 6. return hands the answer back; exit ends everything
  11. Step 7. Why you run scripts, and stop sourcing them
  12. Step 8. The rewrite your tests did not notice
  13. Common mistakes
  14. Check yourself
  15. Lab — predict, then run
  16. Summary

Values you can trust

  1. Learning objectives
  2. Key terms
  3. Why this matters
  4. The lesson
  5. Step 1. Set, and holding nothing
  6. Step 2. Demand a real value at the door
  7. Step 3. The colon is what catches the blank
  8. Step 4. The other honest answer — a default
  9. Step 5. Refuse in your own words
  10. Step 6. Capture an answer
  11. Step 7. The captures that lie
  12. Step 8. The assertion you were told to leave out
  13. Common mistakes
  14. Check yourself
  15. Lab — predict, then run
  16. Summary

Do it to each of them

  1. Learning objectives
  2. Key terms
  3. Why this matters
  4. The lesson
  5. Step 1. A list is not a string
  6. Step 2. The ghost files
  7. Step 3. The loop that ran zero times
  8. Step 4. The guard that inspects the wrong thing
  9. Step 5. Count before you loop
  10. Step 6. Every folder, one run, every fault
  11. Step 7. The recipe where the suite goes red
  12. Common mistakes
  13. Check yourself
  14. Lab — predict, then run
  15. Summary

Prove a check can fail

  1. Learning objectives
  2. Key terms
  3. Why this matters
  4. The lesson
  5. Step 1. The two names the guard compares
  6. Step 2. The line that lets a file be read
  7. Step 3. Testing a script’s insides
  8. Step 4. Break it on purpose
  9. Step 5. Assert the message, and the pair
  10. Step 6. The names the harness owns
  11. Step 7. Reading a suite you did not write
  12. Step 8. Why a suite has more than two tests
  13. Step 9. What this harness does not prove
  14. Common mistakes
  15. Check yourself
  16. Lab — predict, then run
  17. Summary
  18. Part 2 — The guards

Load the settings, and refuse a bad one

  1. Learning objectives
  2. Key terms
  3. Why this matters
  4. The lesson
  5. Step 1. A file that only sets variables
  6. Step 2. The hole arrives at load time
  7. Step 3. Demand each setting where it enters
  8. Step 4. A list is present before it is long
  9. Step 5. The gate, assembled — and the file it must not trust
  10. Step 6. The name that meant two files
  11. Step 7. Prove it, in both directions
  12. Step 8. What this gate does not prove
  13. Common mistakes
  14. Check yourself
  15. Lab — predict, then run
  16. Summary

Prove the drive is really there

  1. Learning objectives
  2. Key terms
  3. Why this matters
  4. The lesson
  5. Step 1. The empty husk, built to order
  6. Step 2. Two questions, read apart
  7. Step 3. The guard, and what it refuses
  8. Step 4. What this check does not prove
  9. Step 5. Prove it — the sabotage suite
  10. Step 6. The measurement, in both directions
  11. Common mistakes
  12. Check yourself
  13. Lab — predict, then run
  14. Summary

Keep the backup inside the drive

  1. Learning objectives
  2. Key terms
  3. Why this matters
  4. The lesson
  5. Step 1. A path is a name, not a place
  6. Step 2. The obvious check, predicted and run
  7. Step 3. The check that works
  8. Step 4. A destination that does not exist yet
  9. Step 5. Prove it
  10. Step 6. The measurement, and the break it cannot measure
  11. Step 7. What this check does not prove
  12. Common mistakes
  13. Check yourself
  14. Lab — predict, then run
  15. Summary

Every source must still be there

  1. Learning objectives
  2. Key terms
  3. Why this matters
  4. The lesson
  5. Step 1. A dropped share, built to order
  6. Step 2. The gate that stops too early
  7. Step 3. Ask every folder, then answer once
  8. Step 4. Two branches, each right for its own reason
  9. Step 5. The caller decides
  10. Step 6. What this check does not prove
  11. Step 7. Prove it, in both directions
  12. Common mistakes
  13. Check yourself
  14. Lab — predict, then run
  15. Summary

Make sure there is room

  1. Learning objectives
  2. Key terms
  3. Why this matters
  4. The lesson
  5. Step 1 The check you would write first
  6. Step 2 The floor with a unit typed into it
  7. Step 3 Validate the number where it enters
  8. Step 4 Two failures, two numbers
  9. Step 5 What the suite must not trust
  10. Step 6 The measurement, in both directions
  11. Step 7 What this check does not prove
  12. Common mistakes
  13. Check yourself
  14. Lab — predict, then run
  15. Summary

Do not let two runs overlap

  1. Learning objectives
  2. Key terms
  3. Why this matters
  4. The lesson
  5. Step 1 Two runs, one number
  6. Step 2 The first fix: a marker file
  7. Step 3 The crash that jams it shut
  8. Step 4 The gate: let the kernel referee
  9. Step 5 The referee at work
  10. Step 6 The crash, revisited
  11. Step 7 What the suite must prove
  12. Step 8 Break it on purpose, in both directions
  13. Step 9 What this gate does not prove
  14. Common mistakes
  15. Check yourself
  16. Lab — predict, then run
  17. Summary

Clean up, and say why you died

  1. Learning objectives
  2. Key terms
  3. Why this matters
  4. The lesson
  5. Step 1 The script that dies saying nothing
  6. Step 2 The cleanup trap: tidy on every way out
  7. Step 3 The report trap: the death explains itself
  8. Step 4 What the report trap deliberately ignores
  9. Step 5 Where the traps live, and the suite that vanished
  10. Step 6 The death no trap can answer
  11. Step 7 What the suite must prove
  12. Step 8 The measurement, in both directions
  13. Step 9 What this pair does not prove
  14. Common mistakes
  15. Check yourself
  16. Lab — predict, then run
  17. Summary

The name must resolve, the port must answer

  1. Learning objectives
  2. Key terms
  3. Why this matters
  4. The lesson
  5. Step 1 The probe with no clock
  6. Step 2 The wait that is not a failure
  7. Step 3 Two questions, two gates
  8. Step 4 Why getent, and not the tool everyone reaches for
  9. Step 5 Two seconds, on purpose
  10. Step 6 The hostname that tries to be a command
  11. Step 7 What the suite must prove
  12. Step 8 The measurement, in both directions
  13. Step 9 What this pair does not prove
  14. Common mistakes
  15. Check yourself
  16. Lab — predict, then run
  17. Summary

Trust the folder, not just the file

  1. Learning objectives
  2. Key terms
  3. Why this matters
  4. The lesson
  5. Step 1. The check you would write
  6. Step 2. Watch it accept a file it never saw arrive
  7. Step 3. Why the folder decides
  8. Step 4. The gate
  9. Step 5. The suite
  10. Step 6. The measurement, in both directions
  11. Step 7. What this gate does not prove
  12. Common mistakes
  13. Check yourself
  14. Lab
  15. Summary

Never run the destructive form by accident

  1. Learning objectives
  2. Key terms
  3. Why this matters
  4. The lesson
  5. Step 1. The check you would write
  6. Step 2. The three commands it cannot tell apart
  7. Step 3. What an authorisation has to name
  8. Step 4. The gate
  9. Step 5. The suite
  10. Step 6. The measurement, in both directions
  11. Step 7. What this gate does not prove
  12. Common mistakes
  13. Check yourself
  14. Lab
  15. Summary

Say what you excluded

  1. Learning objectives
  2. Key terms
  3. Why this matters
  4. The lesson
  5. Step 1. The check you would write
  6. Step 2. Watch three lists, and count what arrives
  7. Step 3. First match wins, and what that costs
  8. Step 4. The gate
  9. Step 5. The suite
  10. Step 6. The measurement, in both directions
  11. Step 7. What this gate does not prove
  12. Common mistakes
  13. Check yourself
  14. Lab
  15. Summary
  16. Part 3 — The test suite

Run them all

  1. Learning objectives
  2. Key terms
  3. Why this matters
  4. The lesson
  5. Step 1. One suite, one report
  6. Step 2. A second suite, and the problem
  7. Step 3. The runner, typed whole
  8. Step 4. Run them all
  9. Step 5. A suite that cannot fail
  10. Step 6. A suite whose tests never ran
  11. Step 7. Break something, and read the red
  12. Step 8. The toolkit’s own battery
  13. Step 9. What this runner does not prove
  14. Common mistakes
  15. Check yourself
  16. Lab — predict, then run
  17. Summary
  18. Part 4 — Build it

Build it

  1. Learning objectives
  2. Key terms
  3. Why this matters
  4. The lesson
  5. Step 1. Two mount points your machine already has
  6. Step 2. The frame
  7. Step 3. Twelve files, sourced once
  8. Step 4. main opens: traps, usage, the config’s folder, the config
  9. Step 5. The config, and its mode
  10. Step 6. The drive folder, and a server that is not there
  11. Step 7. The rules, checked before rsync sees them
  12. Step 8. The copy, rehearsed
  13. Step 9. The first real run, then the second
  14. Step 10. The deleting form, refused and then authorised
  15. Step 11. Two runs, one lock
  16. Step 12. The report when something nobody watched fails
  17. Step 13. The wrapper cron runs, and the suites
  18. Step 14. What this build does not prove
  19. Common mistakes
  20. Check yourself
  21. Lab — predict, then run
  22. Summary
  23. Part 5 — When it breaks

When it breaks

  1. How to read this part
  2. It says success and the drive is empty
  3. It filled the system disk
  4. It hangs
  5. It ran twice
  6. It refuses, and I think it is wrong
  7. When none of these fit

Appendix — The library

  1. The refusal everything shares
  2. From Prove a check can fail
  3. From Load the settings, and refuse a bad one
  4. From Prove the drive is really there
  5. From Keep the backup inside the drive
  6. From Every source must still be there
  7. From Make sure there is room
  8. From Do not let two runs overlap
  9. From Clean up, and say why you died
  10. From The name must resolve, the port must answer
  11. From Trust the folder, not just the file
  12. From Never run the destructive form by accident
  13. From Say what you excluded
  14. From Run them all
  15. What this register proves

Also by the Author

Also by the Author

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub