Link to home
Start Free TrialLog in
Avatar of santoshmotwani
santoshmotwaniFlag for Australia

asked on

bash script to copy files onto windows server

Hi Experts ,

I have a written a bash script to copy files from linux server to windows.
Now the requirements have changed. The files on linux server are now stored in date folders.

Example :

folder name - 2011-01-21
and files under it.

I want to write a script that will check for the specific folder name under windows server , if it is not there it will create one and will copy files from 2011-01-21  and respectively for other folders as well.


Please help

Thnx

#bin/bash

mount -t smbfs -o username=name,password=password //server/foldername /mnt/data
cd /mnt/files
cp *.pdf /mnt/data

Open in new window

Avatar of Steve Tempest
Steve Tempest
Flag of Australia image

have you looked at rsync?

#bin/bash

mount -t smbfs -o username=name,password=password //server/foldername /mnt/data
rsync -axogvz --delete /mnt/files/ /mnt/data/

or did you only want pdf files? Also with the above command please remeber that the --delete will cause rsync to mirror the data.. so if a file is not in the source folder it will be deleted from the destination folder automatically.

Avatar of santoshmotwani

ASKER

no i dont want delete ,

good idea actually i will try rsync ....
i want copy all folder with date format as i have explained in my question..
ok so are you saying that rsync is no good? I can script up what you are after... is this basically what you want

look in /mnt/files for folders

check and see if it matches the date format you have above

then if it matches check /mnt/data/ for the folder

if the folder is not in /mnt/data/ then copy the folder and all of it's contents to /mnt/data/

if the folder already exists on /mnt/data then do nothing?

is that what you are after?

look in /mnt/files for folders         - YES


check and see if it matches the date format you have above    -  above is just a example... Basically daily a new folder is created with a date in the above format.  ( screenshot attached )

then if it matches check /mnt/data/ for the folder - Yes

if the folder is not in /mnt/data/ then copy the folder and all of it's contents to /mnt/data/ - YEs

if the folder already exists on /mnt/data then do nothing?  -   if it exists then it should do something similar to rsync. ( coz we may get new files in yesterdays folder --- its not the case very often)

Thnx mate for helping me..........
are there other folders other than the date folders in /mnt/data?
mount -t smbfs -o username=name,password=password //server/foldername /mnt/data
rsync -axogvz /mnt/files/ /mnt/data/

I would think above will work for you, sorry you didnt answer if rsync is not an option?

yes there are other folders that i do not want to copy .....

let me try rsync sorry....
yes rsync does help... but

There are already files in there ... i do not wish to delete or modify them.....
ok I'm working on the script

without the --delete it wont delete the any files but it will make sure that the files in the source are all on the destination and also that they are all exactly the same...
#!/bin/sh
#set -x
mount -t smbfs -o username=name,password=password //server/foldername /mnt/data

infolder="/mnt/files"
outfolder="/mnt/data"
cd $infolder
for folder in `find $infolder -maxdepth 1 -type d -name "*-*-*"`
do
        echo $folder
        basefolder=`basename $folder`
        if [ -d "$outfolder/$basefolder" ]; then
                echo Folder $outfolder/$folder already exists in destination
                ## rsync could go here but I'm not sure if you want to do that as it will update the files.
        else
                cp -pr "$folder" "$outfolder/$basefolder"
        fi
done
Thnx Heaps last thing what if i want to rysnc.....

  ## rsync could go here but I'm not sure if you want to do that as it will update the files.
ASKER CERTIFIED SOLUTION
Avatar of Steve Tempest
Steve Tempest
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Gr8 work,,,, Thnx heaps
Hi APNFSSC,

i was just wondering if i make it bit faster by adding one more condition i.e.

if folder exists it will count number of files in it . If it does not match to number of files in a source folder than rsync gets executed.....

sorry mate it would be gr8 if u help me here....
rsync should be really fast I would think? I do 14tb rsync over a wan in 2hours... millions of files... how many files are going to be in the folder?
below should do something like that... It will seach each folder for files and count them ... then compare the count if not equal it will rsync.

 
#!/bin/sh
#set -x
mount -t smbfs -o username=name,password=password //server/foldername /mnt/data

infolder="/mnt/files"
outfolder="/mnt/data"
cd $infolder
for folder in `find $infolder -maxdepth 1 -type d -name "*-*-*"`
do
        echo $folder
        basefolder=`basename $folder`
        if [ -d "$outfolder/$basefolder" ]; then
                echo Folder $outfolder/$folder already exists in destination
               ### -u option in rsync will only copy newer files over.
                 local_count=`find $folder -type f | wc -l`
                 remote_count=`find "$outfolder/$basefolder/" | wc -l`
                 if [ "$local_count" -ne "$remote_count" ];then
                         rsync -auv "$folder/"  "$outfolder/$basefolder/"
                 fi
        else
                cp -pr "$folder" "$outfolder/$basefolder"
        fi
done

Open in new window

There are 235,511 files already . I am just worried coz its a production server , running on single core with all other processes as well. But anyways i will run it tonight see hw i go.


n yeh ... production server is in different state and it may slow down the link as well..
Thnx mate !!!