Skip to content

Instantly share code, notes, and snippets.

@joeytwiddle
Created November 6, 2018 05:07
Show Gist options
  • Save joeytwiddle/17bee2e5028ea53bc73f255992bab218 to your computer and use it in GitHub Desktop.
Save joeytwiddle/17bee2e5028ea53bc73f255992bab218 to your computer and use it in GitHub Desktop.
Creates a file with an ext4 filesystem inside it, and moves Dropbox into that filesystem
#!/bin/bash
set -e
set -x
# You could put the fs file and the mountpoint inside your home folder
#new_fs_file="$HOME/dropbox_partition.ext4fs"
#mountpoint="$HOME/Dropbox.mnt"
# But that may not work if you are using a fuse-mounted homefolder (e.g. encrypted with encfs)
new_fs_file="/home/dropbox_for_$USER.ext4fs"
mountpoint="/mnt/dropbox.$USER"
size_meg=5000
uid="$(id -u)"
gid="$(id -g)"
permissions="$uid:$gid"
dropbox stop || true
# Sometimes dropbox will exit before shutdown is complete
while pgrep -u "$UID" dropbox
do sleep 4
done
if [ ! -d "$HOME/Dropbox.before_migration" ]
then mv "$HOME/Dropbox" "$HOME/Dropbox.before_migration"
fi
if [ ! -f "$new_fs_file" ]
then
sudo dd if=/dev/zero of="$new_fs_file" bs=1 count=0 seek=$((size_meg * 1024 * 1024))
sudo mkfs.ext4 -F "$new_fs_file"
fi
sudo mkdir -p "$mountpoint"
# Leave a marker file so it is clear whether the fs is mounted or not
sudo touch "$mountpoint"/I_AM_NOT_MOUNTED_YET
line_for_fstab="$new_fs_file $mountpoint auto defaults,user_xattr 0 0"
if grep -Fx "$line_for_fstab" /etc/fstab >/dev/null
then : #echo "fstab already has the entry"
else
(
echo
echo "# ext4 filesystem for Dropbox"
echo "$line_for_fstab"
) | sudo tee -a /etc/fstab >/dev/null
fi
sudo umount "$new_fs_file" 2>/dev/null || true
sudo umount "$mountpoint" 2>/dev/null || true
sudo mount "$mountpoint"
sudo chown "$permissions" "$mountpoint"
df -h "$mountpoint"
# Symlinking didn't work: dropbox still complained about the lack of ext4
#if [ -e "$HOME/Dropbox" ] || [ -L "$HOME/Dropbox" ]
#then
# symlink_target="$(readlink "$HOME/Dropbox")" || true
# if [ "$symlink_target" = "$mountpoint/Dropbox" ]
# then echo "symlink already exists"
# else
# echo "Could not create symlink $HOME/Dropbox because something is already there"
# exit 1
# fi
#else ln -s "$mountpoint/Dropbox" "$HOME/Dropbox"
#fi
if [ -d "$mountpoint/Dropbox/.dropbox.cache" ]
then echo "There are already files in the new folder!"
else
echo "Copying files into new filesystem..."
cp -a "$HOME/Dropbox.before_migration" "$mountpoint/Dropbox"
fi
#echo "If everything looks ok, you should now run: dropox start"
echo "If everything looks ok, you should now reinstall Dropbox, and select this as your install folder: $mountpoint/Dropbox"
echo "An easy way to do that is to always start dropbox like this: HOME=\"$mountpoint\" dropbox start"
echo "(You might need to add -i on the first run.)"
@edmundhighcock
Copy link

Maybe you could emphasise that the script should not be run as root (it's obvious if you read the script carefully but... )?

@joeytwiddle
Copy link
Author

joeytwiddle commented Aug 17, 2019

Glad it could help you!

But no, it shouldn't be run as root, because it wants to know the uid of the user.

But it does call sudo to do some things as root. So we must enter the root password (probably only once).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment