satellite.sh – sync data between systems using pen-drive

104

Its is easy to sync two connected systems, but what if you need to sync two or more systems, not connected with each other? Need to use external storage device, but everytime copying data to external device is not always easy, and there may be chances that you may forget to copy some files. So here is a small bash-shell script, just need to set path once in script and it will take care of rest things.

save following as ~/bin/satellite.sh and mark executable by executing ‘chmod +x ~/bin/satellite.sh’.
to sync data from local system to external device run:
$ satellite.sh upload  (enter ‘satellite’ on asking passphrase)
to sync data from external device to local system run it with ‘base’ as argument and passphrase.

In this case external storage is USB pen drive labeled as SATELLITE.

#!/bin/bash

if [ $# -ne 1 ];then
echo “Usage: $0 upload|download”
exit 0
fi

arg=$1

# —- BLOCK-START —-
# add path to both arrays
# and basename of the path must be a folder
#
SATPATH=/media/SATELLITE/.A-BOX
#
path_satellite[1]=”$SATPATH/bin”
path_satellite[2]=”$SATPATH/learning”
path_satellite[3]=”$SATPATH/raw_c”
path_satellite[4]=”$SATPATH/raw_python”
path_satellite[5]=”$SATPATH/scripting”
path_satellite[6]=”$SATPATH/Wallpapers”
#
path_base[1]=”$HOME/bin”
path_base[2]=”$HOME/Documents/learning”
path_base[3]=”$HOME/Documents/turbo/raw_c”
path_base[4]=”$HOME/Documents/turbo/raw_python”
path_base[5]=”$HOME/Documents/turbo/scripting”
path_base[6]=”$HOME/Pictures/Wallpapers”
#
# —- BLOCK-END —-

#
# check that both arrays contains same number of elements
#
if [ ${#path_satellite[@]} -ne ${#path_base[@]} ];then
echo -e “mismatch detected in ${0}nkindly verify both arrays, exiting”
exit 1
fi

total=${#path_base[@]}      # since both arrays are having same number of elements
echo “total: $total”

#
# check that order of elements in both arrays are same
#
counter=1
while [ $counter -le $total ]
do
temp_sate=$(basename ${path_satellite[$counter]})
temp_base=$(basename ${path_base[$counter]})

if [ “${temp_sate}” != “${temp_base}” ];then
echo “satellite: ${temp_sate}”
echo “base     : ${temp_base}”
echo “above paths does not match in ${0}, kindly check and rerun”
exit 1
fi
counter=$(expr $counter + 1)
done

#
# chech if local copy of satellite.sh is latest
#
f_remote=”$SATPATH/bin/satellite.sh”
f_local=”$HOME/bin/satellite.sh”

script_remote=$(stat ${f_remote} | grep “Modify”)
script_remote=$(echo ${script_remote:8:19} | sed ‘s/[- :]//g’)

script_local=$(stat ${f_local} | grep “Modify”)
script_local=$(echo ${script_local:8:19} | sed ‘s/[- :]//g’)

echo “remote: ${script_remote}”
echo “local : ${script_local}”

if [ “${script_remote}” -gt “${script_local}” ];then
cp -f ${f_remote} ${f_local}
echo “local copy was outdated, hence updated”
echo “re-run: ${0}”
exit 1
fi

if [ “$arg” = “upload” ];then
echo -ne “phrase to overwrite SATELLITE 33[35G: “
read text

if [ “$text” != “satellite” ];then
echo “wrong phrase”
exit 2
fi

for (( x=1; x<=$total; x++))
do
[ ! -d ${path_satellite[x]} ] && mkdir -p ${path_satellite[x]}
rsync -av –delete ${path_base[x]}/ ${path_satellite[x]}
done

elif [ “$arg” = “download” ];then
echo -ne “phrase to overwrite BASE 33[35G: “
read text

if [ “$text” != “base” ];then
echo “wrong phrase”
exit 2
fi

for (( x=1; x<=$total; x++))
do
[ ! -d ${path_base[x]} ] && mkdir -p ${path_base[x]}
rsync -av –delete ${path_satellite[x]}/ ${path_base[x]}

#
# enable executable bit of bash-shell & python scripts
#
find ${path_base[x]} -name “*.sh” -exec chmod +x {} ;
find ${path_base[x]} -name “*.py” -exec chmod +x {} ;

#
# enable executable bit of binaries
# if executable path/file name contains single quote (‘), skip it — need to fix it!
#
var=$(find ${path_base[x]} -name “*” -type f -print| grep -v “‘”| xargs file | grep ELF | cut -d’:’ -f1)
if [ -n “${var}” ];then
echo ${var} | xargs chmod +x
fi
done

fi
echo -e “synching 33[35G DONE”
#———— END ————