LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-05-2010, 12:20 AM   #1
leejohnli
Member
 
Registered: Sep 2010
Posts: 66

Rep: Reputation: 2
Script to copy file remotely


Guys,
Any sample script that will copy file to remote server.I'm doing it on a manual process using SCP parameter. Tnx
 
Old 10-05-2010, 12:54 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
well just put your scp command in a script, and it's "automatic". You've not given any information about what you want to automate about this. Only general improvement would be to use an ssh preshared key to bypass the need for manual password entry.
 
Old 10-05-2010, 01:14 AM   #3
ionrivera
Member
 
Registered: May 2010
Distribution: Debian, RHEL, AIX, Solaris, HP-UX
Posts: 61

Rep: Reputation: 4
When copying files remotely (It also acts as my Off-site Backup Mirror), I use rsync and ssh to accomplish the task...

1. Add ssh keys to automate login without password prompt (there are lots of tutorial out there... google)

2. In my bash script, rsync command is:
Pull syntax:
rsync -avz -e "$SSH" --delete $USER@$HOST:/SRC/ /DEST/

Push syntax:
rsync -avz -e "$SSH" --delete /SRC/ $USER@$HOST:/DEST/


3. Make a script
# vi sync.sh

my script goes like this (you can copy & paste):

Code:
#!/bin/sh
#

RSYNC=$(which rsync)
SSH=$(which ssh)
RUSER=root                
HOST=192.168.0.10    
SRC=/shares/PUBLIC/       
DEST=/shares/BACKUP/      
PID=/var/run/sync.pid

start() {
        if [ ! -e "$PID" ];then
                touch $PID
                echo "Synchronizing files"
                $RSYNC -avz -e "$SSH" --delete $RUSER@$HOST:$SRC $DEST                              
                rm -f $PID
        fi
}

stop() {
        if [ -e "$PID" ];then
                killall rsync
                echo "Stopping filesynching"
                rm -f $PID
        fi
}
restart() {
    stop
    start
}

case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        restart)
                restart
                ;;
        *)
                echo $"Usage: $0 {start|stop|restart}"
                exit 1
esac

exit $?
4. Dont forget to make your script executable and test it.
# chmod +x sync.sh
# ./sync.sh start

5. If you want to create cron, su as root and:
# crontab -e

and insert the ff:
# m h dom mon dow command
30 17 * * 1-5 /root/scripts/sync.sh start
30 7 * * 1-5 /root/scripts/sync.sh stop

Thats it! Hope that helps...

Last edited by ionrivera; 10-07-2010 at 03:05 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
write script to copy a file kavehiks Linux - General 5 10-21-2009 03:50 PM
how to copy file remotely? bkcreddy17 Programming 5 09-18-2008 07:45 AM
can't find zm script file to copy. Buddhike G Linux - Software 1 10-08-2007 05:17 PM
Copy file between servers from script gr0undz3r0 Linux - Newbie 2 11-13-2006 11:48 AM
Copy file by script python ! BongHongXanh Programming 2 01-11-2006 08:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration