Project

General

Profile

Feature #827

Automount and Sync a USB Drive on Arch Linux

Added by Daniel Curtis over 7 years ago. Updated almost 7 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
Shell Scripts
Target version:
Start date:
07/15/2016
Due date:
% Done:

100%

Estimated time:
0.50 h
Spent time:

Description

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:
    sudo pacman -S rsync
    

Device Identification

  • Get the list of drives:
    sudo fdisk -l
    
  • Start by getting the device label:
    sudo blkid /dev/sdb1
    
  • Create a label for the USB drive:
    sudo dosfslabel /dev/disk/by-uuid/ACE7-1996 "SYNCSHARE" 
    
  • Make a temporary mount directory:
    sudo mkdir /media/SYNCSHARE
    
  • Edit the fstab file:
    sudo nano /etc/fstab
    
    • Add an fstab entry for the SYNCSHARE device:
      UUID=ACE7-1996    /media/SYNCSHARE    vfat    users,noauto    0 0
      

Sync Script

  • Create a simple two-way rsync script:
    sudo nano /usr/local/bin/syncshare.sh
    
    • And add the following
      #!/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 --modify-window=1 --size-only --exclude ".csync*" --exclude ".owncloud*" /media/SYNCSHARE/owncloudshare/ /home/bob/owncloudshare/
        rsync -rtuv --modify-window=1 --size-only --exclude ".csync*" --exclude ".owncloud*" --delete /home/bob/owncloudshare/ /media/SYNCSHARE/owncloudshare/
      
        sync
      
        sudo -u bob DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send "SYNCSHARE Finished Synchronizing!" 
      
        sleep 5
      
        umount /media/SYNCSHARE
      else
        rsync -rtuv --modify-window=1 --size-only --exclude ".csync*" --exclude ".owncloud*" /media/SYNCSHARE/owncloudshare/ /home/bob/owncloudshare/
        rsync -rtuv --modify-window=1 --size-only --exclude ".csync*" --exclude ".owncloud*" --delete-delay /home/bob/owncloudshare/ /media/SYNCSHARE/owncloudshare/
      
        sync
      
        sudo -u bob DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send "SYNCSHARE Finished Synchronizing!" 
      
        sleep 5
      
        umount /media/SYNCSHARE
      fi
      

      NOTE: Adding the -n flag to the rsync commands will help when testing whether or not the script will run correctly.
      NOTE: Make sure to change the username and uid number to the correct values for the notify-send commands.
      NOTE: FAT filesystems require the --modify-window=1 --size-only flags in order to synchronize correctly.
  • Then make it executable:
    sudo chmod +x /usr/local/bin/syncshare.sh
    

Systemd Service

  • Create a systemd unit file:
    sudo nano /etc/systemd/system/syncshare.service
    
    • And add the following:
      [Unit]
      Description=SyncShare trigger
      Requires=dev-disk-by\x2dlabel-SYNCSHARE.device
      After=dev-disk-by\x2dlabel-SYNCSHARE.device
      
      [Service]
      ExecStart=/usr/local/bin/syncshare.sh
      
      [Install]
      WantedBy=dev-disk-by\x2dlabel-SYNCSHARE.device
      
  • And reload the systemd daemon:
    sudo systemctl daemon-reload
    
  • Enable the syncshare service:
    sudo systemctl enable syncshare
    

Udev Rule

  • List the attached USB devices and get the vendor and product IDs:
    lsusb
    
    • Example output:
      Bus 003 Device 004: ID 0781:3528 SanDisk Corp. Cruzer
      

      NOTE: The above example uses 0781 Vendor ID and 3528 Product ID.
  • Add a udev rule to start the syncshare systemd service on device hotplug:
    sudo nano /etc/udev/rules.d/99-local.rules
    
    • And add the following:
      SUBSYSTEMS=="usb", ATTRS{idVendor}=="0781", ATTRS{idProduct}=="3528", ACTION=="add", TAGS+="systemd", ENV{SYSTEMD_WANTS}=="syncshare.service" 
      
  • Restart udev:
    sudo systemctl restart systemd-udevd
    

Resources

Also available in: Atom PDF