I use this website/server to store all of my projects and specially my scripts, that i use almost on all of my machines. But because i update them regularly, i needed a way to automate this process a bit, cause login from each machine, to the server and downloading the scripts, isn't easy/quick, specially if i wanted to use another machine, i don't own. So, i figured, the best way is to have my scripts in a public folder, after all, they don't store any valuable information and create another script, that will be used to synchronize the files, between a local machine and the website/server here.
To download or synchronize a script, i just have to use the new script, called hsync.sh
with the remote URL to get the remote file and the local location, to store or check the local file. The usage of the script is easy:
Usage:
To sync a remote file:
hsync.sh <remote_file> <local_file>
Other Options:
hsync.sh <option>
Options:
help : show this help screen
The script uses wget
to download the remote file in a temporary folder, then checks if the remote and local files are the same, using the cmp
tool and if not, it updates the local file. I also put a failesafe, to check if the local file is smaller in size, cause some times, when i update my scripts, i make them more efficient and the size decreases and the local file, may be smaller but newer than the remote and for example, i haven't updated the remote file.
For sure, you can use the script to synchronize and check if newer versions of what ever file is online and you want to check. The script is below. Just copy/paste.
#!/bin/bash
function help(){
echo " "
echo " Usage: "
echo " To sync a remote file:"
echo " hsync.sh <remote_file> <local_file>"
echo " "
echo " Other Options:"
echo " hsync.sh <option>"
echo " "
echo " Options:"
echo " help : show this help screen"
echo " "
}
if [ "$#" -eq 1 ]; then
case "$1" in
"help") help;;
"--help") help;;
esac
exit
fi
if [ "$#" -ne 2 ]; then
help
exit
fi
REMOTE_URL="$1"
LOCAL_FILE="$2"
TEMP_FILE=$(mktemp)
# Download remote file
wget "$REMOTE_URL" -O "$TEMP_FILE" -q
if [ -f "$LOCAL_FILE" ]; then
LOCAL_SIZE=$(stat -c %s "$LOCAL_FILE")
else
LOCAL_SIZE=0
fi
REMOTE_SIZE=$(stat -c %s "$TEMP_FILE")
if [ "$REMOTE_SIZE" -lt "$LOCAL_SIZE" ]; then
read -p "Remote file is smaller than local. Continue with update? [y/N] " choice
case "$choice" in
y|Y ) echo "[] Downloading remote file...";;
* ) echo "[] Aborting."; exit 0;;
esac
fi
# Compare and update if needed
if [ ! -f "$LOCAL_FILE" ] || ! cmp -s "$TEMP_FILE" "$LOCAL_FILE"; then
echo "[] Updating local file."
mv "$TEMP_FILE" "$LOCAL_FILE"
else
echo "[] Files are the same. No update needed."
rm "$TEMP_FILE"
fi
echo " "