Skip to content

Instantly share code, notes, and snippets.

@Josh5
Last active September 8, 2018 03:24
Show Gist options
  • Save Josh5/976d10b862ec419bd9b90fa71b27fd15 to your computer and use it in GitHub Desktop.
Save Josh5/976d10b862ec419bd9b90fa71b27fd15 to your computer and use it in GitHub Desktop.
cloudsync
#!/bin/bash
# @Author: jsunnex
# @Date: 2017-10-02 10:18:03
# @Last Modified by: jsunnex
# @Last Modified time: 2018-08-17 14:06:36
#
#
# Use this script to backup your systemo config for quick redeploy
#
# chmod +x /opt/cloudsync.sh
# crontab -e
#
# CRON:
# 30 */1 * * * /opt/cloudsync.sh >> /tmp/cloudsync.log
#
######################################### CONFIG #########################################
#
# Place this in ~/.cloudsync
# All paths must be absolute!
#
### Home Sync:
## These folders will be synced both ways
## NOTE: Only folders contained in home can be synced
SYNC_DIR="";
SYNC_NAME="";
# Locations in home dir to sync
SYNC_LOCATIONS="";
### System Backup:
## These folders will be backed up in a single direction
BACKUP_DIR="";
BACKUP_NAME="";
# Locations on system to backup
BACKUP_LOCATIONS="";
# Strings to ignore
BACKUP_IGNORE_STRINGS="";
#
##########################################################################################
if ! which rsync >/dev/null; then
echo "You need to first install rsync - 'sudo apt install rsync' should do the trick";
exit 1;
fi
if ! which unison >/dev/null; then
echo "You need to first install unison - 'sudo apt install unison' should do the trick";
exit 1;
fi
# Read local config file
source ${HOME}/.cloudsync;
# Setup backup and sync destinations
BACKUP_DESTINATION="${BACKUP_DIR}/${BACKUP_NAME}";
SYNC_DESTINATION="${SYNC_DIR}/sync/${SYNC_NAME}";
mkdir -p ${BACKUP_DESTINATION};
if [[ "${SYNC_LOCATIONS}" != "" && ! -d "${SYNC_DESTINATION}" ]]; then
echo "It is not safe to point to a non-existant sync location. This is a good way to lose all your files.";
echo
echo "First create the directroy ${SYNC_DESTINATION}, then run this script again..."
echo " - mkdir -p ${SYNC_DESTINATION}"
echo
exit 1;
fi
stage_header() {
echo -e "\e[1;34m"
echo -e "############"
echo -e "## "
echo -e "## ${1}";
echo -e "## "
echo -e "############"
echo -e "\e[0m"
}
cloudbackup () {
# Read backup stamp
if [[ "${BACKUP_LOCATIONS}" != "" ]]; then
DATE_NOW=$(date +"%Y-%m-%d");
DATE_LAST=$(date -d $(cat ${BACKUP_DESTINATION}/.last_backup) +"%Y-%m-%d");
if [[ "${DATE_NOW}" > "${DATE_LAST}" ]]; then
stage_header "The last backup was run on ${DATE_LAST}. Time to run another one...";
ARGS="";
for ignore in ${BACKUP_IGNORE_STRINGS}; do
ARGS="$ARGS --exclude=*${ignore}*"
done
for loc in ${BACKUP_LOCATIONS}; do
rsync -aAXvR ${ARGS} ${loc} ${BACKUP_DESTINATION}
done
echo ${DATE_NOW} > ${BACKUP_DESTINATION}/.last_backup;
else
stage_header "Not running backup. The last backup was run on ${DATE_LAST}.";
fi
fi
}
cloudsync () {
if [[ "${SYNC_LOCATIONS}" != "" ]]; then
ARGS="-auto -silent -dontchmod -perms=0";
for arg in $@; do
if [[ "${arg}" == "-v" ]]; then
ARGS=" -dontchmod -perms=0";
fi
done
for arg in $@; do
if [[ "${arg}" == "-a" ]]; then
ARGS="${ARGS} -auto";
fi
if [[ "${arg}" == "-s" ]]; then
ARGS="${ARGS} -silent";
fi
if [[ "${arg}" == "--no-perms" ]]; then
ARGS="${ARGS} -perms=0";
fi
done
for loc in ${SYNC_LOCATIONS}; do
dest=${SYNC_DESTINATION}${loc#$HOME};
stage_header "Syncing ${loc} << >> ${dest}";
unison ${ARGS} ${loc} ${dest};
echo
# TODO: Add file permission ledger and run a command post sync of any file that updates file permissions. Some sync locations do not support file permissions
done
fi
}
###################
###
### EXECUTE:
###
DATESTAMP=`date "+%Y/%m/%d-%H:%M:%S"`;
echo -e "\e[1;32m"
echo -e "##############################################"
echo -e " Running cloudsync... "
echo
echo -e " Executed at ${DATESTAMP} "
echo -e "##############################################"
echo -e "\e[0m"
cloudbackup;
cloudsync $@;
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment