Remote Copy Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remote Copy Script
# 1  
Old 04-12-2011
Remote Copy Script

Hi All,

My first post on the Forums! I've just whipped up a quick script to slightly automate a log file retrieval process for work and to be completely honest my BASH scirpting knownledge is extremely limited as is my understanding of a lot of linux commands.

Basically the scripts purpose is to remove the hassle of having to type in several scp commands to retrieve logs from numerous boxes.

Fair warning the script could be completely laughable but this is why I'm asking for critique. Another massive no no is not having actually tested the script (this is why I'm asking the community to point out any majorily flawed coding).

Thank you in advance!

Code:
#!/bin/bash

# This bash script is used for the slightly automated process of exporting log files from the XXXXXXX Servers
# Version: 0.1
# Last Updated: 12/04/2011

ECHO THIS BASH SCRIPT IS TO BE USED FOR THE EXPORTATION OF ALL SERVER LOGS TO A USB
ECHO THE FOLLOWING CONDITIONS MUST HAVE BEEN COMPLETED BEFORE RUNNING THIS SCRIPT....
ECHO 1. YOU MUST BE LOGGED IN ON THE ROOT ACCOUNT OF XXXXX SERVER SERVER01. THIS SCRIPT WILL NOT WORK ON THE OTHER SERVERS!
ECHO 2. THE USB DRIVE MUST BE INSERTED AND MOUNTED DIRECTLY TO /mnt/ WITHOUT A FOLDER NAME PRIOR TO RUNNING THIS SCRIPT
ECHO If the drive is not mounted please type the following; mount -t vfat /dev/sdb1 /mnt/ (kill this script before attempting to run command)
ECHO NOTE: sdb1 may NOT be the correct dev for the USB refer to notes if an error occurs

$ read -p "If the above has been completed press any key....if it hasn't please exit the script"

ECHO Now creating directories on /mnt/

# This first command will first create a current date directory on the USB Drive
mkdir /mnt/`date +%Y%m%d`/

# The following commands will then create all relevant server folders within the current date of this backup process taking place
mkdir /mnt/`date +%Y%m%d`/SERVER01
mkdir /mnt/`date +%Y%m%d`/SERVER02
mkdir /mnt/`date +%Y%m%d`/SERVER03
mkdir /mnt/`date +%Y%m%d`/SERVER04
mkdir /mnt/`date +%Y%m%d`/SERVER05
mkdir /mnt/`date +%Y%m%d`/SERVER06
mkdir /mnt/`date +%Y%m%d`/SERVER07
mkdir /mnt/`date +%Y%m%d`/SERVER08
mkdir /mnt/`date +%Y%m%d`/SERVERR01
mkdir /mnt/`date +%Y%m%d`/SERVERR02
mkdir /mnt/`date +%Y%m%d`/SERVERR03

$ read -p "The directories have been created on the USB, press any key to start the copy process...."

