The given bash script synchronizes can be used to synchronize a directory from a local file system with a windows share.
#!/bin/sh
# Synchronize.sh
# Copyright (C) 2008 Ferad Zyulkyarov
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Shell script for synchronizes files between local linux directory and
# windows share
#
# This script synchronizes a local directory from a remote windows share.
# First it mounts the remote share then synchronizes and then unmounts
# the remote share.
#
# Requirements: To use this scrypt you need to have installed
# 1. mount.cifs or smbmount
# 2. rsync
#
# Placeholders in the script that should be adapted:
# mount - the mount command to use {smbmount, mount.cifs, mount -t smbfs}
# umount - the umount command to use {smbumount, umount.cifs, umount}.
# remote_share$ e.g. //10.0.0.1/myshare
# mount_dir - where to mount the remote share e.g.
# /home/username/localmyshare
# username - this is the username to access the remote share
# password - your password
# source_synch_dir - the source to synchronize from
# dest_synch_dir - the destination to synchronize to
#
mount='mount.cifs'
umount='umount.cifs'
remote_share='//10.0.0.1/myshare'
mount_dir='/home/username/localmyshare'
username='your username'
password='your password'
source_synch_dir='/home/username/localmyshare'
dest_synch_dir='/home/username/syncdata'
mounted_here=0
print_usage()
{
echo "Usage: $0 {get, put}"
echo " get - synchornize from the remote share to the local"
echo " put - synchornize from local to the remote share"
}
if [ $# -ne 1 ] ; then
print_usage
exit 1
fi
synchronize_command="empty"
if [ $1 = "get" ] ;
then
synchronize_command="rsync -av -delete $source_synch_dir/ $dest_synch_dir"
elif [ $1 = "put" ] ;
then
synchronize_command="rsync -av -delete $dest_synch_dir/ $source_synch_dir"
else
print_usage
exit 1
fi
if [ ! -x $source_synch_dir ] ;
then
echo "Mounting remote file system..."
$mount $remote_share $mount_dir -o username=$username,password=$password
if [ $? -ne 0 ] ; then
echo "Error: Cannot mount the remote file system."
exit 2
else
mounted_here=1
echo "Remote file system was mounted successfully."
fi
fi
echo "Starting synchornization..."
#rsync -av -delete $source_synch_dir $dest_synch_dir
$synchronize_command
synch_result=$?
# unmount the share if we mounted here
if [ $mounted_here -eq 1 ];
then
$umount $mount_dir
if [ $? -ne 0 ] ; then
echo "Error: failed to unmount the remote file system."
else
echo "Remote file system unmounted successfully."
fi
fi
if [ $synch_result -ne 0 ] ; then
echo "Error: Synchornization failed with exit code: " $?
exit 3
else
echo "Synchornization completed successfully."
fi
exit 0
Tuesday, November 25, 2008
Subscribe to:
Comments (Atom)