Project

General

Profile

Feature #827

Updated by Daniel Curtis over 7 years ago

Once in a while I will need to do a two-way synchronization of the contents of an owncloud sync folder to a specific USB drive, and having this task done automagically by systemd. 

 *NOTE*: The USB drive used in this guide is FAT32. If using NTFS, you must install the ntfs-3g-fuse package from the AUR. 

 * Make sure rsync is installed: 
 <pre> 
 sudo pacman -S rsync 
 </pre> 

 h2. Device Identification 

 * Get the list of drives: 
 <pre> 
 sudo fdisk -l 
 </pre> 

 * Start by getting the device label: Install ntfs-3g: 
 <pre> 
 sudo blkid /dev/sdb1 pacman -S ntfs-3g 
 </pre> 

 * Make a temporary mount directory: 
 <pre> 
 sudo mkdir /media/SYNCSHARE 
 </pre> 

 * Edit Start by getting the fstab file: device label: 
 <pre> 
 sudo nano /etc/fstab blkid /dev/sdb1 
 </pre> 
 #* Add an fstab entry *NOTE*: If a label does not exist, generate one for the SYNCSHARE device: partition to be automounted: 
 <pre> 
 UUID=ACE7-1996 	 /media/SYNCSHARE 	 vfat 	 users,noauto 	 0 0 sudo ntfslabel /dev/sdb1 SYNCSHARE 
 </pre> 

 h2. Sync Script 

 * Create a simple two-way rsync script: 
 <pre> 
 sudo nano /usr/local/bin/syncshare.sh 
 </pre> 
 #* And add the following 
 <pre> 
 #!/bin/sh 
 # Script to do a two-way sync with a USB drive 

 if [ $(mount | grep -c /media/SYNCSHARE) != 1 ] 
 then 
   /sbin/mount /dev/disk/by-uuid/ACE7-1996 || exit 1 
   echo "/media/SYNCSHARE is now mounted" 

   rsync -rtuv --exclude ".csync*" --exclude ".owncloud*" --delete /home/bob/owncloudshare/ /usr/share/owncloudshare/ /media/SYNCSHARE/owncloudshare/ 
   rsync -rtuv --exclude ".csync*" --exclude ".owncloud*" /media/SYNCSHARE/owncloudshare/ /home/bob/owncloudshare/ 
  
   sync 

   notify-send "SYNCSHARE Finished Synchronizing!" 

   sleep 5 

   umount /media/SYNCSHARE 
 else 
   rsync -rtuv --exclude ".csync*" --exclude ".owncloud*" --delete /home/bob/owncloudshare/ /media/SYNCSHARE/owncloudshare/ 
   rsync -rtuv --exclude ".csync*" --exclude ".owncloud*" /media/SYNCSHARE/owncloudshare/ /home/bob/owncloudshare/ 
  
   /usr/share/owncloudshare/ 

 sync 

   

 notify-send "SYNCSHARE Finished Synchronizing!" 

   sleep 5 

   

 umount /media/SYNCSHARE 
 fi 
 </pre> 

 * Then make it executable: 
 <pre> 
 sudo chmod +x /usr/local/bin/syncshare.sh 
 </pre> 

 h2. Systemd Service 

 * Create a systemd unit file: 
 <pre> 
 sudo nano /etc/systemd/system/syncshare.service 
 </pre> 
 #* And add the following: 
 <pre> 
 [Unit] 
 Description=SyncShare trigger 
 Requires=media-SYNCSHARE.mount 
 After=media-SYNCSHARE.mount 

 [Service] 
 ExecStart=/usr/local/bin/syncshare.sh 

 [Install] 
 WantedBy=media-SYNCSHARE.mount 
 </pre> 

 h2. Udev Rule 

 * Add a udev rule to start the syncshare systemd service on device hotplug: 
 <pre> 
 sudo nano /etc/udev/rules.d/99-local.rules 
 </pre> 
 #* And add the following: 
 <pre> 
 SUBSYSTEMS=="usb", ATTRS{idVendor}=="002b", ATTRS{idProduct}=="1905", ACTION=="add", ENV{SYSTEMD_WANTS}=="syncshare.service" 
 </pre> 

 h2. Resources 

 * http://serverfault.com/questions/766506/automount-usb-drives-with-systemd 
 * https://bbs.archlinux.org/viewtopic.php?id=207050 
 * https://bbs.archlinux.org/viewtopic.php?id=192461 
 * https://wiki.archlinux.org/index.php/Persistent_block_device_naming#by-label 
 * https://askubuntu.com/questions/25071/how-to-run-a-script-when-a-specific-flash-drive-is-mounted/190366 
 * http://jasonwryan.com/blog/2014/01/20/udev/ 
 * https://wiki.archlinux.org/index.php/NTFS-3G#Manual_mounting

Back