# This first command is only using cp as you are already on the SERVER01 box and don't need to use scp (for remote copy)
cp /var/log/* /mnt/`date +%Y%m%d`/SERVER01

# The following commands will now execute Secure Remote Copy using the root account of all servers. It will prompt for the password for each instance
# We are not using an RSA Public Key and therefore cannot avoid the need to enter in the password for each server instance
scp root@xxx.xxx.xxx.xxx:/var/log/* /mnt/`date +%Y%m%d`/SERVER02
scp root@xxx.xxx.xxx.xxx:/var/log/* /mnt/`date +%Y%m%d`/SERVER03
scp root@xxx.xxx.xxx.xxx:/var/log/* /mnt/`date +%Y%m%d`/SERVER04
scp root@xxx.xxx.xxx.xxx:/var/log/* /mnt/`date +%Y%m%d`/SERVER05
scp root@xxx.xxx.xxx.xxx:/var/log/* /mnt/`date +%Y%m%d`/SERVER06
scp root@xxx.xxx.xxx.xxx:/var/log/* /mnt/`date +%Y%m%d`/SERVER07
scp root@xxx.xxx.xxx.xxx:/var/log/* /mnt/`date +%Y%m%d`/SERVER08
scp root@xxx.xxx.xxx.xxx:/var/log/* /mnt/`date +%Y%m%d`/SERVERR01
scp root@xxx.xxx.xxx.xxx:/var/log/* /mnt/`date +%Y%m%d`/SERVERR02
scp root@xxx.xxx.xxx.xxx:/var/log/* /mnt/`date +%Y%m%d`/SERVERR03

$ read -p "The copying process is now completed, press any key to unmount the usb drive......"

# The change of directory is to ensure the drive can be unmounted (if the user ran the bash script within the /mnt/ directory itself 
cd $home

# Command to unmount the USB Drive
umount /mnt/

#The USB drive has been unmounted and can be removed
$ read -p "You can now remove the USB key....press any key to exit the script"
exit

Things i'm not sure on;

- Using the read command purely as a pause process, will it work the way I intend it too?
- Will the individual scp commands process, prompt for password, process the transfer and then move onto the next scp command?
- If the script is run from within the USB directory will the cd and unmounting run into problems?
# 2  
Old 04-12-2011
A few things:
  • Your uppercase ECHO won't work. Commands in Unix are case sensitive, and the ECHO command just isn't known.
  • The dollar sign in front of the read command will throw an error. What's the intent behind that?
  • read won't wait for any key, but only continue to run on newline (eg. Enter)
  • Instead of backticks (`cmd`) start using $(cmd), as the former is considered deprecated.
  • A cd / will always succeed, so you don't have to rely on the $home variable being set
  • Instead of repeatedly calling date, why not just call it once, save the result in a variable, and use that from thereon?
  • And of course the script could be made shorter by using loops, but that's more or less optical for such a short script.
  • If you want to do a quick check of the syntax run bash -n script, which will do a syntax check but not run the script. Once it does that, you can run bash -x script to see a trace of what is done

Last edited by pludi; 04-12-2011 at 11:16 AM..
This User Gave Thanks to pludi For This Post:
# 3  
Old 04-12-2011
Hi pludi,

Thanks for your input, I've made adjustments to the script as suggested. I'm going to learn about looping to see if I can optimise the script a bit more.

The $ behind read was from what I found on a website discussing it's purpose, I did think it was a bit strange.

I'll run the checks now, if I've made a mistake, I'd appreciate it being pointed out!

Cheers,
effektd

Code:
#!/bin/bash

# This bash script is used for the slightly automated process of exporting log files from the XXXXXXX Servers
# Version: 0.2
# Last Updated: 13/04/2011

# Date variable
BKDATE=$(date +%Y%m%d)

echo THIS BASH SCRIPT IS TO BE USED FOR THE EXPORTATION OF ALL SERVER LOGS TO A USB
echo THE FOLLOWING CONDITIONS MUST HAVE BEEN COMPLETED BEFORE RUNNING THIS SCRIPT....
echo 1. YOU MUST BE LOGGED IN ON THE ROOT ACCOUNT OF XXXXX SERVER SERVER01. THIS SCRIPT WILL NOT WORK ON THE OTHER SERVERS!
echo 2. THE USB DRIVE MUST BE INSERTED AND MOUNTED DIRECTLY TO /mnt/ WITHOUT A FOLDER NAME PRIOR TO RUNNING THIS SCRIPT
echo If the drive is not mounted please type the following; mount -t vfat /dev/sdb1 /mnt/ (kill this script before attempting to run command)
echo NOTE: sdb1 may NOT be the correct dev for the USB refer to notes if an error occurs

read -p "If the above has been completed press ENTER to continue....if it hasn't please exit the script"

echo Now creating directories on /mnt/

# This first command will first create a current date directory on the USB Drive
mkdir /mnt/$BKDATE/

# The following commands will then create all relevant server folders within the current date of this backup process taking place
mkdir /mnt/$BKDATE/SERVER01
mkdir /mnt/$BKDATE/SERVER02
mkdir /mnt/$BKDATE/SERVER03
mkdir /mnt/$BKDATE/SERVER04
mkdir /mnt/$BKDATE/SERVER05
mkdir /mnt/$BKDATE/SERVER06
mkdir /mnt/$BKDATE/SERVER07
mkdir /mnt/$BKDATE/SERVER08
mkdir /mnt/$BKDATE/SERVERR01
mkdir /mnt/$BKDATE/SERVERR02
mkdir /mnt/$BKDATE/SERVERR03

read -p "The directories have been created on the USB, press ENTER to being the copy process..."

# This first command is only using cp as you are already on the SERVER01 box and don't need to use scp (for remote copy)
cp /var/log/* /mnt/$BKDATE/SERVER01

# The following commands will now execute Secure Remote Copy using the root account of all servers. It will prompt for the password for each instance
# We are not using an RSA Public Key and therefore cannot avoid the need to enter in the password for each server instance
scp root@xxx.xxx.xxx.xxx:/var/log/* /mnt/$BKDATE/SERVER02
scp root@xxx.xxx.xxx.xxx:/var/log/* /mnt/$BKDATE/SERVER03
scp root@xxx.xxx.xxx.xxx:/var/log/* /mnt/$BKDATE/SERVER04
scp root@xxx.xxx.xxx.xxx:/var/log/* /mnt/$BKDATE/SERVER05
scp root@xxx.xxx.xxx.xxx:/var/log/* /mnt/$BKDATE/SERVER06
scp root@xxx.xxx.xxx.xxx:/var/log/* /mnt/$BKDATE/SERVER07
scp root@xxx.xxx.xxx.xxx:/var/log/* /mnt/$BKDATE/SERVER08
scp root@xxx.xxx.xxx.xxx:/var/log/* /mnt/$BKDATE/SERVERR01
scp root@xxx.xxx.xxx.xxx:/var/log/* /mnt/$BKDATE/SERVERR02
scp root@xxx.xxx.xxx.xxx:/var/log/* /mnt/$BKDATE/SERVERR03

read -p "The copying process is now completed, press ENETER to unmount the USB...."

# The change of directory is to ensure the drive can be unmounted (if the user ran the bash script within the /mnt/ directory itself 
cd /

# Command to unmount the USB Drive
umount /mnt/

#The USB drive has been unmounted and can be removed
read -p "You can now remove the USB key....press ENTER to exit the script"
exit

# 4  
Old 04-13-2011
One thing I missed before: you should quote anything that you echo, since the text may contain special characters that the shell might try to interpret (for example the braces on line 14).

An (slightly advanced) example for a loop that you could use:
Code:
#!/usr/bin/bash

# Read the script, skip all lines up to #__DATA__, and remove the leading hash
# from the remaining lines
SERVERS=$( sed -e '1,/^#__DATA__/d; s/^#//' $0 )

# Echo the data and feed it into a loop reading each line. The line is split on
# the colon, and the data saved into 2 variables
echo "$SERVERS" | while IFS=':' read name ip
do
    echo "Server \"$name\" has the IP $ip"
done

# The servers are specified here. Since everything is quoted this won't be
# executed

#__DATA__
#SERVER01:1.2.3.4
#SERVER02:5.6.7.8
#SERVER03:9.8.7.6

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy from local to remote

Hi I need a advice for writing simple bash script, I have a file pod.txt which contains source location and remote location: /mnt/infile/20141103/701_0001.png/remote/tmp/pk21730/p0330223723074.png /mnt/infile/20141103/203_0001.png/remote/tmp/pk21731/p0330223723081.png and I must copy ... (6 Replies)
Discussion started by: primo102
6 Replies

2. UNIX for Dummies Questions & Answers

Remote copy through shellscript

I am using RedHat 4.4 Version Operating System. I want to copy one file from source to destination.I don't want to give a password when I am executing the below shell script. code: #!/bin/bash scp -r file.zip 192.168.1.5:/root/ Here it asking a password to enter,Is there any way give a... (2 Replies)
Discussion started by: kannansoft1985
2 Replies

3. Shell Programming and Scripting

Copy a file from local host to a list of remote hosts --- perl script

Hi friends, i need to prepare a script ( in perl) i have a file called "demo.exe" in my local unix host. i have a list of remote hosts in a file "hosts.txt" now i need to push "demo.exe" file to all the hosts in "hosts.txt" file. for this i need to prepare a script(in perl, but shell... (5 Replies)
Discussion started by: siva kumar
5 Replies

4. Shell Programming and Scripting

Copy and run that script in Remote server

Hi All, I need script to perform below task. 1. I have a script in one server and need to copy this script to remote server 2. login in to remote server 3. run the script which i copied to this server. #!/bin/bash read a scp /tmp/script.sh user@hostname:/tmp ssh user@$a ./scirpt.sh ... (2 Replies)
Discussion started by: bapu1981
2 Replies

5. UNIX for Advanced & Expert Users

Using remote rsync, but copy locally?

I'm looking to use rsync to compare remote files and to copy the diff to a local directory, rather than transfer over the net. The net connection is not fast enough to transfer these files (~1.8TB) and I'd like to sneakernet them instead. Possible? (4 Replies)
Discussion started by: dfbills
4 Replies

6. Shell Programming and Scripting

Confirming Remote Copy

Hey guys. I'm knocking up a script, a part of which will be copying some pretty large files to some remote servers. With the size of the files I need to make sure that they don't lose any data / corrupt on the way (it's unlikely, but these are business critical). Obviously I could use the... (2 Replies)
Discussion started by: dlam
2 Replies

7. 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

8. Shell Programming and Scripting

copy file from script file to remote computer

hi, i want copy one or group of file from a computer to others, but i have some problem that do not allow me to do this. i do this by scp command like this : scp <file name> root@cpName:destinationAddress but the problem is that it do not it automatically. it means when it is connecting to... (4 Replies)
Discussion started by: MShirzadi
4 Replies

9. HP-UX

help me to copy remote file

I want to copy dump generated from oracle database to my local DAT drive. Currently I am copying remote file to local drive thru rcp command and later copy it to local DAT. Pls. help me in this. Thanks Man Mohan email address removed (8 Replies)
Discussion started by: manmohan73
8 Replies

10. UNIX for Dummies Questions & Answers

Remote file copy

I facing a problem with Unix command "rcp". I unable to perform a rcp between host machines. I have religiously followed the man pages, but still unable to solve the problem. Do i check for anything to perform this command? Pls help....thanks =) (3 Replies)
Discussion started by: lchunl
3 Replies
Login or Register to Ask a Question