Zaloha.sh is a tiny and simple shell script used to synchronize two local directories: a source directory and a backup directory. It employs standard Linux/Unix tools such as find, sort, awk, mkdir, rmdir, cp and rm to support its underlying functionality.
Zaloha obtains information about the directories and files via the find command. Both directories must be available locally i.e mounted to the local file system. It also features reverse-synchronization, and can optionally compare files byte by byte. Besides, it asks users to confirm actions before they are executed.
In this article, you will learn how to install and use zaloha.sh to synchronize two local directories in Linux.
Installing Zaloha.sh in Linux
To install Zaloha.sh, you need to clone its Github repository using the git command-line tool, but before that, you need to install git as shown.
# dnf install git # CentOS/RHEL 8/Fedora 22+ # yum install git # CentOS/RHEL 7/Fedora $ sudo apt install git # Ubuntu/Debian
Once git is installed, run the following command to clone the remote repository to your system, move into the local repository, then install the zaloha.sh script in a location in your PATH e.g /usr/bin and make it executable as shown.
$ git clone https://github.com/Fitus/Zaloha.sh.git $ cd Zaloha.sh/ $ echo $PATH $ sudo cp Zaloha.sh /usr/bin/zaloha.sh $ sudo chmod +x /usr/bin/zaloha.sh
Synchronize Two Local Directories in Linux Using Zaloha.sh
Now that zaloha.sh is installed in your PATH, you can run it normally like any other command. You can synchronize two local directories as shown.
$ sudo zaloha.sh --sourceDir="./admin_portal/" --backupDir="/var/www/html/admin_portal/"
After running it, zaloha will analyze the two directories and prepares the commands necessary to synchronize the two directories.
You will be prompted to confirm the actions to be executed: “Execute above-listed copies to /var/www/html/admin_portal/? [Y/y=Yes, other=do nothing, and abort]:”. Answer yes to proceed.
Backup to External/Removable USB Media
You can also backup to a removable media (e.g /media/aaronk/EXT) mounted to the local file system. The destination directory must exist for the command to work, otherwise you will get the error message “Zaloha.sh: <backupDir> is not a directory”.
$ sudo mkdir /media/aaronk/EXT/admin_portal $ sudo zaloha.sh --sourceDir="./admin_portal/" --backupDir="/media/aaronk/EXT/admin_portal"
Backup Changes from Source to Backup Directory
Now make more changes in the source directory, then run zaloha.sh once more to back up the changes in the external disk as shown.
$ mkdir /home/aaronk/admin_portal/plugins $ mkdir /home/aaronk/admin_portal/images $ sudo zaloha.sh --sourceDir="/home/aaronk/admin_portal/" --backupDir="/media/aaronk/EXT/admin_portal"
Zaloha.sh will create the new directories in the backup directory and copy any new files from the source as well as highlighted in the following screenshot.
Reverse Synchronize Changes from Backup to Source Directory
Assuming you have made changes in the backup directory to files that already exist in the source directory, you can make the changes reflect in the source directory using the reverse sync feature, enabled using the --renUp
option.
$ zaloha.sh --revUp --sourceDir="/home/aaronk/admin_portal/" --backupDir="/media/aaronk/EXT/admin_portal"
Note that any new files or directories created in the backup directory that doesn’t exist in the source directory will also be deleted as indicated in the following screenshot.
You can tell zaloha to follow symbolic links on the source directory using the --followSLinksS
option and on the backup directory using the --followSLinksB
option.
$ sudo zaloha.sh --followSLinksS --followSLinksB --sourceDir="./admin_portal/" --backupDir="/var/www/html/admin_portal/"
To view the Zaloha documentation, run the following command.
$ zaloha.sh --help
That’s all for now! Zalohah.sh is a small and simple Bash-based backup script to synchronize two local directories in Linux. Give it a try and share your thoughts with us via the feedback form below.
Aaron,
You seem to have had problems with getting REV.NEW to work: The option
--revNew
must be given and the file to be REV.NEW-ed must be younger than the last run of Zaloha.sh (= implies that it was created directly in the backup directory).If the file is older, then it will be REMOVE-ed, as this implies that it must have existed in the source directory before and somebody removed it from the source directory …
@Jens
Many thanks for the clear and precise explanation. We are truly grateful.
Add rsync comparison: Clearly Rsync is a program with 20 years history, a de-facto standard, more feature-rich, network-capable and its delta-transfer algorithm has been a landmark in IT. However, this program has the following strong points (besides being very small):
1) It has the interactivity (execute only after user’s confirmation) plus finer information about the actions. E.g. write of a new file is NEW and the update of an existing file is UPDATE. If you use the
--color
option, NEW will display black/white (less risky operation) and UPDATE will display red (overwriting = more risky operation = more attention needed). I am not aware that rsync would give you that level of detail.2) rsync is memory-constrained as it builds its file lists in memory. Problems have been reported with huge directory trees. This script builds and processes its file lists as CSV files (= not memory-constrained).
3) it has been reported that Rsync is slower than cp when acting locally. This is caused by the fact that rsync has a checksum-computing function built-in that is active even when copying locally. This program invokes the operating system’s cp command directly.
4) integrations with other systems, needed changes, etc: a shell script has certain advantages over a binary program
@Jens
We truly appreciate the insights you have shared concerning Rsync in comparison to zaloha.sh. Many thanks once more.
What does this script offer better than the infinitely more mature rsync? Rsync works for any combination of local and remote sources and backups as well.
@Aamir
You are correct, rsync is a more advanced tool with so many options. But you can employ zalohah.sh as a simple and easy-to-use backup tool.
what is the advantage over then rsync?
@Siva
It is a simple and easy-to-use backup tool.
why not simply rsync?
https://linux.die.net/man/1/rsync
@Jabir
True, rsync alone works fine too. Thanks for the feedback.