How can I create a job that copies the Directory /media/hdd/docs to the Windows share \\Docs each night at 23:00, not overwriting existing files. The Windows share is shared to "Everyone" With read/Write on a Win2k12 server box

  • What do you mean by "job"? A cron job? What should it do with existing files? Rename them? Ignore them? Is the share mounted somewhere on the Debian box? – terdon Jan 18 '14 at 20:24
  • Look into rsync. – goldilocks Jan 18 '14 at 20:25
  • 1
    Do you want the files to get updated if they're newer/different from the Linux share to Windows share or are if they already exist not to touch them in any way? Also do you need to know how to mount the windows share on the Linux box or is that already working? – slm Jan 18 '14 at 20:30

Assuming a few requirements in your scenario, I would use the rsync man page to find the appropriate options and write a shell script similar to the following untested script.

Create a new file called /opt/sync_docs.sh with the following contents.

#/bin/env sh

# Use the second set of arguments for debugging
OPTS="-avzn --ignore-existing"
#OPTS="-avzn --ignore-existing -vv"

rsync $OPTS /media/hdd/docs/ smb://smbserver/Docs/ >/var/log/rsync/sync_docs.log 2>&1

ERRMSG="Docs sync to windows shared failed, better check it out"
if [ "$?" -ne "0" ]; then echo $ERRMSG; fi

Create the log directory and change ownership to the user you will run the script as. This user must have permission to write to the Samba share and permission to write to the log directory. From this point on, I'll refer to this user as "properuser".

# mkdir /var/log/rsync
# chown properuser /var/log/rsync

Test the script with the log options uncommented. Once the script is tested, adjusted and working appropriately, I would want to schedule the script to be run by cron at 23:00 daily.

Create a cron job in the appropriate folder, usually inside /etc/cron.d/, and fill in your email address.

0 23 * * * properuser /opt/sync_docs.sh

If you ever get an error message, just login and check the log file which will only show you the results of the last run.

# less /var/log/rsync/sync_docs.log
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.