Script to copy contents of folder

dtallman
New Contributor

Hello,

I am interested in a script that would copy a folder and its contents of that folder from the local computer to a share point located on an xserver. Because this script would be run on multiple user computers, it is necessary that the script also rename the new copied folder on the xserver with the each appropriate person's username. -- to help us know which person has had their folder copied.

It would best if the script also did not change permissions of the folder and files.

Any help is appreciated.

Thank you,
Deric

2 REPLIES 2

JRM
Contributor

The first thing you will want to do is mount the share. You will probably want to make a user for this so that you don't have to obtain the users credentials again, unless the volume will already be mounted by that user, then you will want them to have just have permissions to it and skip this part. If you are going to end up with a mix then you will need to preform a check.

#!/bin/bash afp_server="Server_DNS_or_IP" afp_volume="yourvolume" AFP_Monitor_User="user" AFP_Monitor_PSWD="password" if ! [ -d "/Volumes/$afp_volume" ] ; then # Assume they do not have this volume mounted mkdir "/Volumes/$afp_volume" ; mount_afp "afp://$AFP_Monitor_User:$AFP_Monitor_PSWD@$afp_server/$afp_volume" "/Volumes/$afp_volume" else # They have a volume of the same name mounted [on some server] # I would test for proper file rights here touch "/Volumes/$afp_volume/where-they-are-saving" if [ -e "/Volumes/$afp_volume/where-they-are-saving" ] ; then true # User can write here else false # User can't write here fi fi # create the users folder we are going to move. Casper uses $3 for the logged in user when running scripts. # set a variable so we don't have to retype it a lot. UsersCustomPath="/Volumes/$afp_volume/where-they-are-saving/$3" # use -p to create any needed sub directories mkdir -p "$UsersCustomPath" # Now let's do the copy. # man says use -p to preserve the following attributes of each source file # in the copy: modification time, access time, file flags, file mode, # user ID, and group ID, as allowed by permissions. Access Control # Lists (ACLs) and Extended Attributes (EAs), including resource # forks, will also be preserved. # # There is more but you probably won't run into it... consult 'man cp' if you want to know more. # # we also likely want sub directories: use -R # # if you want to follow symbolic links add -L # if you want each copy to show in the log use verbose mode: -v cp -pRv "/Users/$3/Folder-to-copy-to-server" "UsersCustomPath" # do any clean up here. # rm -Rv "/Users/$3/Folder-to-copy-to-server"

Please note I threw this together so there can be typo's and likely better ways to do this. I commented out the RM because you would want to make sure there weren't any errors.

Other things to consider:
1. checking for enough space to do the copy (esp if to a server startup volume)
2. Do you want it in a folder and not in perhaps an archive (.dmg) file
3. something
4. something
5. profit?

(ok kidding about 3-5)

JRM
Contributor

BTW -

AFP_Monitor_User and AFP_Monitor_PWD were variables from a script I have that checks out cruddy novel afp service, these would be the user and password combo of anyone that wasn't already mounted and having rights to the server.

following the line "# User can't write here" the following should probably be an "exit 1" (on a newline) so the script doesn't try and continue when it can't write to the server.