Random scripts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
2.3 KiB

#!/bin/bash
set -e
declare -A SYNC_FOLDERS
SYNC_FOLDERS=(
[/media/Baranduin/backup/]=jd@twoflower:/home/jd/data/backup/
[/media/Baranduin/shared/]=jd@twoflower:/home/jd/data/shared/
)
#colors
reset=$( tput sgr0 )
bold=$( tput bold )
# Unstyled colors
red=$( tput setaf 1 || tput AF 1 )
green=$( tput setaf 2 || tput AF 2 )
yellow=$( tput setaf 3 || tput AF 3 )
# Bold colors
bldred=$bold$red
bldgrn=$bold$green
bldylw=$bold$yellow
function confirm {
echo ""
while true; do
read -rp "Use the above commands? [${bldylw}y${reset}es/${bldylw}n${reset}o/${bldylw}d${reset}ry-run/${bldylw}k${reset}eep]: " yn
case $yn in
[Yy]* ) break;;
[Nn]* ) exit;;
[Dd]* ) DRY_RUN="--dry-run"; break;;
[Kk]* ) DELETE=""; break;;
* ) echo "Please answer yes, no, dry-run, or keep.";;
esac
done
}
function rsync-to-remote {
echo "These commands will be ran:"
for SRC in "${!SYNC_FOLDERS[@]}"
do
echo " rsync" "$RSYNC_FLAGS" "${bldred}$DELETE${reset}" "$SRC" "${SYNC_FOLDERS[$SRC]}" "${bldgrn}$DRY_RUN${reset}"
done
confirm
for SRC in "${!SYNC_FOLDERS[@]}"
do
rsync $RSYNC_FLAGS $DELETE "$SRC" "${SYNC_FOLDERS[$SRC]}" $DRY_RUN
done
}
function rsync-to-local {
echo "These commands will be ran:"
for SRC in "${!SYNC_FOLDERS[@]}"
do
echo " rsync" "$RSYNC_FLAGS" "${bldred}$DELETE${reset}" "${SYNC_FOLDERS[$SRC]}" "$SRC" "${bldgrn}$DRY_RUN${reset}"
done
confirm
for SRC in "${!SYNC_FOLDERS[@]}"
do
rsync $RSYNC_FLAGS $DELETE "${SYNC_FOLDERS[$SRC]}" "$SRC" $DRY_RUN
done
}
# sync local changes to remote
# sync remote to local
# dry run
RSYNC_FLAGS="-avh"
DRY_RUN=""
DELETE="--delete"
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-d|--dry-run)
DRY_RUN="--dry-run"
shift
;;
-s|--sync)
SYNC=1
shift
;;
-k|--keep)
DELETE=""
shift
;;
-h|--help)
echo "Usage: backup [-d | -s ]"
echo " Back up files to twoflower"
echo "Options:"
echo " -d|--dry-run : Show what will happen but don't delete"
echo " -s|--sync : Copy from twoflower to this machine"
echo " -k|--keep : Keep missing files"
exit 0
;;
*)
NAME="$1"
shift
;;
esac
done
if [[ -n $SYNC ]]; then
rsync-to-local
else
rsync-to-remote
fi