9. Scripts and scheduled jobs¶
squeeze and unsqueeze are made to be scripted: quiet when asked, never stopping to ask a question you can answer on the command line, and returning zip's and unzip's own exit codes.
A nightly backup with a password¶
Keep the password in a file only you can read:
printf '%s\n' 'maple-harbour-lantern-quietly-orbit' > ~/.backup-password
chmod 600 ~/.backup-password
Then the backup needs no typing and shows nothing unless something's wrong:
% squeeze -q -e --read-password ~/.backup-password nightly Documents; echo "exit code $?"
exit code 0| Option | What it does |
|---|---|
-q |
Quiet: only problems are shown, on standard error. |
-e --read-password file |
Protect with the password on the file's first line. It never appears on the command line, where other programs could see it. |
Run it every night¶
With crontab -e, a line such as this makes the backup at 2 am each day:
0 2 * * * /usr/local/bin/squeeze -q -e --read-password ~/.backup-password ~/Backups/nightly-$(date +\%F) ~/Documents
Use the full path /usr/local/bin/squeeze in cron and in Shortcuts, whose jobs don't read your Terminal settings.
Exit codes¶
0 means everything worked. squeeze returns zip's codes and unsqueeze unzip's, so a script that checks them keeps working. The ones you're most likely to see:
| Code | squeeze | unsqueeze |
|---|---|---|
| 0 | Done | Done |
| 9 | Stopped with Control-C; anything part-written is removed | |
| 10 | A refused command, such as an option that isn't allowed | |
| 12 | Nothing to do | |
| 16 | A refused command, such as options that contradict | |
| 18 | A file couldn't be read, or an image or PDF couldn't be squeezed | |
| 50 | Not enough free space, or the archive would grow too much | |
| 80 | Stopped with Control-C; nothing was changed | |
| 82 | No password, or the wrong one |
Every code is in Messages and exit codes.
Lists of files¶
Like zip, squeeze reads names from its input with -@:
find ~/Documents -name '*.pdf' -mtime -7 | squeeze -@ week-pdfs