- 1 year ago
Sync Linux directory with Windows network path
To synchronize a directory on a Linux system with a Windows network path (shared folder) using the CIFS/SMB protocol, you can use the mount
command to mount the network share and then use tools like rsync
to perform synchronization. Here's a step-by-step guide:
Install Required Packages:
Make sure you have the necessary packages installed on your Linux system. You'll needcifs-utils
for mounting CIFS shares andrsync
for synchronization.bashsudo apt-get update
sudo apt-get install cifs-utils rsync
Create a Directory for Mounting:
Create a local directory on your Linux system where you'll mount the Windows network share.bashsudo mkdir /mnt/windows_share
Mount the Network Share:
Use themount
command to mount the Windows network share onto the local directory. Replace//server/share
with your actual network path, and adjust the options as needed.bashsudo mount -t cifs //server/share /mnt/windows_share -o username=your_username,password=your_password
Replace
your_username
andyour_password
with your Windows credentials.Synchronize with
rsync
:
Now that the network share is mounted, you can usersync
to synchronize the local directory on Linux with the network share.bashrsync -av --delete /path/to/local/directory/ /mnt/windows_share/
Adjust the source directory (
/path/to/local/directory/
) and destination directory (/mnt/windows_share/
) as needed.Unmount the Network Share:
When you're done synchronizing, unmount the network share using theumount
command.bashsudo umount /mnt/windows_share
Automate the Process:
To automate the synchronization process, you can create a shell script that performs the mounting, synchronization, and unmounting steps. Use thecrontab
scheduler to run the script at scheduled intervals.bash#!/bin/bash
# Mount the network share
sudo mount -t cifs //server/share /mnt/windows_share -o username=your_username,password=your_password
# Perform synchronization
rsync -av --delete /path/to/local/directory/ /mnt/windows_share/
# Unmount the network share
sudo umount /mnt/windows_share
Remember that storing credentials in scripts might not be the most secure practice. Consider using a more secure method to handle credentials, such as creating a credentials file with restricted permissions. Also, ensure that you have the necessary permissions to access the Windows network share.