Fitus / Zaloha2.sh

Small and simple directory synchronizer (a BASH script)
MIT License
82 stars 12 forks source link
awk backup bash find scp ssh synchronization

Zaloha2.sh

Zaloha2 is a small and simple directory synchronizer:

Full documentation is available both online as well as inside of Zaloha2.sh.

On Linux/Unics, Zaloha2 runs natively. On Windows, Cygwin is needed.

What are synchronization programs good for ?

Read article here.

How does Zaloha2.sh work ?

Explained in five sentences (the Local Mode)

Explained by an Interactive Flowchart

Open Interactive JavaScript Flowchart here.

An insight into the CSV metadata is given here

Open CSV data model here.

Explained in full detail

Read the relevant sections in the Documentation.

Obtain Zaloha2.sh

The simplest way: Under the green button "Code" above, choose "Download ZIP". From the downloaded ZIP archive, extract Zaloha2.sh and make it executable (chmod u+x Zaloha2.sh).

For running the Simple Demo, extract also the scripts Simple_Demo_step1/2/3/4/5/6/7.sh and make them executable.

The integrity of Zaloha2.sh can be verified by its SHA-256 hash. The hash of current version is:

sha256sum Zaloha2.sh
6384d78f048d9d4de119bf6fc02a2ce961ce5bc8e0d6eba67553dc32b1e3859c  Zaloha2.sh

Usage Examples

Basic Usage: Synchronize test_source to test_backup:

Zaloha2.sh --sourceDir="test_source" --backupDir="test_backup"

Basic usage with Colors (on terminals with ANSI escape codes):

Zaloha2.sh --sourceDir="test_source" --backupDir="test_backup" --color

Besides the standard synchronization, if there are files in test_backup that are newer (younger) than the corresponding files in test_source, copy them back to test_source (Reverse-Update):

Zaloha2.sh --sourceDir="test_source" --backupDir="test_backup" --revUp

Besides the standard synchronization, if there are files in test_backup that do not exist in test_source, and those files are newer (younger) than the last run of Zaloha2, copy them back to test_source (Reverse-New):

Zaloha2.sh --sourceDir="test_source" --backupDir="test_backup" --revNew

Besides the standard synchronization, if there are files in test_backup that do not exist in test_source, irrespective of how old they are, copy them back to test_source (Reverse-New-All):

Zaloha2.sh --sourceDir="test_source" --backupDir="test_backup" --revNewAll

Do Not Remove objects from test_backup (unless the removals are unavoidable), just add new files and subdirectories to test_backup (and/or update existing files in there):

Zaloha2.sh --sourceDir="test_source" --backupDir="test_backup" --noRemove

Exclude subdirectories .git from the synchronization. If such subdirectories already exist in test_backup, they are considered obsolete (= removals are prepared):

Zaloha2.sh --sourceDir="test_source" --backupDir="test_backup" \
           --findSourceOps='( -type d -a -name .git ) -prune -o'

Exclude files with ending .NFO from the synchronization. If such files already exist in test_backup, they are considered obsolete (= removals are prepared):

Zaloha2.sh --sourceDir="test_source" --backupDir="test_backup" \
           --findSourceOps='( -type f -a -name *.NFO ) -o'

Generally Exclude files with ending .NFO from the synchronization (never do anything with them). Note that the option --findGeneralOps is used instead of --findSourceOps. The + sign tells Zaloha2 to use the passed FIND expression in addition to the internally defined defaults:

Zaloha2.sh --sourceDir="test_source" --backupDir="test_backup" \
           --findGeneralOps='+( -type f -a -name *.NFO ) -o'

Disable the internally defined defaults for --findGeneralOps that exclude the well-known Linux and Windows "trash" and "lost+found" subdirectories: Use the option --findGeneralOps without the leading + sign to replace the internally defined defaults (here by an empty string):

Zaloha2.sh --sourceDir="test_source" --backupDir="test_backup" \
           --findGeneralOps=

A more complex example with several FIND expressions: Do not backup any subdirectories .git, .svn and .cache in test_source. If they already exist in test_backup, prepare their removals. These are also clear cases for --findSourceOps. Further, completely ignore (in both directories) any coredump files and the subdirectories mount and database located directly under test_source and test_backup. The reasons are that arbitrary external media can be mounted under mount and the backup of database is implemented by dedicated tools provided by the database vendor (also not on file level). These are again clear cases for --findGeneralOps. Additionally, preserve the internally defined defaults for --findGeneralOps (this is achieved by the leading + sign). Finally, for a good visual appearance, use the expression by expression method:

Zaloha2.sh --sourceDir="test_source" --backupDir="test_backup"      \
           --findSourceOps='( -type d -a -name .git ) -prune -o'    \
           --findSourceOps='( -type d -a -name .svn ) -prune -o'    \
           --findSourceOps='( -type d -a -name .cache ) -prune -o'  \
           --findGeneralOps='+'                                     \
           --findGeneralOps='( -type f -a -name core ) -o'          \
           --findGeneralOps='-path ///d/mount -prune -o'            \
           --findGeneralOps='-path ///d/database -prune -o'

Compare files Byte-By-Byte instead of by just their sizes and modification times (warning: this might take much time):

Zaloha2.sh --sourceDir="test_source" --backupDir="test_backup" --byteByByte

Do not prepare scripts for the case of restore (No Restore, saves processing time and disk space):

Zaloha2.sh --sourceDir="test_source" --backupDir="test_backup" --noRestore

Instead of GNU AWK, use MAWK, the very fast AWK implementation based on a bytecode interpreter:

Zaloha2.sh --sourceDir="test_source" --backupDir="test_backup" --mawk

Produce less screen output (No Progress Messages from the analysis phase):

Zaloha2.sh --sourceDir="test_source" --backupDir="test_backup" --noProgress

Do Not Execute the actions (just prepare the scripts, see the Documentation for more details):

Zaloha2.sh --sourceDir="test_source" --backupDir="test_backup" --noExec

Put Zaloha2 Metadata (working files of Zaloha2.sh) to a separate storage, e.g. for speed reasons (please note that if Zaloha2.sh is used for multiple synchronizations, then each such use (instance) of Zaloha2.sh requires a separate Metadata directory):

Zaloha2.sh --sourceDir="test_source" --backupDir="test_backup" \
           --metaDir="/fast_storage/Zaloha_metadata/data_of_instance_0001"

Show the Documentation (quit viewing by typing q):

Zaloha2.sh --help

So much typing?

Some command lines may get lengthy. But: Usually you do not create new setups each day, nor you type the commands always from scratch. After having fine-tuned a given synchronization/backup process, you are advised to materialize the command line of Zaloha2 into a wrapper script. The wrapper script can then be attached to a desktop icon, for instance, so that the whole process can be started just by double-clicking on that icon. The advantage of wrapper scripts is that you can do more in them than just invoke Zaloha2: e.g. mount the backup media before, unmount it after, specially handle sensitive data and so on.

Usage Screenshot (of the Local Mode)

Simple_Demo_screenshot

Performance tuning and Performance data (in the Local Mode)

Performance was measured on following system and data:

Measured performance of the analysis phase:

Performance of the execution phase:

The Remote Source Mode

In the Remote Source Mode, the source directory is on a remote source host that can be reached via SSH/SCP and the backup directory is available locally. This mode is activated by the option --sourceUserHost.

The Remote Source Mode explained in eight sentences

Usage Example of the Remote Source Mode

# Establish the SSH master connection
ssh -nNf -o ControlMaster=yes -o ControlPath='~/.ssh/cm-%r@%h:%p' 'user@sourcehost'

# Run Zaloha2.sh
Zaloha2.sh --sourceDir="test_source_remote"    \
           --backupDir="test_backup_local"     \
           --sourceUserHost='user@sourcehost'  \
           --sshOptions='-o ControlMaster=no -o ControlPath=~/.ssh/cm-%r@%h:%p'     \
           --scpOptions='-o ControlMaster=no -o ControlPath=~/.ssh/cm-%r@%h:%p -T'

# Terminate the SSH master connection
ssh -O exit -o ControlPath='~/.ssh/cm-%r@%h:%p' 'user@sourcehost'

The Remote Backup Mode

In the Remote Backup Mode, the source directory is available locally and the backup directory is on a remote backup host that can be reached via SSH/SCP. This mode is activated by the option --backupUserHost.

The Remote Backup Mode explained in eight sentences

Usage Example of the Remote Backup Mode

# Establish the SSH master connection
ssh -nNf -o ControlMaster=yes -o ControlPath='~/.ssh/cm-%r@%h:%p' 'user@backuphost'

# Run Zaloha2.sh
Zaloha2.sh --sourceDir="test_source_local"     \
           --backupDir="test_backup_remote"    \
           --backupUserHost='user@backuphost'  \
           --sshOptions='-o ControlMaster=no -o ControlPath=~/.ssh/cm-%r@%h:%p'     \
           --scpOptions='-o ControlMaster=no -o ControlPath=~/.ssh/cm-%r@%h:%p -T'

# Terminate the SSH master connection
ssh -O exit -o ControlPath='~/.ssh/cm-%r@%h:%p' 'user@backuphost'

Comparing contents of files via SHA-256 hashes

The option --byteByByte cannot be used in the Remote Modes, because the CMP command needs local access to both compared files.

