The Haiku/BeOS Tip Server
Tips and tricks for Haiku/BeOS users

Creating system backups with zip

Contributed by: Scot Hacker
This tip is valid for: Both BeOS and Haiku

Zip isn’t the only mechanism available on BeOS for creating system backups (a much more user-friendly, complete backup system for BeOS is Bald Mountain’s BeB), but it does have some pretty powerful capabilities… once you get to know the command-line options.

If you’re using BeOS R4.1 or later, zip should already be installed on your system. The latest version can always be download from BeBits.

General backup notes

Since backups can take a while, plan things out before beginning so you don’t have to run the same backup a bunch of times to get it right. For example, you may want to just back up your home directory, skipping the rest of the system, on the assumption that you can always install the OS and apps easily enough later down the road. You also may want to create certain archives based on system queries. Or you may want to back up everything but image and sound files. You may or may not want your backups to follow symlinks.

It helps a lot to have multiple BFS partitions, if you can afford the space. It’s useful, for instance, to have a partition dedicated to media files, where you can store all of your images, movies, and sounds. You can create links to these directories on other partitions inside your home folder for easy access.

Of course, it goes without saying that a backup is worthless if you don’t move it to another physical hard drive. Better yet, move it to another machine, to your web server, or to a removeable cartridge that you can store off- premises, in case of fire or flood.

Basic syntax

The basic zip syntax is zip outfile infile, where infile is typicall a directory name. The .zip extension will be automatically added to the end of outfile. Thus, if your current directory is /boot/home and you want to back up your /boot/home/words directory, you might type:

zip words words

This would create a file called words.zip in /boot/home that would include the contents of /boot/home/words.

Recurse sudirectories

Of course, you’ll usually want your zips to crawl through entire subdirectory structures. Thus, you’ll need the -r flag, for “recurse,” e.g.

zip -r /boot/home/mail_backup /boot/home/mail

This will crawl your entire mail folder and all of its subdirectories. Unzipping the archive later will recreate this directory structure.

Skip some filenames

You may feel that some files are too huge to be backed up, and/or they’re not important enough to be worth the space. Tell zip to skip them with the -x flag. E.g.:

zip -r some_images images -x "*tga" "*tif*"

would crawl the images subdirectory of the current directory and zip everything except for files ending in “tga” or including “tif” in their names.

Note the quote signs around the name templates – without them zip would ignore just those targas and tiffs which are in the current directory.

Thus, you could back up all of your gifs and jpegs but skip your larger targas and tiffs. You may also want to skip some common sound file extensions, or other .zip files this way.

Include only some filenames

The opposite of the -x flag is the -i flag, which will cause zip to only zip the files you specify. So, for instance, if you want to back up the mirror of your web site but only archive HTML files and none of your images, you could use:

zip -r site_backup /boot/home/mysite -i "*html"

Dereference symlinks

If you’ve been using symlinks liberally, you could run into problems if you allow zip to follow them — you could end up zipping much more than you intended. To store symlinks without following them, use the -y flag:

zip -yr image_bak images

This command will back up your entire images directory, but won’t follow any symlinks it finds within. E.g. you may be storing all your targas on another partition and accessing them via symlink in your images directory — decompressing this archive will give you a live working symlink, but won’t back up the remote directory itself.

Achieve maximum compression

If you want to make sure your zip archive ends up as small as possible, use the -9 flag to specify maximum compression. This will cause zip to operate a little more slowly, but machines today are fast; it’s usually worth it. Thus, a good, straightforward set of backup flags to use is:

zip -9ry outfile infiles

Have zip take the output of another command

It’s possible to pipe the output of a shell command into the input of zip by using the -@ flag. For example:

ls | zip testfile -@

would simply run an ls on the current directory and zip all the files in that list. But wait — this actually gets useful, because you can just as easily run a query or find command to find only files that match a given criteria and make an archive of them alone (see the tip Queries from Terminal if necessary).

Thus, the following command zips up just your BeMail messages with a status of “Sent” that were last modified before August 1. You’ll have to unwrap this all onto one line:

query "(((MAIL:status=="*[sS][eE][nN][tT]*")&&
(last_modified<=901954800))&&
(BEOS:TYPE=="text/x-email"))" |
zip -r /boot/home/outgoing.mail.pre.080198.zip -@

Update an existing archive

One flag specially geared toward creating backups is the -u flag, which is used to “update” an archive with only the files that are new or that have been changed since the archive was last created or updated. This is your equivalent of the “incremental backup.” In the example above, we used < tt>ls | zip testfile -@ to create a simple archive called testfile.zip of all files in the current directory. Now let’s say you edited one file in this directory and created one new one. If you now run:

ls | zip -u testfile -@

then zip will add the new file to testfile.zip and update the file you edited. Note that this assumes that testfile.zip already exists.

Add a comment to your archive

If you want to include a note to yourself inside the archive, use the -z flag. For example, you may not look at this archive again for a year, and want to be reminded at that time that this zip did not include any targa images. Type:

zip -r -z image_bak images -x "*tga"

After compression is complete, the shell will prompt you to enter your message — it can be as long as you like. When finished, type a period on a blank line. Double-click the file and Expander will show your comment as the first line. Similarly, unzippng from Terminal will display the comment as the first line in the shell output (Expander is just a window onto shell output).

Grow an archive

To add files to an existing archive, use the -u flag (for update) to add any files that have changed or that are new. To add only files that have changed but skip new files, use the -f flag (for freshen).

Unzip options

You’ll also want to become familiar with the unzipping options, so you can restore your archives properly, without overwriting data. You can see all of the unzip options with unzip --help, but the most important are:

Test the integrity of an archive: unzip -t filename.zip

Just show the comment embedded in an archive: unzip -z filename.zip

Exclude some files: unzip -x "*tga" "*mp3" (this would extract everything but mpeg sounds and targa images).

Include only certain files: unzip -C filename.zip *html* (This will extract only files that have “html” in their filenames; note that the wildcard characters are escaped so as to not confuse the shell).

More info

Both zip and unzip have many more options, accessible via zip --help< /tt> and unzip --help.

Posted in Tracker & Deskbar


(comments are closed).