LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Copy folders from windows machine to linux script (https://www.linuxquestions.org/questions/linux-newbie-8/copy-folders-from-windows-machine-to-linux-script-836687/)

lyschange 10-07-2010 02:19 AM

Copy folders from windows machine to linux script
 
HI all, im looking for a script to run that will be run from my linux box to copy folders from a windows server at a certain time on a dailly basis.

ionrivera 10-07-2010 02:40 AM

Copy files and folders from windows machine to your linux box using rsync and ssh
 
When copying files remotely even on windows, I use rsync and ssh to accomplish the task...

## PREREQUISITE: Install open-ssh on your windows server ##

1. On your Windows server and Linux box, add ssh keys to automate login without prompting for a password (there are lots of tutorial out there... just google)

2. On your Linux box, make a script (/root/sync.sh)
Code:

sudo -i vi sync.sh
my script goes like this (you can copy & paste):

Code:

#!/bin/sh
#

RSYNC=$(which rsync)
SSH=$(which ssh)
RUSER=root                # Supply your windows server user here
WINSERVER=192.168.0.10    # Change it with your windows server IP or Hostname
SRC=/shares/PUBLIC/      # Your windows shared folder via openssh-windows
DEST=/shares/BACKUP/      # Your linux box destination folder
PID=/var/run/sync.pid

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

## Note: If you want to have an identical copy of files on both your windows shared folder and linux backup (acts as a mirror), you would need a "--delete" flag in your rsync routine. ## 

                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 $?

3. Dont forget to make your script executable and test it.
Code:

chmod +x sync.sh
 ./sync.sh start

4. If you want to run the script e.g. daily that starts copying/syncing every 5:30PM and stops copying/syncing every 7:30AM, su as root on your Linux Box and:
Code:

crontab -e
and insert the ff:
Code:

# m h  dom mon dow  command
30 17 * * 1-5 /root/sync.sh start
30 7 * * 1-5 /root/sync.sh stop

Thats it! Hope that helps...


All times are GMT -5. The time now is 04:06 PM.