Skip to content

Instantly share code, notes, and snippets.

@zakkak
Created September 29, 2014 00:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zakkak/45401b2c9009986913fb to your computer and use it in GitHub Desktop.
Save zakkak/45401b2c9009986913fb to your computer and use it in GitHub Desktop.
A Sync/BackUp script
#!/bin/bash
#########################################################################
# #
# File: backup_settings.sh A sync/backup script #
# #
# ++++++++++++++++++++++++++++++ #
# Copyright (C) 2013 + Author: Foivos S. Zakkak + #
# + Website: foivos.zakkak.net + #
# + Email: foivos@zakkak.net + #
# ++++++++++++++++++++++++++++++ #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or (at #
# your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, but #
# WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
# General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
# #
#########################################################################
########################################################################
# #
# This is a script to sync configuration files between two directories #
# (HOME and BACKUP_DIR) #
# #
# The files are first diffed and if they are not same the most recent #
# overwrites the older one. #
# #
# Limitations: The script does not adapt to file deletions #
# #
########################################################################
IFS=$(echo -en "\n\b")
BACKUP_DIR=$HOME/"SparkleShare/settings"
mkdir -p $BACKUP_DIR
chmod 700 $BACKUP_DIR
FILES="
Documents/zakkak.vcf
.bash_aliases
.bash_profile
.zshrc
.indent.pro
.tmux.conf
.latexmkrc
.emacs
.emacs.el
.dircolors
.gitconfig
.gitignore
.mbsyncrc
.fonts.conf"
FOLDERS="
.config/htop
.fonts
bin"
CURR_DIR=$(pwd)
cd $HOME
for d in $FOLDERS
do
for sd in `find $d -type f 2> /dev/null`
do
FILES+="
${sd}" # DON'T indent this
done
done
cd $CURR_DIR
cd $BACKUP_DIR
for d in $FOLDERS
do
for sd in `find $d -type f 2> /dev/null`
do
# if [[ ! "$FILES" =~ "$sd" ]] # This check is too expensive
# then
FILES+="
${sd}" # DON'T indent this
# fi
done
done
# Eliminate duplicates
IFS=
FILES=`echo -n $FILES | sort | uniq`
IFS=$(echo -en "\n\b")
cd $CURR_DIR
for f in $FILES
do
LOCAL="$HOME/$f"
BACKUP="$BACKUP_DIR/$f"
diff $LOCAL $BACKUP 2> /dev/null > /dev/null
RES=$?
if [ $RES -ne 2 ] # If diff didn't fail (both files exist)
then
if [ $RES -eq 1 ] # if there are diffs between the files
then
# If local is newer than backup copy to backup else fetch
# from backup
if [ $LOCAL -nt $BACKUP ]
then
cp $LOCAL $BACKUP
echo -e "\033[33;1m --> \033[m" $f
else
cp $BACKUP $LOCAL
echo -e "\033[31;1m <-- \033[m" $f
fi
fi
else
if [ -f $LOCAL ]
then
DIR=${BACKUP%/*}
if [ ! -d $DIR ]
then
mkdir -p $DIR
fi
cp $LOCAL $BACKUP
echo -e "\033[33;1m --> \033[m" $f
elif [ -f $BACKUP ]
then
DIR=${LOCAL%/*}
if [ ! -d $DIR ]
then
mkdir -p $DIR
fi
cp $BACKUP $LOCAL
echo -e "\033[31;1m <-- \033[m" $f
else
echo -e "\033[37;41;1mFile not found:\033[m" $f
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment