Skip to content

Instantly share code, notes, and snippets.

@qtaped
Last active January 12, 2023 22:04
Show Gist options
  • Save qtaped/bca15ce29972d8d8ea33cf128051e7ef to your computer and use it in GitHub Desktop.
Save qtaped/bca15ce29972d8d8ea33cf128051e7ef to your computer and use it in GitHub Desktop.
simple rsync bash script
#!/usr/bin/env bash
# brew install rsync
# SETTINGS
def_src="$HOME" # default source
def_dst="/Path/to/external/drive/folder" # default destination
# Variables
date=$( date "+%Y-%m-%d-%H-%M-%S" )
rsync_version=$( rsync --version | head -n 1)
sim=""
# COLORS
bold='\033[1m'
blue='\033[1;34m'
ul='\033[4m'
nc='\033[0m'
# welcome screen
clear
echo
echo " .----.-----.--.--.-----.----. "
echo " | _|__ --| | | | __| "
echo " |__| |_____|___ |__|__|____| "
echo " |_____| "
echo
echo -e "\n${bold}:: Running ${blue}${rsync_version}${nc}...\n"
read -p "Source: [default: ${def_src}] " src
test -z "${src}" && src=${def_src}
read -p "Destination: [default: ${def_dst}] " dst
test -z "${dst}" && dst=${def_dst}
logs="${dst}/_rsyncLogs.txt"
if [ ! -d "${dst}" ]
then
echo -e "Destination ${dst} do not exists.\nAborting…"
exit 2
elif [ ! -d "${src}" ]
then
echo -e "Source ${src} do not exists.\nAborting…"
exit 2
fi
echo
# simulation
rsync -arhP --exclude=.* --dry-run "${src}/" "${dst}/"
echo
echo -e " - Source: \t${ul}${src}${nc}"
echo -e " - Destination: \t${ul}${dst}${nc}"
echo
echo -e "${bold}→ Continue? [y/n]${nc}"
read -p " Logs only: [l] " -r
echo
if [[ ! $REPLY =~ ^[YyLl]$ ]]
then
exit 1
elif [[ $REPLY =~ ^[Ll]$ ]]
then
sim="--dry-run"
fi
# rsync command
echo -e "\n-------------------\n${date}\n-------------------\n" >> "${logs}"
time rsync -arhP --exclude=.* ${sim} "${src}/" "${dst}/" 2>&1 | tee -a "${logs}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment