Solved

Possible to Error Check 'Find -exec cp' command?

Posted on 2013-02-05
7
380 Views
Last Modified: 2013-02-07
Is it possible to error check the results of the -exec option when used with the find command?

for example, running:
find -mmin -10 -exec cp {} /somefolder \;-

Open in new window

in a folder that has multiple files that are less than 10 min and the '/somefolder' does not exist errors out for each file found with "no such file or directory exists"

if I error check with echo $? I receive 0 which is due to the find command being succesfull.  The -exec command however is not and this is what I would like to error check for.

Anyway of doing this with out offloading to another script?
0
Comment
Question by:jwmcf1
  • 4
  • 3
7 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 38857506
Why don't you do something like

[[ -d /somefolder ]] && find -mmin -10 -exec cp {} /somefolder \;

Open in new window


This would make sure that find is only executed when the folder exists
0
 

Author Comment

by:jwmcf1
ID: 38857567
Thank you for the recomendation.  However, this line of code is just a small part of a script.

Really what I am attempting to do is prevent the deletion of a file if the copy of a file errors out for any reason.

basically the script is something like:

#first find the newest files and then copy them
find -mtime -2 -exec cp {} /somefolder \;
if [ "$?" = "0" ]
then 
#delete the current oldest file which in this case is +30 days
find -mtime +30 -exec rm {} \;
else
echo "error during copy of file.  stop script and prevent any deletions until fixed blah bl.."
fi

Open in new window


ultimate goal is to maintain the most recent 30 days worth of files and then copy these files to another location for backup and after backup delete the oldest file.  But before I delete anything I want to make sure the copy has actually happend.
0
 
LVL 31

Accepted Solution

by:
farzanj earned 500 total points
ID: 38857905
From your script, first your find command looks wrong, because you need to mention a path as well.  So I suppose the path is current directory, so I will put dot. You can put any path instead of dot
Try this:

files="$(find . -mtime -2)"
for file in "$files"
do
    cp $file /somefolder
    if [[ $? == 0 ]]
    then
           ... other find command
    fi
done

Open in new window

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 31

Expert Comment

by:farzanj
ID: 38857909
Your logic is still a little fuzzy.  Which file to check and which one to delete?  Any thing in the name or how would I tell?
0
 

Author Comment

by:jwmcf1
ID: 38860448
I will try testing using a for loop as you have shown.

As far as logic, I copy the most recent file and the cp command is being checked for error and if no error from the copy;  file older than 30 days is being deleted.

One file copied, one file deleted.  Idea is to always have the last 30 days worth of files in the local path and while also having an archive (the daily copy) at another location (remote server).
0
 
LVL 31

Expert Comment

by:farzanj
ID: 38860756
I understand that, but is the criteria only about timestamp?  Are there categories of files?  Like abc_old and abc_new_date and xyz_old and xyz_new_date, are you have to get the latest in the each category.  This would have been more realistic.  

If this is the only criteria, then you can have
ls -lsr | tail -1 | awk '{print $NF}'  gets you the lastest file and if you remove option r from ls, you get the oldest.
0
 

Author Comment

by:jwmcf1
ID: 38864818
The for loop works to accomplish this task.  Thank you for the help.

this code does what i am looking for
#!/bin/bash

logfile=/usr/local/bin/exportlog.txt
addresses=somebody@email.com
todaysdate=$(date +"%Y-%m")

echo $todaydate
date > $logfile
echo " " >> $logfile 2>&1

umount /mnt/backup >> $logfile 2>&1
mount -t cifs -o username=someuser,password=somepass //backupserver/E /mnt/backup >> $logfile 2>&1
mkdir /mnt/backup/exporttest/$(hostname)/$todaysdate >> $logfile 2>&1

echo $(date)
echo "Starting Export Backup" >> $logfile 2>&1
echo " " >> $logfile 2>&1

echo "Copying..." >> $logfile 2>&1
cd /data/
files="$(find -mtime -1)"
for file in "$files"
do
   cp --parents --preserve -a $file /mnt/backup/exporttest/$(hostname)/$todaysdate >> $logfile 2>&1
   if [[ $? == 0 ]]
   then
    find . -mtime +30 -exec rm {} \; >> $logfile 2>&1
   else
    cat /usr/local/bin/exportlog.txt | mail -s "EXPORT COPY ERROR" $addresses
   fi
done

cd /
sleep 1m
umount /mnt/backup

echo "Finished Exports Backup."
echo $(date)

Open in new window

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now