Script to create folder, copy file, and send email on success

I am very new to UNIX as well as scripting so please be gentle, haha.

I have a Linux client which has a mount point mapped to my /etc folder on a NetApp FAS system. I woudl like to be able to copy the hosts and rc files once a week from the FAS to a different location for backup.

When the copy happens I would like the script to create a folder based on the current date, then copy the rc and hosts file to that new folder. Once this copy is successful I would like a basic email to be sent to a DL to inform everyone that the copy succeeded.

Is this something that can be scripted?

Thank you!

yes that is something that can be scripted.

Though you probably want a few further pointers :wink:

You can use the cp command to do the copying, the return value of the previous command is $? so yu can test for success by checking that value, the sendmail binary can be used to send a mail from the command line with the -t flag (this may need adaption depending onb your site setup), and the whole lot can be scheduled with cron .

Thanks for that!

So far this is what i have for the copying process at least:

mkdir /mnt/backup/$(date +%F)
#(create dated folder on destination [linux VM])

cp /mnt/FAS/etc/hosts /mnt/backup/$(date +%F)
#(copy from my Netapp to the local VM file structure)

So i guess this is where im stuck. How can i validate that this copy was in fact successful? I would like to get an email with a "Success" or "Failure" and the name of the NetApp (as there will be many that this will be run separately on) So if for some reason NetApp15 failed during the copy, we will be informed and will be able to investigate the issue.

I hope some of that makes sense, haha

Example script:

 
success=1
if [ ! -d /mnt/backup/$(date +%F) ]
then
  mkdir /mnt/backup/$(date +%F) || success=0
  #(create dated folder on destination [linux VM])
fi
if [ $success = 1 ]
then
  cp /mnt/FAS/etc/hosts /mnt/backup/$(date +%F) || success=0
  #(copy from my Netapp to the local VM file structure)
fi
if [ $success = 1 ]
then
  echo "copy success" | mailx -s "File copy success" minnino@minnino.server.com
else
  echo "copy failed" | mailx -s "File copy failed" minnino@minnino.server.com
fi

Thanks rdrtx1, i ran that script and got the following output. The only thing i changed at all was the paths, and did an 'echo' in front of each line so i could see it run...

if [ ! -d /mnt/fas11/backup/2012-10-19 ]

then

mkdir /mnt/fas11/backup/2012-10-19
fi
= 1 ]

then

cp /mnt/fas11/vol0/etc/hosts /mnt/fas11/backup/2012-10-19

fi

= 1 ]

then

contains invalid character '\015'

Send options without primary recipient specified.

Usage: mailx [-BDFintv~] [-s subject] [-a attachment ] [-c cc-addr] [-b bcc-addr]
[-r from-addr] [-h hops] [-A account] [-R reply-addr] to-addr ...
mailx [-BDeHiInNRv~] [-T name] [-A account] -f [name]
mailx [-BDeinNRv~] [-A account] [-u user]

else
contains invalid character '\015'

Send options without primary recipient specified.
Usage: mailx [-BDFintv~] [-s subject] [-a attachment ] [-c cc-addr] [-b bcc-addr]
[-r from-addr] [-h hops] [-A account] [-R reply-addr] to-addr ...
mailx [-BDeHiInNRv~] [-T name] [-A account] -f [name]
mailx [-BDeinNRv~] [-A account] [-u user]

fi

CDCSMBRAL04:~/Desktop #

I also checked in my /mnt/fas11/backup folder and did not see a dated folder created.