Last active
March 5, 2017 11:40
-
-
Save norpol/3ba784f51113e8e96d16667cda425995 to your computer and use it in GitHub Desktop.
Automatically mount (without root) and copy content helper script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Findmount of OR (Mount and Unmount) a given block device | |
# ask for user input and | |
# rsync it's entire content to a specified path | |
# and do it without root privileges | |
# Requires: rsync, gvfs-mount, grep with '-c' (count option), awk, ✨Unicode | |
# Useful for quickly copying photos from a camera to a specified directory | |
# and unmounting (but not deleting content) properly. | |
set -ue | |
# Target directory/path into which content of DEV is going to be copied | |
TARGET="${2}" | |
# /dev/sdXY1 | |
DEV="${1}" | |
# check if device is 'special block' and grep count if mounted | |
if [ -b "${DEV}" ] && [ $(mount | grep "${DEV}" -c) -eq 0 ]; then | |
MNT=$(gvfs-mount -d "${DEV}" | awk '{print $4}') | |
UMOUNT="1" | |
else | |
MNT=$(mount | awk -v dev=${DEV} '$1 == dev {print $3; exit}') | |
UMOUNT="0" | |
fi | |
# Fail if MNT couldn't got set | |
[ -n "${MNT}" ] | |
# Ask if user rsync should happen | |
echo -n "rsync -acv \"${MNT}\" \"${TARGET}\"? (Y/n): " | |
read answer | |
[ "${answer}" = "n" ] || rsync -acv "${MNT}" "${TARGET}" | |
# Only Umount if unmounted earlier | |
echo "UMOUNT=${UMOUNT}" | |
[ "${UMOUNT}" -eq 1 ] && gvfs-mount -u "${MNT}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment