LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 06-16-2012, 12:46 AM   #1
macklenc
LQ Newbie
 
Registered: Mar 2009
Distribution: Ubuntu 8.10
Posts: 5

Rep: Reputation: 0
Angry cron is making a mess of file names...


So, I am currently writing a backup script, that will run by cron every so often (I am using 1 minute intervals for testing). When I run my script, everything works fine, the backup files are created properly. However, when cron runs the backup script, the file names are sometimes chopped or have a '?' placed in the name depending on the name. The name format follows something like this:
User-Desktop-15Jun-hourly-23-diff.tar
but when cron runs this the result is:
-15Jun-hourly-23-diff.tar

Any advice would be appreciated. I tried searching for quite a while, but I guess I cant explain this problem to google...
 
Old 06-16-2012, 02:01 AM   #2
xeleema
Member
 
Registered: Aug 2005
Location: D.i.t.h.o, Texas
Distribution: Slackware 13.x, rhel3/5, Solaris 8-10(sparc), HP-UX 11.x (pa-risc)
Posts: 987
Blog Entries: 4

Rep: Reputation: 248Reputation: 248Reputation: 248
Greetingz!

Whatever defines "User-Desktop-" in your script is not being outputted properly when the script creates the archive.
Try creating a TARGET_FILE variable that combines whatever uses "User-Desktop-" and whatever creates the "-15Jun-hourly-23-diff.tar" part.

In order to provide a clearer example, I'll need to see your script.
Be sure to put [code] before your script in your next post, and [/code] at the end.
 
Old 06-16-2012, 04:11 PM   #3
macklenc
LQ Newbie
 
Registered: Mar 2009
Distribution: Ubuntu 8.10
Posts: 5

Original Poster
Rep: Reputation: 0
Alright, here's the code, it isn't compact at all...
I have another script that generates this script, and the required files.
config/host contains only
Code:
Chris-Desktop
And here's the crontab entry:
Code:
*       *       *       *       *       bash ~/setup/backup-Chris-Desktop-06.15.2012.sh > ~/setup/backup.log
Thanks again
Code:
#!/bin/bash

set -x
SSH=0
DIRECTORIES="/home/chris"
BACKUPDIR="/home/macklenc/backup"
REMOTECOM=enterprise

REMOTEUSER=macklenc

monthly=1
monthlyInc=0

weekly=0
weeklyInc=0
weeklyDiff=1

daily=0
dailyInc=0
dailyDiff=1

hourly=0
hourlyInc=0
hourlyDiff=1

COMPUTER=$(cat config/host)


PATH=/usr/local/bin:/usr/bin:/bin
DOW=`date +%a`              # Day of the week e.g. Mon
DOM=`date +%d`              # Date of the Month e.g. 27
DM=`date +%d%b`             # Date and Month e.g. 27Sep
DAT=$(date +%Y-%m-%d)
TIME=$(date +%H)

#monthly - keep all
#monthlyInc - Overwrite monthly
#monthlyDiff - Keep for specified duration

mkdir -p $BACKUPDIR/$COMPUTER

monthlyF() {
	month="$DM-full.tar"
	if [ $SSH -eq 0 ]
	then
		tar -a -c -f - $DIRECTORIES | ssh $REMOTEUSER@$REMOTECOM "cat > $BACKUPDIR/$COMPUTER-$month" 
	else
		tar -a -c -f $BACKUPDIR/$COMPUTER-$month $DIRECTORIES 
	fi
}

monthlyIncF() {
	monthi="monthly-full-inc.tar"
	if [ $SSH -eq 0 ]
	then
		tar -a -c -f - $DIRECTORIES | ssh $REMOTEUSER@$REMOTECOM "cat > $BACKUPDIR/$COMPUTER-$monthi" 
	else
		tar -a -c -f $BACKUPDIR/$COMPUTER-$monthi $DIRECTORIES 
	fi
}

weeklyF() {
	week="$DM-weekly.tar"
	if [ $SSH -eq 0 ]
	then
		tar -a -c -f - $DIRECTORIES --newer-mtime='week ago' | ssh $REMOTEUSER@$REMOTECOM "cat > $BACKUPDIR/$COMPUTER-$week" 
	else
		tar -a -c -f $BACKUPDIR/$COMPUTER-$week $DIRECTORIES --newer-mtime='week ago' 
	fi

	if [ $SSH -eq 0 ]
	then
		echo "false" | ssh $REMOTEUSER@$REMOTECOM "cat > $BACKUPDIR/$COMPUTER/weeklydiff.dat"
	else
		echo "false" | cat > $BACKUPDIR/$COMPUTER/weeklydiff.dat
	fi
}

weeklyIncF() {
	weeki="weekly-inc.tar"
	if [ $SSH -eq 0 ]
	then
		tar -a -c -f - $DIRECTORIES --newer-mtime='week ago' | ssh $REMOTEUSER@$REMOTECOM "cat > $BACKUPDIR/$COMPUTER-$weeki" 
	else
		tar -a -c -f $BACKUPDIR/$COMPUTER-$weeki $DIRECTORIES --newer-mtime='week ago' 
	fi

	if [ $SSH -eq 0 ]
	then
		echo "false" | ssh $REMOTEUSER@$REMOTECOM "cat > $BACKUPDIR/$COMPUTER/weeklydiff.dat"
	else
		echo "false" | cat > $BACKUPDIR/$COMPUTER/weeklydiff.dat
	fi
}

weeklyDiffF() {
	weekd="weekly-diff-$DM.tar"
	if [ $SSH -eq 0 ]
	then
		tar -a -c -f - $DIRECTORIES --newer-mtime='week ago' | ssh $REMOTEUSER@$REMOTECOM "cat > $BACKUPDIR/$COMPUTER-$weekd" 
	else
		tar -a -c -f $BACKUPDIR/$COMPUTER-$weekd $DIRECTORIES --newer-mtime='week ago' 
	fi

	if [ $SSH -eq 0 ]
	then
		echo "true" | ssh $REMOTEUSER@$REMOTECOM "cat > $BACKUPDIR/$COMPUTER/weeklydiff.dat"
	else
		echo "true" | cat > $BACKUPDIR/$COMPUTER/weeklydiff.dat
	fi
}

dailyF() {
	day="$DM-daily.tar"
	if [ $SSH -eq 0 ]
	then
		tar -a -c -f - $DIRECTORIES --newer-mtime='day ago' | ssh $REMOTEUSER@$REMOTECOM "cat > $BACKUPDIR/$COMPUTER-$day"
	else
		tar -a -c -f $BACKUPDIR/$COMPUTER-$day $DIRECTORIES --newer-mtime='day ago' 
	fi

	if [ $SSH -eq 0 ]
	then
		echo "false" | ssh $REMOTEUSER@$REMOTECOM "cat > $BACKUPDIR/$COMPUTER/dailydiff.dat"
	else
		echo "false" | cat > $BACKUPDIR/$COMPUTER/dailydiff.dat
	fi
}

dailyIncF() {
	dayi="daily-inc.tar"
	if [ $SSH -eq 0 ]
	then
		tar -a -c -f - $DIRECTORIES --newer-mtime='day ago' | ssh $REMOTEUSER@$REMOTECOM "cat > $BACKUPDIR/$COMPUTER-$dayi"
	else
		tar -a -c -f $BACKUPDIR/$COMPUTER-$dayi $DIRECTORIES --newer-mtime='day ago' 
	fi

	if [ $SSH -eq 0 ]
	then
		echo "false" | ssh $REMOTEUSER@$REMOTECOM "cat > $BACKUPDIR/$COMPUTER/dailydiff.dat"
	else
		echo "false" | cat > $BACKUPDIR/$COMPUTER/dailydiff.dat
	fi
}

dailyDiffF() {
	dayd="daily-diff-$DM.tar"
	if [ $SSH -eq 0 ]
	then
		tar -a -c -f - $DIRECTORIES --newer-mtime='day ago' | ssh $REMOTEUSER@$REMOTECOM "cat > $BACKUPDIR/$COMPUTER-$dayd"
	else
		tar -a -c -f $BACKUPDIR/$COMPUTER-$dayd $DIRECTORIES --newer-mtime='day ago' 
	fi

	if [ $SSH -eq 0 ]
	then
		echo "true" | ssh $REMOTEUSER@$REMOTECOM "cat > $BACKUPDIR/$COMPUTER/dailydiff.dat"
	else
		echo "true" | cat > $BACKUPDIR/$COMPUTER/dailydiff.dat
	fi
}

hourlyF() {
	hour="$DM-hourly-$(date +%H).tar"
	if [ $SSH -eq 0 ]
	then
		tar -a -c -f - $DIRECTORIES --newer-mtime='hour ago' | ssh $REMOTEUSER@$REMOTECOM "cat > $BACKUPDIR/$COMPUTER-$hour" 
	else
		tar -a -c -f $BACKUPDIR/$COMPUTER-$hour $DIRECTORIES --newer-mtime='hour ago' 
	fi

	if [ $SSH -eq 0 ]
	then
		echo "false" | ssh $REMOTEUSER@$REMOTECOM "cat > $BACKUPDIR/$COMPUTER/hourlydiff.dat"
	else
		echo "false" | cat > $BACKUPDIR/$COMPUTER/hourlydiff.dat
	fi
}

hourlyincF() {
	houri="hourly-$(date +%H)-inc.tar"
	if [ $SSH -eq 0 ]
	then
		tar -a -c -f - $DIRECTORIES --newer-mtime='hour ago' | ssh $REMOTEUSER@$REMOTECOM "cat > $BACKUPDIR/$COMPUTER-$houri" 
	else
		tar -a -c -f $BACKUPDIR/$COMPUTER-$houri $DIRECTORIES --newer-mtime='hour ago' 
	fi

	if [ $SSH -eq 0 ]
	then
		echo "false" | ssh $REMOTEUSER@$REMOTECOM "cat > $BACKUPDIR/$COMPUTER/hourlydiff.dat"
	else
		echo "false" | cat > $BACKUPDIR/$COMPUTER/hourlydiff.dat
	fi
}

hourlyDiffF() {
	hourd="$DM-hourly-$(date +%H)-diff.tar"
	if [ $SSH -eq 0 ]
	then
		tar -a -c -f - $DIRECTORIES --newer-mtime='hour ago' | ssh $REMOTEUSER@$REMOTECOM "cat > $BACKUPDIR/$COMPUTER-$hourd" 
	else
		tar -a -c -f $BACKUPDIR/$COMPUTER-$hourd $DIRECTORIES --newer-mtime='hour ago' 
	fi

	if [ $SSH -eq 0 ]
	then
		echo "true" | ssh $REMOTEUSER@$REMOTECOM "cat > $BACKUPDIR/$COMPUTER/hourlydiff.dat"
	else
		echo "true" | cat > $BACKUPDIR/$COMPUTER/hourlydiff.dat
	fi
}

if [[ $(date +%M) == "00" || $1 == "-h" ]]
then
	if [ $hourly -eq 1 ]; then
		hourlyF
	elif [ $hourlyInc -eq 1 ]; then
		hourlyIncF
	elif [ $hourlyDiff -eq 1 ]; then
		hourlyDiffF
	fi
fi

if [ $TIME == "00" ]
then
	{
		if [ $daily -eq 1 ]; then
			dailyF
		elif [ $dailyInc -eq 1 ]; then
			dailyIncF
		elif [ $dailyDiff -eq 1 ]; then
			dailyDiffF
		fi

		if [ $DOW == "Sat" ]; then
			{			
				if [ $weekly -eq 1 ]; then
					weeklyF
				elif [ $weeklyInc -eq 1 ]; then
					weeklyIncF
				elif [ $weeklyDiff -eq 1 ]; then
					weeklyDiffF
				fi
				
				if [ $DOM == "01" ]; then
					if [ $monthly -eq 1 ]; then
						monthlyF
					elif [ $monthlyInc -eq 1 ]; then
						monthlyIncF
					fi
				fi
			}
		fi
	}
fi

if [ $SSH -eq 1 ]
then
	(cd $BACKUPDIR; bash filemanager.sh)
else
	ssh $REMOTEUSER@$REMOTECOM "cd $BACKUPDIR; bash filemanager.sh"
fi
 
Old 06-19-2012, 01:10 AM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,242

Rep: Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024
1. if you have
Code:
#!/bin/bash
at the top of each script (& you should) then there is no need to reference it again eg
Code:
# this 
*       *       *       *       *       bash ~/setup/backup-Chris-Desktop-06.15.2012.sh > ~/setup/backup.log

# becomes
*       *       *       *       *       /setup/backup-Chris-Desktop-06.15.2012.sh > ~/setup/backup.log
and so on..

Also, use the full path to the 'host' file eg
Code:
COMPUTER=$(cat  /setup/config/host)
In fact, when running anything in cron, its usually a good idea to use absolute full paths, although you have set your own PATH I see.

For enhanced debugging, use
Code:
#this
set -xv

instead of just
set -x
I also recommend http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS

HTH
 
  


Reply

Tags
cron, debian, ssh


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
Wrote to partition while making image file w/ dd: Did I mess it up? vincebs Linux - General 3 06-24-2010 08:18 AM
[SOLVED] How to changing same file names by their directory names? bayaraa_u Linux - General 3 04-09-2010 08:26 AM
shell script using /etc/cron.hourly to execute cron.php file? rioguia Programming 3 06-11-2008 08:09 AM
cron file 000-delay.cron (it is on my FC5) - what is the purpose? jtmoon Linux - Server 1 03-08-2007 11:15 AM
Making usb stick with device names mrsolo Linux - Hardware 0 11-16-2004 06:00 PM


All times are GMT -5. The time now is 05:20 AM.

Main Menu
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