1

I'm trying to get the script here working on my Ubuntu Server to backup the SVN repository via a cron.

#!/bin/bash

BACKUP_DIR="/backup/repository"
REPOSITORY_DIR="/home/svn/repository"
#search in repository folder to find all the repository names
ls -Al --time-style=long-iso /home/svn/repository/ | grep '^d' | awk '{print $8}' | while read line
do
    if [ ! -d $BACKUP_DIR"/"$line ]; then
        mkdir $BACKUP_DIR"/"$line
    fi

    #Getting revision number
    REVISION=`cat $REPOSITORY_DIR"/"$line"/db/current" | awk '{print $1}'`

    #Archive last backup
    tar -czf $BACKUP_DIR"/"$line"-last.tar.gz" $BACKUP_DIR"/"$line"/"

    #Dangerous :)
    if [ -n "$BACKUP_DIR" ]; then
    rm -rf $BACKUP_DIR"/"$line"/*"
    fi

    #Check to see if exists a hot backup
    if [ -d $BACKUP_DIR"/"$line"/"$line"-"$REVISION ]; then
        echo "Skipping Backup ! Backup Already Exists"
    else
        echo "Doing backup for "$line
        /usr/bin/svn-hot-backup $REPOSITORY_DIR"/"$line $BACKUP_DIR"/"$line
    fi

done

I've changed the paths to the correct ones, and subversion-tools is installed, however I get the following errors if I try and run sudo bash svnbackup.sh:

: command not foundline 2:
svnfullbackup.sh: line31: syntax error near unexpected token 'done'
svnfullbackup.sh: line31: 'done'

I presume these errors are why the cron didn't work at its alloted time. How can I fix them?

2 Answers 2

0

The "command not found" error looks like it was overwritten which is an indication of the wrong type of line endings. Run dos2unix on your script file to fix that. It's possible that it's some other stray character. Display your script using hexdump or cat -v to reveal that. This problem often occurs when you use a Windows editor on a Unix file.

Instead of this line:

ls -Al --time-style=long-iso /home/svn/repository/ | grep '^d' | awk '{print $8}' | while read line

use this:

find "$REPOSITORY_DIR" -maxdepth 1 -type d | while read -r line

which doesn't rely on ls which is not meant for use in scripts/pipes. Doing it this way also is more likely to work in cases where filenames include spaces.

You can do the quoting the way you have it, but this example might be better since it works if there are spaces in filenames:

if [ ! -d "$BACKUP_DIR/$line" ]; then
    mkdir "$BACKUP_DIR/$line"

It's better to use $() instead of backticks and cat is unnecessary (also simplified quoting):

REVISION=$(awk '{print $1}' "$REPOSITORY_DIR/$line/db/current" )

And there are more places where you can simplify quoting.

2
  • dos2unix did indeed fix the script, problem occurred from saving it onto the server via Notepad++ in Windows. It's currently duplicating the repository name, in the sense that it looks for /home/svn/repo/home/svn/repo instead of just once. But at least it runs now! Thanks for your answer.
    – user34542
    Jul 15, 2010 at 13:40
  • 1
    @bcmcfc: If you're using my find command, it includes the directory name in its output. You will need to add -printf '%f\n' at the end of the arguments. Sorry I didn't spot that. Jul 15, 2010 at 15:50
0

I can't seem to find anything wrong with the script, except that it's not gonna work when you have repository names that contain spaces.... is that the case?

You must log in to answer this question.