Feature #827
Automount and Sync a USB Drive on Arch Linux
Description
- Table of contents
- Device Identification
- Sync Script
- Systemd Service
- Udev Rule
- Resources
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
- Add an fstab entry for the SYNCSHARE device:
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.
- And add the following
- 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 add the following:
- 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.
- Example output:
- 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"
- And add the following:
- Restart udev:
sudo systemctl restart systemd-udevd
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
Updated by Daniel Curtis about 8 years ago
- Description updated (diff)
- Status changed from New to In Progress
- % Done changed from 0 to 50
Updated by Daniel Curtis about 8 years ago
- Description updated (diff)
- Status changed from In Progress to Resolved
- % Done changed from 50 to 100