When possible, native *nix formats like tar are the best choice for backups. However, there are cases where other formats may be preferable due to their features. For example, specific compression options or non‑solid compression that allow faster access to individual files. In these situations, common tools such as 7‑Zip (with Windows heritage) may not store UNIX meta data correctly. By combining small, easy‑to‑create helper scripts with your backup archives, you can preserve metadata. Here are three short scripts that can be generated via shell one-liners:
perms.sh→ Restores file permissions only.ownperms.sh→ Restores both permissions and ownership.- Run with no arguments → restores both.
- Run with
-o→ ownership only. - Run with
-p→ permissions only.
symlinks.sh→ Recreates symbolic links.- Optional: delete symlinks before archiving to avoid duplication, then restore them afterward.
Warning: These simple scripts may not work reliably if you have very exotically named files/directories. For example, files that include " in their name. In such cases I assume you are an advanced user and know how to work around such things. In fact, you probably do not even need this gist. 🤣
If you are backing up a directory, run the following one‑liner inside that directory before archiving:
{ printf '#!/bin/sh\nset -e\ncd "$(dirname "$0")"\n'; find . -depth ! -type l ! -name perms.sh -printf 'chmod %m "%p"\n'; } > perms.shNote: Triple-click to select the entire line above.
This generates a script (perms.sh) that can later be executed with:
sh perms.shto restore file permissions on a UNIX‑like system.
If you also need to preserve file ownership, use this slightly more advanced variant:
{ printf '#!/bin/sh\nset -e\no=n\np=n\n[ -z "${1:-}" ] && o=y && p=y\n[ "${1:-}" = "-o" ] && o=y\n[ "${1:-}" = "-p" ] && p=y\ncd "$(dirname "$0")"\n'; find . -depth ! -type l ! -name ownperms.sh -printf '[ "$o" = y ] && chown "%u:%g" "%p"\n[ "$p" = y ] && chmod %m "%p"\n'; } > ownperms.shTo restore both permissions and ownership:
sh ownperms.sh- Use
-oto restore ownership only. - Use
-pto restore permissions only.
Note: Restoring ownership may require elevated privileges (e.g. sudo or runas).
While 7-zip (with -snl) and Info‑ZIP (with -y) can store symlinks, some tools or other archive formats cannot. In those cases, you could also generate a script to recreate symlinks:
{ printf '#!/bin/sh\nset -e\ncd "$(dirname "$0")"\n'; find . -type l -printf 'ln -sfn "%l" "%p"\n'; } > symlinks.shThis produces symlinks.sh, which can be executed as follows to restore symlinks:
sh symlinks.shOptionally, you may delete symlinks before creating the archive. This avoids possible duplication of linked files and folders being included in the backup twice.
Only do this if you have first created symlinks.sh and also fully understand and accept the impact of the following (it deletes all symlinks on your system recursively from the directory you are currently working in!).
find . -type l -deleteNote: Assuming you created it first, you can restore the symlinks at any point by running symlinks.sh.