bash script to copy files onto windows server

santoshmotwani
santoshmotwani used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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.

Top Expert 2010

Author

Commented:
no i dont want delete ,

good idea actually i will try rsync ....
Top Expert 2010

Author

Commented:
i want copy all folder with date format as i have explained in my question..
11/26 Forrester Webinar: Savings for Enterprise

How can your organization benefit from savings just by replacing your legacy backup solutions with Acronis' #CyberProtection? Join Forrester's Joe Branca and Ryan Davis from Acronis live as they explain how you can too.

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?
Top Expert 2010

Author

Commented:

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..........
Top Expert 2010

Author

Commented:
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?

Top Expert 2010

Author

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

let me try rsync sorry....
Top Expert 2010

Author

Commented:
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
Top Expert 2010

Author

Commented:
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.
#!/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.
                 rsync -auv "$folder/"  "$outfolder/$basefolder/"   
        else
                cp -pr "$folder" "$outfolder/$basefolder"
        fi
done

Open in new window

Top Expert 2010

Author

Commented:
Gr8 work,,,, Thnx heaps
Top Expert 2010

Author

Commented:
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

Top Expert 2010

Author

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


Top Expert 2010

Author

Commented:
n yeh ... production server is in different state and it may slow down the link as well..
Top Expert 2010

Author

Commented:
Thnx mate !!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial