cancel
Showing results for 
Search instead for 
Did you mean: 

Can I set a timeout to cp?

SOLVED
Go to solution
Eric Antunes
Honored Contributor

Can I set a timeout to cp?

I cold backup everyday the database to a backup/test server via NFS but sometimes I shutdown that backup server and, since the backup script is sequential (shutdown db, cp and startup db), the database will not start if the backup server in not up or there is any kind of problem with NFS. I always can do this with 2 separate scripts but it is easier to cron just one.

So is it possible to set a 90 minutes timeout for cp for example?

Thanks,

Eric Antunes
Each and every day is a good day to learn.
49 REPLIES
Indira Aramandla
Honored Contributor

Re: Can I set a timeout to cp?

Hi Eric,

In you script after the database shutdown and when you issue the cp command you chekc for the retun code.

Whne the retun code is not zero it. not successfull then define the following and have the sleep wait_time in a loop.

Eg:-

WAIT_TIME=30
TIMEOUT=900
TIMER=0
Check with the return code of the cp command and then have the following in a loop.
while [ $retruncode -eq 1 ]
do
echo "Waiting for the database copy..."
sleep ${WAIT_TIME}
TIMER=`expr $TIMER + ${WAIT_TIME}`
if [ $TIMER -ge $TIMEOUT ] ; then
echo "\nWARNING: Timeout while waiting for copying to complete.\n"
RC=8
fi
done

IA
Never give up, Keep Trying
Muthukumar_5
Honored Contributor

Re: Can I set a timeout to cp?

I hope cp process will not make more time to process. You have to check the network connection with backup-server is up / not. If up then do copy then startup db again.

If you want to make timeout to cp process then,

execute cp operation in a process with background mode. Get the PID and sleep for 90 minutues and check PID is completed or not. If not kill that.

Example:

shutdown() {
..
}
copy() {

rcp backupserver:/tmp/test/*.db /database/

}
start(){
...
}


shutdown
copy &
PID=$!
start

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: Can I set a timeout to cp?

Sorry missed to say the check process and kill them as,

shutdown
copy &
PID=$!
sleep 90860
kill -9 $PID 2>/dev/null
start

hth.
Easy to suggest when don't know about the problem!
Stephen Keane
Honored Contributor

Re: Can I set a timeout to cp?

Muthukumar,

that means even if the cp is successful, you still have to wait 90 minutes!

You'd want to sleep for 1 minute and check the PID. Put the sleep/check in a loop that executes 90 times, unless the PID disappears in which case it would break/stop.

Muthukumar_5
Honored Contributor

Re: Can I set a timeout to cp?

I have outlined. That is all. :)

Basic concept is we have to make cp operation in background and control that with it's PID. We can add points like getting return code from cp and ps -ef | grep -v grep | grep -q $PID return code to make sleep 90 minutes operation.

hth.
Easy to suggest when don't know about the problem!
Eric Antunes
Honored Contributor

Re: Can I set a timeout to cp?

Hi all,

I'm analysing the proposed solutions to assign points more correctly after.

Thanks,

Eric Antunes

Each and every day is a good day to learn.
Eric Antunes
Honored Contributor

Re: Can I set a timeout to cp?

Ok, here's my test script:

WAIT_TIME=30
TIMEOUT=300
TIMER=0

SOURCE_DIR=/disc1/patches
DESTINATION_DIR=//patches

cp -R $SOURCE_DIR/* $DESTINATION_DIR

while [ $exit_code -eq 1]
do
sleep ${WAIT_TIME}
TIMER=`expr $TIMER + ${WAIT_TIME}`
if [$TIMER ge $TIMEOUT]; then
echo "\WARNING: ...!\n"
exit_code=8
fi

But I get:

"Syntax error at line 49 : `do' is not matched.". I'm missing something with the "while do" cicle...?
Each and every day is a good day to learn.
Stephen Keane
Honored Contributor

Re: Can I set a timeout to cp?

Yes

done

if .. fi

do .. done

Yogeeraj_1
Honored Contributor

Re: Can I set a timeout to cp?

hi eric,

you may also wish to add the "-p" switch when doing the cp -R...

-p will cause cp to preserve in the copy as many of the modification time, access time, file mode, user ID, and group ID as allowed by permissions.

hope this helps too!

regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Eric Antunes
Honored Contributor

Re: Can I set a timeout to cp?

Ok,

Now I get:

"A ] character is missing"

Any clue??

Eric
Each and every day is a good day to learn.
RAC_1
Honored Contributor

Re: Can I set a timeout to cp?

I would try to address it in different way. Before you shutdown the database, why not check for NFS mount, nfs server and do a test copy?? If that is OK, you shutdown database and start copy.

Now depending upon the size of the data to be copied you can also put a wait time for cp to comlpete. If it does not, kill cp and start database.

One catch here is to take into consideration the load on system/network load that will impact the copy time. And hence copy wait time needs to be adjusted.

To address this you can have another script/check in same script to keep comparing the source data and copied data.

Anil
There is no substitute to HARDWORK
Eric Antunes
Honored Contributor

Re: Can I set a timeout to cp?

RAC,

Seems an excelent idea. With "ping -vr -n 5" for example but how do I put this testing into a script??

Eric

Each and every day is a good day to learn.
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Can I set a timeout to cp?

Let me suggest a much more elegant Plan C and you won't need to worry about no stinkin' timeouts.

1) Shutdown database
2) Create a vxfs snapshot of all your Oracle filesystems -- this will take only seconds
3) Restart your database
4) NFS exportfs your vxfs snapshots (read-only)
5) NFS Mount (read only) your snapshots on the client
6) Do the copies
7) NFS unmount the snapshots on the client
8) exports -i -u /yoursnapshot(s)
9) umount the snapshots

And yes, vxfs snapshot filesystems can be exported.

This will give you almost all the uptime of a hot backup with all the security of a cold backup. And in fact, now that time is of no consequence, you could backup to tape instead.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Can I set a timeout to cp?

If you still want a way to timeout a cp command although snapshots are really the better solution, the best approach is Perl because the way it handles timeouts is exactly the way it's done in C. The attached Perl script (timeoutcp.pl) was quickly adapted from a very similar script.

Use it like this:

timeoutcp.pl -t 5400 file1 file2 file3 /mydestdir/xxx/
STAT=${?}
if [[ ${STAT} -ne 0 ]]
then
echo "Cp failed; status ${STAT}" >&2
fi

Invoke as timeoutcp.pl -u for usage; it does a cp -p but you can alter the system() call to do whatever you like.
If it ain't broke, I can fix that.
Sandman!
Honored Contributor

Re: Can I set a timeout to cp?

Eric,

made modifications to the test script so that it first pings the NFS server && if it's up kicks off the copy. Taking this approach the script aborts when either the cp succeeds or the timeout value is exceeded.

============================================
wait_time=30
timeout=300
timer=0

srcdir=/disc1/patches
destdir=//patches

while true
do
ping && cp -R $srcdir/* $destdir
RC=$?
let timer=$timer + $wait_time
if [[ $RC -eq 0 || $timer -ge $timeout ]]
then
echo "\WARNING: ...!\n"
break
fi
sleep $wait_time
done
============================================

regards
David P. Goodsell
Occasional Visitor

Re: Can I set a timeout to cp?

>while [ $exit_code -eq 1]
>Now I get:
> "A ] character is missing"

you need to put a space between the "1" an "]"

while [ $exit_code -eq 1 ]

hth
bv
"The lyf so short, the craft so long to lerne." - Chaucer
Bob_Vance
Esteemed Contributor

Re: Can I set a timeout to cp?

Ooops, I was logged in as wrong user (David G.)

bv
"The lyf so short, the craft so long to lerne." - Chaucer
Eric Antunes
Honored Contributor

Re: Can I set a timeout to cp?

Thanks for all replies.

Clay,

I'm very interested in your snapshots solution but how can I do it?

"Create a vxfs snapshot of all your Oracle filesystems -- this will take only seconds" -> How can I do this??

Also thanks for your perl script. I'll try to understand it to see what is the better script for me...

Bob,

I've assigned point to your 2º login, thanks because that error is solved.

Now, I get another:

"sh[54]: test: Specify a parameter with this command."

Sandman,

I'll also analyse you script and assign you points accordingly.

Best Regards,

Eric Antun
Each and every day is a good day to learn.
Stephen Keane
Honored Contributor

Re: Can I set a timeout to cp?

If this is your line 54 ...

if [$TIMER ge $TIMEOUT]; then


then you need spaces wither side of the [ and ] and ge should be -ge etc

so it should read

if [ "$TIMER" -ge "$TIMEOUT" ] ; then
A. Clay Stephenson
Acclaimed Contributor

Re: Can I set a timeout to cp?

Vxfs snapshots are very easy.

For example to create a snapshort of /u01/oradata (we will call it /u01/snaporadata), you do this:

mkdir /u01/snaporadata
mount -F vxfs -o snapof=/u01/oradata /dev/vg05/lvol1 /u01/snaporadata

That's it. The mysterious /dev/vg05/lvol1 is an otherwise unused LVOL or disk that is used as the snapshot buffer. It should be sized to about 15% or so of the original filesystem. The "snapshot" does not copy the original filesystem but is a composite of unchanged blocks from the original filesystem plus the original contents of any changed blocks (since the snapshot was started) recorded in the snapshot buffer.

To remove the snapshot all you do is unmount it.
umount /u01/snaporadata
rmdir /u01/snaporadata

Man mount_vxfs for deails. Note: Snapshots require the OnlineJFS product but no HP-UX box should be without OnlineJFS anyway.
If it ain't broke, I can fix that.
Eric Antunes
Honored Contributor

Re: Can I set a timeout to cp?

Hi Clay,

Can you explain me better the phrase "no HP-UX box should be without OnlineJFS anyway."? I may need it to justify this purchase.

Thanks,

Eric Antunes

Each and every day is a good day to learn.
A. Clay Stephenson
Acclaimed Contributor

Re: Can I set a timeout to cp?

The main advantage of OnlineJFS is that it allows online expansion of filesystems. Unless you have OnlineJFS, you would have to unmount the filesystem first and if it's a system filesystem (e.g. /var, /usr, /tmp) you would have to knock the box down to single-user mode. It also allows much better sizing of filesystems because you don't have to deliberately oversize the filesystem initially because expansion is so easy. How many times have you setup systems with one filesystem much larger then needed and another that needs expansion? You can also use OnlineJFS to reduce the size of a filesystem "on the fly" although that was a bit risky until recent versions of VxFS>
If it ain't broke, I can fix that.
Sandman!
Honored Contributor

Re: Can I set a timeout to cp?

Your script should initialize the exit_code variable otherwise the test will fail...

>
> exit_code = 1
> while [ $exit_code -eq 1 ]
>


cheers!
Eric Antunes
Honored Contributor

Re: Can I set a timeout to cp?

Hi all,

I did it with the following script:


WAIT_TIME=30
TIMEOUT=300
TIMER=0

ORIGEM=/disc1/oradata/TST
DESTINO=/_disc2/
exit_code=1

cp -Rp $ORIGEM/* $DESTINO
exit_code=$?

while [ $exit_code -eq 1 ]
do
echo "waiting for backup...\n"
sleep ${WAIT_TIME}
TIMER=`expr $TIMER + ${WAIT_TIME}`
if [ $TIMER -ge $TIMEOUT ]
then
echo "\nWARNING: backup NOT EXECUTED!\n";
exit_code=8;
fi
done

echo "\nSUCCESS! Backup executed!!\n"


PS: Indira please post something since the base script is yours...

Best Regards,

Eric Antunes
Each and every day is a good day to learn.