You are not logged in.

#1 2013-05-17 03:44:58

ctarwater
Member
Registered: 2009-02-05
Posts: 300

Help with Bash script to backup local directory before git pull

I'm struggling a bit with this one and would appreciate any comments on the coding (or suggestions of other angles of attack).

I'm working on a bash script to perform the following actions:

1. Check github to see if a local git pull is needed
2. Backup current directory before performing git pull
  - if any error occurs then exit and email me
3. git pull
  - if any error occurs then exit and email me

The part I'm feeling the most unsure of is the error handling since I'm still very new to bash scripting and I'm not quite sure I have it correctly.

#!/bin/sh
set -o errexit					# Exit the script if an error occurs

remote=$(					
    git ls-remote -h origin master |		# Get commit hash from head of remote master repo (github)
    awk '{print $1}'				# Commit hash
)
local=$(git rev-parse HEAD)			# Get commit hash from head of server repo (dev/test)

if [[ $local == $remote ]]; then		# If the hashes match
    echo "No update required."			# Then no pull is needed
else						# If the hashes don't match then prepare to run our backup
   TIME=`date +"%b-%d-%y"`             		# Define the TIME variable as today's date.
   FILENAME="backup-$TIME.tar.gz"      		# Define the filename structure.
   SRCDIR="/cool-stuff-here"                    # Define directory to backup.
   DESDIR="/backup-of-cool-stuff-here"          # Define where to put the backup.
	tar -cpzf $DESDIR/$FILENAME $SRCDIR	# Perform the backup
    trap 'mailx -s "Pre git pull backup failed on dev" me@home.com' ERR		# Email me if our backup fails
	cd /repo             	                # Make sure we're in our repo directory
        git pull
    trap 'mailx -s "Git pull failed on dev" me@home.com' ERR			# Email me if our git-pull fails
fi

Offline

#2 2013-05-17 13:16:54

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Help with Bash script to backup local directory before git pull

I'm confused why you want to even do this -- git provides a backup for you at every commit.

As far as shell scripting goes, your use of trap is confusing at best.

if ! { commands; }; then
  # handle error. write something to stderr, send a mail, set a cat on fire...
fi

Last edited by falconindy (2013-05-17 13:18:12)

Offline

#3 2013-05-17 18:03:58

ctarwater
Member
Registered: 2009-02-05
Posts: 300

Re: Help with Bash script to backup local directory before git pull

I'm doing this for 2 reasons:
- as a scripting exercise
- because the local repo is a shared wordpress mess and people forget to make commits so I'd like a local backup that includes all changes that may not be in github (sadly).

Anyhow, thanks for the input about trap, I'm definitely confused by it's use at this point but have reworked the script without it.

#!/bin/sh
set -e						#Exit on any error.

TIME=`date +"%m.%d.%Y@%H:%M:%S"`             	# Define the TIME variable as today's date and time.
MONTH=`date +"%b"`				# Define MONTH as today's month.
FILENAME="backup-$TIME.tar.gz"      		# Define the filename structure.
SRCDIR="/stuff"           			# Define folder to backup.
DESDIR="/Backup/$MONTH/" 			# Define the backup folder location.
LOGFILE="$DESDIR/backup-$TIME.log"		# Store the output in a log.
REMOTE=$(git ls-remote -h origin master | awk '{print $1}')	# Get commit hash from head of remote master repo (github)				
LOCAL=$(git rev-parse HEAD)			# Get commit hash from head of server repo (dev/test)

if [[ $LOCAL == $REMOTE ]]; then		# If the hashes match
    echo "No update required."	>> $LOGFILE	# Then no pull is needed
else						# If the hashes don't match then prepare to run our backup

mkdir -p $DESDIR				# Create our directory if it doesn't exist.
echo "Backup started for "$TIME >> $LOGFILE	# Make it log what it's doing.
tar -cpzf $DESDIR/$FILENAME $SRCDIR >> $LOGFILE 2>&1		# Perform the backup.
echo "Backup Finished for "$TIME >> $LOGFILE	# Log that it's finished.

echo "Preparing for git pull " >> $LOGFILE	#Log what we're doing.
cd /stuff >> $LOGFILE 2>&1			# Make sure we're in our repo directory
git pull >> $LOGFILE 2>&1
echo "Git pull completed successfully. " >> $LOGFILE

mail -s "Backup log `date`" me@home.com < $LOGFILE	# Email the output to me.

fi

Offline

Board footer

Powered by FluxBB