LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-17-2008, 10:42 AM   #1
onthefritz
Member
 
Registered: Dec 2005
Location: Colorado
Distribution: Ubuntu, RHEL, openSUSE
Posts: 105

Rep: Reputation: 19
Backup script issues


I have a backup script I created but I keep getting errors. Can someone look through it and let me know where I messed up?
Code:
#!/bin/bash
# -------------------------------------
# Author: Fritz
# title: NightlyBackup.sh
# Description: This script is to backup user files on Linux systems.
#              The backups are put into tar balls and gzip is used to compress.
# -------------------------------------
#
#
# Backup Directories
# DIRNUM is the number of directories needing to be backed up. I have excluded 0 for ease of use.
DIRNUM="1"
BACKUPDIR[1]="/home/OTF/Desktop/"
#BACKUPDIR[2]=""
#BACKUPDIR[3]=""
#BACKUPDIR[4]=""
#BACKUPDIR[5]=""
DIRNUM=$((DIRNUM+1))
COUNT="1"
STATUS="0"

# Check for the /backup directory. If it is not there, create it.
if [ ! -d /backup ];
then
mkdir /backup
fi

# Pull the backups with while loop
while [ $COUNT < $DIRNUM ]
do
tar czvf "/backup/Backup$(date +%Y%m%d).tgz" $BACKUPDIR[$COUNT] >> "/var/log/backup.$(date +%Y%m%d).log" 2>&1
echo "Backup of directory $COUNT is done"
COUNT=$((COUNT+1))
done

# Clean up old backups
find /backup -maxdepth 0 -mtime 7 -exec rm {} \;
if $STATUS;
then
echo "STATUS: BACKUP COMPLETE"
else
echo "STATUS: BACKUP FAILED!"
fi
This script is going to be used on multiple systems and end users will be able to change what they backup if needed. (They know a little about scripting and understand what numbers to change.)

Here is the error I get:
Code:
./backup.sh: line 29: 2: No such file or directory
./backup.sh: line 38: 0: command not found
STATUS: BACKUP FAILED!

The FAILED part is no big deal. I haven't put in error checking yet. but the No such file or directory I don't understand.

thanks for the help.

OTF
 
Old 06-17-2008, 11:08 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
line 29:
Code:
while [ $COUNT -lt $DIRNUM ]
use numerical comparison operators -lt, -eq, -ne and so on, since the = < <= are for string comparison.

line 38:
Code:
if [ $STATUS -eq 0 ]
the "if $STATUS" construct without square brackets, tries to evaluate the expression as a command and returns its exit status. Also take in mind that in bash you cannot use
Code:
if [ 0 ]
if [ 1 ]
to return true or false respectively. A single argument without relational operators, is treated as a string and the result of the test is always true.
 
Old 06-17-2008, 11:09 AM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,671
Blog Entries: 54

Rep: Reputation: 2953Reputation: 2953Reputation: 2953Reputation: 2953Reputation: 2953Reputation: 2953Reputation: 2953Reputation: 2953Reputation: 2953Reputation: 2953Reputation: 2953
Since it's all explained already here's just a simpler, shorter example based on what you wrote:
Code:
#!/bin/bash
tellMe() { case "$?" in 0) echo "completed OK.";; *) echo "FAILED.";; esac; }
BACKUPDIR=( "/home/OTF/Desktop/" "/tmp" )
if [ ! -d /backup ]; then mkdir /backup || exit 127; fi
for ((NUMBER=0;NUMBER<${#BACKUPDIR[@]};NUMBER++)); do
        echo -en "Backup of ${BACKUPDIR[$NUMBER]} "
        tar -czf "/backup/Backup$(date +%Y%m%d).tgz" "${BACKUPDIR[$NUMBER]}" \
        >> "/var/log/backup.$(date +%Y%m%d).log" 2>&1; tellMe
done
echo -en "Deleting -mtime 7 backups: "
find /backup -maxdepth 0 -mtime 7 -exec rm {} \;
tellMe
exit 0

Last edited by unSpawn; 06-17-2008 at 11:10 AM.
 
Old 06-18-2008, 07:32 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Just a minor pt:

mkdir /backup

is an absolute path. Normally a non-root user would not be able to create dir there, not to mention you aren't checking for failure eg

Code:
mkdir /backup
if [[ $? -ne 0 ]]
then 
    echo "failed to create backup dir"
    exit 1
fi
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
strange backup issues tanoatlq Linux - Software 6 05-16-2008 11:33 AM
Backup Exec Permissions issues greencabby Linux - Enterprise 1 02-13-2007 03:46 PM
backup server - permission issues Red Squirrel Linux - Software 3 02-26-2006 04:52 PM
issues with mirc script accessing our php script dtra Linux - Software 2 07-02-2005 05:30 PM
Tape backup issues joe_sixpack Linux - General 0 10-02-2003 08:06 PM


All times are GMT -5. The time now is 10:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration