Need help setting up script

Hello all, I am trying to setup a script that will copy any files older than 30 days in four directories to my windows box, then delete any files older than 30days in the four directories, verify that they are deleted, then send me a confirmation email.

I would like to set this up as a cron job to run once a week on Sunday at 9pm.

So far I have this for the script:

find /data/folder1/ -type f -mtime +30 | xargs rm -f
find /data/folder2/ -type f -mtime +30 | xargs rm -f
find /data/folder3/ -type f -mtime +30 | xargs rm -f
find /data/folder4/ -type f -mtime +30 | xargs rm -f

That is the portion for deleting the files, but I am not sure about copying the files first to my windows box, verifying they are deleted, then sending an email.

The box is solaris 8. I don't have much experience with Unix so I appreciate the help.

gather your commands

find /data/folder[1-4] -type f -mtime +30 | xargs rm -f

Hi ctsgnb, thank you for the response. Actually they all have different names, I just labeled them as newfolder 1 etc.

Do you have any suggestions on:

Copying files older than 30 days to a windows box
Verifying that the script completed
Sending a confirmation email.

Thanks and I appreciate your time.

The below script will do your work.

#!/bin/ksh
EMAIL=""  ## To whom the list goes after completion
WINDOWS_SHARE="" ## Windows share where the files will be copied
for LOOK_DIR in folder1 folder2 folder3 folder4
do
for FILE in `find ${LOOK_DIR} -type f -mtime +30 -print`
do
echo ${FILE} >> file_list ## This file will have the list of files copied and removed
cp ${FILE} ${WINDOWS_SHARE} ## Copies your file to windows share
rm ${FILE}   ## Deletes the file
done
done
cat file_list | mailx -s "list of files copied and deleted" ${EMAIL}

You can customise the mailx command as you want.

Remember to put the email address and windows share mounted on your server inside the quotes for their variable.

Regards,
Vishal

Awesome, thank you for the help!!

To edit my variables, does this sound about right?

#!/bin/ksh
EMAIL="user1@emailaddress.com, user2@emailaddress.com"  ## To whom the list goes after completion
WINDOWS_SHARE="\\windowsbox\file_backup" ## Windows share where the files will be copied
for LOOK_DIR in /data/folder1 /data/folder2 /data/folder3 /data/folder4
do
for FILE in `find ${LOOK_DIR} -type f -mtime +30 -print`
do
echo ${FILE} >> file_list ## This file will have the list of files copied and removed
cp ${FILE} ${WINDOWS_SHARE} ## Copies your file to windows share
rm ${FILE}   ## Deletes the file
done
done
cat file_list | mailx -s "Email Notification for Unix Server. Removal of old files" ${EMAIL}

Also to confirm, do I save this as a .sh or .ksh ?

Once again, thank you for your help!

Your windows share doesn't appears correct, it has to be NFS mounted on your server, which I think needs some configuration on windows box, to enable NFS mounting of the share on Unix.

You can save it with any name you want, since we have specified which shell to use for executing [check the first line], but the best way to keep is .ksh as it will be executed on korn shell.

Regards,
Vishal

Sorry to bump this thread. Would there be any way to tar the files before copying them to a windows share?

Also, what would be the easiest way to set this up as a cron job?

All the help is greatly appreciated.

TAR is not going to do any good until you are going to zip them which will reduce their size.

You might want to tar all the files together once they are moved and then you can zip the tar.

In order to set the cron, put the commands in the script and then you can schedule it for the time you want it to execute.

Regards,
Vishal

Thanks again for the help Vishal. I will get a NFS mount setup on the windows box. Thanks for the info on TAR. I will just zip them once they are moved.

In the script, this line specifically:

for LOOK_DIR in /data/folder1 /data/folder2 /data/folder3 /data/folder4

The four folders are in /data/, so by spacing them out, I did that correctly right? If I would want to add to them, I just space and then add another directory?

Appreciate the help.

#!/bin/ksh
EMAIL="user1@emailaddress.com, user2@emailaddress.com"  ## To whom the list goes after completion
WINDOWS_SHARE="\\windowsbox\file_backup" ## Windows share where the files will be copied
for LOOK_DIR in /data/folder1 /data/folder2 /data/folder3 /data/folder4
do
for FILE in `find ${LOOK_DIR} -type f -mtime +30 -print`
do
echo ${FILE} >> file_list ## This file will have the list of files copied and removed
cp ${FILE} ${WINDOWS_SHARE} ## Copies your file to windows share
rm ${FILE}   ## Deletes the file
done
done
cat file_list | mailx -s "Email Notification for Unix Server. Removal of old files" ${EMAIL}

UNIX doesn't automagically understand \\servername\ style addresses. You have to mount that on your server somewhere. Unfortunately I'm not sure how to do this on Solaris.

You're feeding the temp file into mailx with a useless use of cat.

Your for-loops are redundant. You can stuff all four folders into one find.

Putting find into backticks like that is dangerous and unnecessary. They call this a useless use of backticks.

-print is redundant when you have no other output options. find always prints, then.

Re-opening file_list with >>file_list every loop is redundant, just save it once and be done with it by putting the redirection outside the loop.

You're not doing any error-checking at all. It will delete the file even if it fails to copy it to the share!

#!/bin/ksh
EMAIL="user1@emailaddress.com, user2@emailaddress.com"  ## To whom the list goes after completion
WINDOWS_SHARE="/path/to/mountpoint" ## Windows share where the files will be copied

# Save all error messages to a logfile
exec 2>errorlog

# Feed the list of files directly into xargs.
# echo a b c | xargs touch is equivalent to touch a b c
#
# We use tar -rf so that, when tar gets run more than once, it 
# appends to the existing .tar instead of overwriting it!
find /data/folder1 /data/folder2 /data/folder3 /data/folder4 -type f -mtime +30 | xargs -I {} tar -rf "${WINDOWS_SHARE}/file.tar"

# Read the list of files back from file.tar, to verify they're there before deleting them.
# Save a copy of the list in files_list.
tar -tf "${WINDOWS_SHARE}/file.tar" | tee files_list | xargs -I {} rm

# One of the rare useful uses of cat on a single file
( cat files_list ; echo "Errors:" ; cat errlog ) | mailx -s "Email Notification for Unix Server. Removal of old files"

The computer doesn't care, only the #!/bin/ksh is important. The extension .ksh is just to tell programmers it's a ksh script and not some other kind of script when they see the filename.

All you need is to configure simple shared folder on Win box and mount it as SMB/CIFS on Solaris server (mount process can be part of backup script). I'm sure that there is much more possibilities how to get it working (FTP, ssh/scp server for Win etc).
Not sure how you can trust in stability and reliability NFS with cooperation with Win, but better to avoid.