For the Remote Modes, a new option --sha256 has been introduced. This option causes both FIND scans to additionally invoke SHA256SUM on each encountered file. Note that the SHA-256 hashes of the files are calculated on the hosts where the files are located. Also no contents of files are transferred over the network, just the SHA-256 hashes. The SHA-256 hashes are then compared to detect files that appear identical but their contents differ.

Other uses of the SHA-256 hashes

The option --sha256 is not limited to the Remote Modes – it can be used in the Local Mode too. CSV data about files that contain their SHA-256 hashes can be used for other purposes as well:

Assume you have files with identical (duplicate) contents scattered across your filesystem, and you want to keep only one copy per unique file. In other words: de-duplicate files by content. The finding of files with duplicate contents can be achieved by sorting the CSV file 330 by the SHA-256 hashes (= by column 13) and evaluating the result, e.g. by a simple AWK program that prints out the files where the SHA-256 hash equals to the SHA-256 hash in the immediately preceding record in the sorted file.

Performance tuning in the Remote Source and Remote Backup Modes

Development status

Add-on script Zaloha2_Snapshot.sh

An add-on script to create hardlink-based snapshots of the backup directory exists: Zaloha2_Snapshot. This allows to create Time Machine-like backup solutions.

Full list of changes (Zaloha2.sh compared to Zaloha.sh)

Some design changes break backward compatibility with Zaloha.sh (the predecessor of Zaloha2.sh). For this reason, this repository is a new repository created specially for Zaloha2.sh.

           Zaloha.sh            Zaloha2.sh
  New option --sourceUserHost to activate the Remote Source Mode via SSH/SCP
  New option --backupUserHost to activate the Remote Backup Mode via SSH/SCP
  New option --sshOptions to pass additional command-line options to SSH in the remote modes
  New option --scpOptions to pass additional command-line options to SCP in the remote modes
  New option --findParallel to run the local and remote FIND scans in parallel in the remote modes
Option --metaDir In Remote Backup Mode: allows to place the Zaloha metadata directory on the remote backup host to a different location than the default.
  New option --metaDirTemp: In the remote modes, Zaloha2 needs a temporary Metadata directory too. This option allows to place it to a different location than the default.
Shellscript 610 In Remote Backup Mode: executed on the remote side
Shellscript 620 Split to 621 (pre-copy), 622 (copy), 623 (post-copy). In Remote Backup Mode: 621 and 623 are executed on the remote side. In both remote modes, 622 contains SCP commands instead of CP commands.
Shellscript 630 Split to 631 (pre-copy), 632 (copy), 633 (post-copy). In Remote Source Mode: 631 and 633 are executed on the remote side. In both remote modes, 632 contains SCP commands instead of CP commands.
Shellscript 640 In Remote Backup Mode: executed on the remote side
Shellscript 650 Split to 651 (pre-copy), 652 (copy), 653 (post-copy). In Remote Backup Mode: 651 and 653 are executed on the remote side. In both remote modes, 652 contains SCP commands instead of CP commands.
Restore script 810 In the remote modes: contains SCP commands instead of CP commands
Restore script 870 Commands to preserve times of files have been moved from script 810 to script 870
  New option --sha256 for comparing the contents of files via SHA-256 hashes
CSV data model of 16 columns Extended to 17 columns to accommodate the SHA-256 hashes in new separate column 13 (original columns 13+14+15+16 shifted to 14+15+16+17)
  New check for falsely detected hardlinks: SHA-256 hash differs
Option --hLinks Renamed to --detectHLinksS (more descriptive option name)
Option --touch Renamed to --extraTouch (more descriptive option name)
  New option --syncSLinks for synchronization of symbolic links
Option --noExec1Hdr Renamed to --no610Hdr
Option --noExec2Hdr Replaced by finer-grained options --no621Hdr, --no622Hdr and --no623Hdr
Option --noExec3Hdr Replaced by finer-grained options --no631Hdr, --no632Hdr and --no633Hdr
Option --noExec4Hdr Renamed to --no640Hdr
Option --noExec5Hdr Replaced by finer-grained options --no651Hdr, --no652Hdr and --no653Hdr
  Ability to process all symbolic links (even those with target paths that contain three or more consective slashes). The implied change is additional escaping of slashes by ///s in column 16 for symbolic links.
  New Sanity Check for column 6 not alphanumeric
  More stringent directories hierarchy check
  More tolerant check of modification times of files (zero or even negative modification times are possible)
  More tolerant check of target paths of symbolic links (empty target paths are possible on some OSes)
  Minor code improvements and optimizations + improved documentation
Code size 76 kB Code size 118 kB
Docu size 78 kB Docu size 100 kB

License

MIT License