HowTo

Tarup - Quick Backup Script

Don't edit config files without it

First rule of systems administration: Backup up your config files before you edit them!  That can be a pain in the butt sometimes, so the rule is often ignored.  As my new friend Josh Hall says "sys admins should be lazy, but efficent."  In the spirit of being lazy and efficient, I present the tarup.sh script. In the interest of full disclosure, I got the idea for this script while attending RHEL training conducted by instructor Josh Hall.

The script

To get started, copy this simple bash script and follow the instructions below.


#!/bin/bash 
#----------------------------------------------------------- 
# NAME: tarup.sh 
# Author: Chris Updegrove | chrisup at gmail dot com 
# WHAT: A script for backing up stuff 
# First argument: The directory you want to back up 
# Example: ./tarup.sh /usr/local/etc 
# Tip: Add location of script as an alias for east execution 
# Should be added to the global bashrc file for all users 
#------------------------------------------------------------ 

# ----------------------------------------------------------- 
# START WITH SOME VARIABLES 
SRC=$1 
DEST=/home/backup/$(basename $SRC)-$(date '+%Y-%m-%d-%H:%M') 
TAR='tar -zcvf' 
EXT='.tar.gz' 
# --------------------------------------------------------------- 

# --------------------------------------------------------------- 
# The commands 
if [ -e $DEST ] 
then echo "WARNING: $DEST exists" 
read -p "Press Ctrl-c to exit or Enter to continue:" 
fi 

echo $TAR $DEST$EXT $SRC 
$TAR $DEST$EXT $SRC 

echo "$SRC was backed up to $DEST$EXT" 


1.) Put the script under /usr/local/bin or /$HOME/scripts or something. Don't forget to chmod + x the script after download.

2.) Add an alias for the script to your .bashrc like so 
alias tarup='sh /usr/local/bin/scripts/tarup.sh' 

3.) Create the destination directory and update the $DEST variable in the script. 
DEST=/home/backups/$(basename $SRC)-$(date '+%Y-%m-%d-%H:%M')

4.) Run the script like this: 
[user@host] # ./tarup.sh /usr/local/etc/

That's it. After you run the script, you'll see a tar file with the directory basename like so:
/home/backups/etc-2008-11-15-20:33.tar.gz[user@host] # ls -la /home/backups -rwxr-xr-x 1 user users 310352 Nov 15 20:43 etc-2008-11-15-20:33.tar.gz

Back To Articles