aboutsummaryrefslogtreecommitdiff
path: root/backup.sh
diff options
context:
space:
mode:
authorMitch Riedstra <mitch@riedstra.us>2015-12-28 13:52:48 -0500
committerMitch Riedstra <mitch@riedstra.us>2015-12-28 13:52:48 -0500
commitd32ab30dc3611481cb55ce7b18bb1a80352f9146 (patch)
tree0b3ee3c8fefbfc8dfa98349bb22fc9ca1358137a /backup.sh
downloaddotfiles-d32ab30dc3611481cb55ce7b18bb1a80352f9146.tar.gz
dotfiles-d32ab30dc3611481cb55ce7b18bb1a80352f9146.tar.xz
Initial
Diffstat (limited to 'backup.sh')
-rw-r--r--backup.sh46
1 files changed, 46 insertions, 0 deletions
diff --git a/backup.sh b/backup.sh
new file mode 100644
index 0000000..1f0334e
--- /dev/null
+++ b/backup.sh
@@ -0,0 +1,46 @@
+#!/bin/sh
+# Backup script in less than 50 lines
+_dir="/backups/servers" hostname="" ssh_opts="" rsync_opts="" log=0 _pth="" _exc="exclude"
+logdir="/var/log/backups" log_fmt="+%m.%d.%y_.%H.%M.%S"
+if [ -z $_dir ] ; then
+ echo "You need to setup a base backup directory"
+fi
+while [ $# -gt 0 ] ; do case $1 in
+ -h) hostname="$2"; shift; shift; ;;
+ -n) rsync_opts="${rsync_opts}n"; shift; ;;
+ -l) log=1; shift; ;; -lt) log=2; shift; ;;
+ -e) _exc="$2"; shift; shift; ;;
+ -p) _pth="$2"; shift; shift; ;;
+ *) echo "Unknown option: $1"; exit 1; shift; ;;
+esac done
+if [ -z $hostname ] ; then
+ echo "Yo, you need to set a hostname"; exit 1;
+fi
+files="exclude key"
+for _file in $files ; do
+ if ! [ -f $_dir/$hostname/$_file ] ; then
+ echo "Can't find file: $_dir/$hostname/$_file"; exit 1;
+ fi
+done
+directories="content"
+for _directory in $directories ; do
+ if ! [ -d $_dir/$hostname/$_directory ] ; then
+ echo "Can't find dir: $_dir/$hostname/$_directory"; exit 1;
+ fi
+done
+if [ -e $_dir/$hostname/cfg ] ; then . $_dir/$hostname/cfg; fi
+if ! [ -d $logdir ] ; then mkdir $logdir ; fi
+
+log_file="$logdir/${hostname}.$(date $log_fmt).log"
+if ! [ -z $_pth ] ; then log_file="$logdir/${hostname}.$_pth.$(date $log_fmt).log" ; fi
+backup() {
+ rsync -avzHP$rsync_opts --numeric-ids --delete --exclude-from="$_dir/$hostname/$_exc" \
+ -e "ssh -i $_dir/$hostname/key $ssh_opts" "root@$hostname:/$_pth" "$_dir/$hostname/content/$_pth";
+}
+if [ $log -eq 2 ] ; then
+ backup | tee $log_file;
+elif [ $log -eq 1 ] ; then
+ backup >> $log_file;
+else
+ backup;
+fi