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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to create folder, copy file, and send email on success
# 1  
Old 10-19-2012
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!
# 2  
Old 10-19-2012
yes that is something that can be scripted.

Though you probably want a few further pointers Smilie

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.
# 3  
Old 10-19-2012
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
# 4  
Old 10-19-2012
Example script:
Code:
 
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


Last edited by rdrtx1; 10-19-2012 at 02:05 PM..
# 5  
Old 10-19-2012
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script to execute Oracle procedure and trigerring email on success and failure

Here is the shell script which need to trigger a stored procedure and when the record count is 0 then it should capture in the log that 0 duplicate records are found and if it's more than 0 then log with no of records. Also if there is any sqlerror then it should write the same in the log file and... (17 Replies)
Discussion started by: senmng
17 Replies

2. Shell Programming and Scripting

How to copy all structure folder create last day?

HI All, please help , i got same case . i want copy folder and directory create yesterday. for sample below : drwxr-xr-x 4 apps apps 33 Nov 23 04:00 xxxxxx@gmai.com drwxr-xr-x 4 apps apps 33 Nov 23 04:00 yyyyyyy@gmail.com drwxr-xr-x 4 apps apps 33 Nov 24 04:00... (2 Replies)
Discussion started by: fajar_3t3
2 Replies

3. Shell Programming and Scripting

Bash for checking and copy Daily files and send an email

Hello, Trying to get a bash for files received on daily basis and want to copy those files to different directory and send an email notification if file exists then send the file counts else Alert with no file received. FileName format: DailyTransaction_2015-03-09_ 200.csv before 200 their is... (1 Reply)
Discussion started by: krux_rap
1 Replies

4. Shell Programming and Scripting

Need help in writitng a script to rename file name and copy to other folder

Hi All, My requirement is as follows: A file (say abc) will be having list of the .txt file names. I need to read this abc file line by line and rename the .txt file names inside it and move them to other folder/path. Eg: abc ------- file1.txt file2.txt file3.txt Output (should... (1 Reply)
Discussion started by: pavan.yadalla
1 Replies

5. Shell Programming and Scripting

Shell script to send an email from the txt file

Hi Friends, Could you guys help me out of this problem... I need to send an email to all the users and the email has to be picked from the text file. text file contains the no. of records like: giridhar 224285 847333 giridhar276@gmail.com ramana 84849 33884 venkata.ramana@gmail.com... (6 Replies)
Discussion started by: giridhar276
6 Replies

6. Shell Programming and Scripting

how to avoid cron job output to send to the junk email folder?

Hi i created a cron job which invoke a shell script and output some content via email. Some times these output are sent to the junk email folder. i want these mails to be sent to inbox with some specific font. what i have to do? (4 Replies)
Discussion started by: vidhyaS
4 Replies

7. UNIX for Advanced & Expert Users

send an email to my Drafts folder

Is there a way I can send a text file directly to my Drafts folder? (1 Reply)
Discussion started by: idontknow
1 Replies

8. Shell Programming and Scripting

Script to send email after comparing the folder permissions to a certain permission & send email

Hello , I am trying to write a unix shell script to compare folder permission to say drwxr-x-wx and then send an email to my id in case the folders don't have the drwxr-x-wx permissions set for them . I have been trying to come up with a script for few days now , pls help me:( (2 Replies)
Discussion started by: nairshar
2 Replies

9. Shell Programming and Scripting

Script to send email if new file is added when folder is updated

Hi, I have a script which updates a repository which is run manually. When this happens either no or new files will be added to a specific folder. If there are new files that have been added, an email needs to be sent. How can i check a folder for any new files after the update repos script has... (0 Replies)
Discussion started by: acc01
0 Replies

10. Shell Programming and Scripting

script to send a file in email

a file is created on a daily basis in the name xyz_pqr_20071207.dat.i want to send the file as an attachment if the file contains more than 50 records.how can i write a script such that it will transmit the file after the file is created.i want to sed the file to say asdf@xyz.com. please help me... (2 Replies)
Discussion started by: dr46014
2 Replies
Login or Register to Ask a Question