Skip to content

Instantly share code, notes, and snippets.

@damonp
Created January 9, 2016 23:05
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 damonp/5803bde2fe45c2694c2a to your computer and use it in GitHub Desktop.
Save damonp/5803bde2fe45c2694c2a to your computer and use it in GitHub Desktop.
Backup configuration files before you break them.
#!/bin/bash
# $Id: tinyback.sh 33 2007-05-02 14:18:50Z damonp $
#
# Shell script to create backup of a list of files
#
# Copyright (c) 2007 Damon Parker < damonp@damonparker.org >
# Licensed under the GNU GPL. See http://www.gnu.org/licenses/gpl.html
# This script is not supported in any way. Use at your own risk.
#
# START configure
# OPTIONAL ITEMS
configfile=~/.tinyback
now=`date +"%m-%d-%Y"`
backfile="`echo $USER.$HOSTNAME`.$now.tar.gz"
cat=`which cat`
grep=`which grep`
tar=`which tar`
# END configure
# check for configfile, create if necessary
[ ! -f $configfile ] && touch $configfile || :
# convert relative paths to absolute paths
absolute_path () {
local startdir=`pwd`
if [ "${newfile}" = "${newfile#/}" ]; then
newfile=$startdir/$newfile;
fi
}
case "$1" in
'add' | 'a')
newfile=$2
absolute_path
# make sure that file or dir exists to backup
if [ ! -f $newfile ]; then
if [ ! -d $newfile ]; then
echo "$newfile does not exists"
exit 2
fi
fi
# make sure we don't add duplicates
cat $configfile | grep -w $newfile > /dev/null
if [ "$?" == "0" ]; then
echo "$newfile exists in $configfile"
exit 3
fi
# add file to backup list
echo "$newfile" >> $configfile
echo "$newfile added to $configfile"
;;
'backup' | 'b')
if [ "$2" == "" ]; then
echo "No host to backup files to supplied"
fi
# make sure we put backup in our own tmp and not in /tmp
[ ! -d ~/tmp ] && mkdir ~/tmp || :
if [ -f $configfile ]; then
files="`cat $configfile | grep -E -v "^#"`"
else
echo "File $configfile does not exists"
exit 3
fi
if [ "$files" == "" ]; then
echo "$configfile is empty, please add list of files/directories to backup"
echo "Use `$basename $0` add <filename>"
exit 2
fi
$tar -zcf ~/tmp/$backfile $files >/dev/null
scp ~/tmp/$backfile $2:
rm ~/tmp/$backfile
;;
'edit' | 'e')
pico=`which pico`
vim=`which vim`
if [ -x $pico ]; then
$pico $configfile
elif [ -x $vim ]; then
$vim $configfile
fi
;;
'help' | 'h')
echo "Usage: `basename $0` <command> [args]
add <filename> add filename to backup file list
backup <user@host> create backup file and copy to host using scp
edit edit backup file list with pico or vim
help this help file
list list files to be backed up
short commands a, b, e, h and l also supported";
;;
'list' | 'l')
echo "Files selected to be backed up:"
cat $configfile
;;
*)
echo "tinyback v0.8
Copyright (C) 2007 Damon Parker <damonp@damonparker.org>
type \"`basename $0` help\" for usage
"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment