Script to overwrite & before that keep copy a file on many servers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to overwrite & before that keep copy a file on many servers
# 1  
Old 10-12-2013
Script to overwrite & before that keep copy a file on many servers

I have ssh password less auth enable & script does the job well as well
Code:
#/bin/bash
for i in `cat ip`
do
scp /etc/resolv.conf root@$ip
done


But I need to take backup of the file i will overwrite .. is there any simple way ?

Kindly respond

Last edited by Scrutinizer; 10-12-2013 at 08:50 PM.. Reason: code tags
# 2  
Old 10-12-2013
Code:
#/bin/bash
for i in `cat ip`
do
ssh $ip 'tar cvf /path/to/archive/resolve.tar  /etc/resolve.conf'
scp /etc/resolv.conf root@$ip
done

[/path/to/archive] example directory - has to exist

Allowing root to ssh into a box is considered a security issue. FWIW....
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 10-13-2013
Code:
#!/bin/bash
file=/etc/resolve.conf
for i in `cat ip`
do
<$file ssh root@$ip "
cp -p $file $file.bak && cat >$file
"
done


Last edited by MadeInGermany; 10-13-2013 at 12:26 AM..
This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 10-13-2013
Thanks Guys its fixed... silly mistake i did .. fix is simple
Code:
#/bin/bash
for i in `cat ip`
do
ssh $ip mv /etc/resolv.conf /root/backup/resolv.conf.`date +"%m-%d-%y-%T"`
scp /etc/resolv.conf root@$ip
done


Last edited by Scrutinizer; 10-13-2013 at 03:05 AM.. Reason: code tags
# 5  
Old 10-13-2013
Do not use mvfor this!
A safe backup is cp -p.
1. It retains the inode: leaves links, owner,group,permissions intact.
And 2. does not risk a malfunction, e.g. your second access could fail because the accessing host cannot be determined.
For reason 1 the file should be overwritten with cp; your scp is safe.
# 6  
Old 10-13-2013
In addition, cp -p is also not entirely safe unless you test its outcome and make that determine whether the scp can proceed or not...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare file size and then copy/overwrite

// Redhat I have this code working, but need to add one more qualification so that I don't overwrite the files. #!/bin/sh cd /P2/log/cerner_prod/millennium/archive/ for f in * do || continue #If this isn't a regular file, skip it. && continue #If a backup already... (2 Replies)
Discussion started by: Daniel Gate
2 Replies

2. Shell Programming and Scripting

Need help with script to copy code to multiple servers

Hi, I am new to scripting and i am trying to use below script to copy code to multiple servers and multiple locations on each server. the script is not working or doesnt give any error. Any help is appreciated. basically i want a script to get the code from a location (dir below) and read the... (2 Replies)
Discussion started by: Ron0612
2 Replies

3. Shell Programming and Scripting

Shell script to overwrite a file

Hi Guys, My requirement as follows, i want to write a shell script to display the files of a folder, i export it to a file to mail the file. The problem is the exported file is getting appended every time I run the script. I just want the file to be over written. can anyone suggest?? ... (4 Replies)
Discussion started by: Karthick N
4 Replies

4. Shell Programming and Scripting

awk search/replace specific field, using variables for regexp & subsitution then overwrite file

Hello, I'm trying the solve the following problem. I have a file which I intend to use as a csv called master.csv The columns are separated by commas. I want to change the text on a specific row in either column 3,4,5 or 6 from xxx to yyy depending upon if column 1 matches a specified pattern.... (3 Replies)
Discussion started by: cyphex
3 Replies

5. Shell Programming and Scripting

shell script to take input from a text file and perform check on each servers and copy files

HI all, I want to script where all the server names will be in a text file like server1 server2 server3 . and the script should take servernames from a text file and perform copy of files if the files are not present on those servers.after which it should take next servername till the end of... (0 Replies)
Discussion started by: joseph.dmello
0 Replies

6. Shell Programming and Scripting

Copy a file on remote servers

Hey Unix Gurus, I'm having trouble in copying a file on 5 different servers, first how can you do it locally (i.e without the need to ssh to the server you want to copy the file) and if you need to ssh how do u run a command within that server. Please see my code below(it doesn't work somehow).... (10 Replies)
Discussion started by: sexyTrojan
10 Replies

7. Shell Programming and Scripting

How do I search first&second string & copy all content between them to other file?

Hi All, How do I search first string & second string and copy all content between them from one file to another file? Please help me.. Thanks In Advance. Regards, Pankaj (12 Replies)
Discussion started by: pankajp
12 Replies

8. Shell Programming and Scripting

Overwrite & Delete in Text File

Dear All, I have text file like this: Header Record 1 Record 2 ....... Record n Tail This line of code : awk '{ if ( NR == 1 ) { head=substr($0,1,300);} else { last = substr($0,1,300);}END{printf "Header is : %-300s Trailer is : %-300s\n", head, last}' filename converted Header... (11 Replies)
Discussion started by: 33junaid
11 Replies

9. Shell Programming and Scripting

File Compare & Move between 2 servers

Greetings - I am a newbie in shell scripts. I have been thru the whole forum but there has been no similar query posed. The objective of my system is to have a unified filebase system. I am using RSync to synchronise files between the location & central server with both of them having the... (4 Replies)
Discussion started by: evolve
4 Replies

10. Shell Programming and Scripting

a script to clone a dir tree, & overwrite the dir struct elsewhere?

hi all, i'm looking for a bash or tcsh script that will clone an empty dir tree 'over' another tree ... specifically, i'd like to: (1) specify a src directory (2) list the directory tree/hiearchy beneath that src dir, w/o files -- just the dirs (3) clone that same, empty dir hierarchy to... (2 Replies)
Discussion started by: OpenMacNews
2 Replies
Login or Register to Ask a Question