Instantly share code, notes, and snippets.

Embed
What would you like to do?
Move Dropbox to a sparse file
# Location of the image which will contain the new ext4 partition
DROPBOXFILE="$HOME"/.dropbox.img
# Current location of my Dropbox folder
DROPBOXHOME="$HOME"/Dropbox
# Where we will copy the folder to. If you have little space, you could make this
# a folder on a USB drive
DROPBOXBACKUP="$HOME"/old_Dropbox
# What size is the Dropbox image file going to be. It makes sense to set this
# to whatever the capacity of your Dropbox account is, or a little more.
DROPBOXSIZE="20G"
# Create a 'sparse' file which will start out small and grow to the maximum
# size defined above. So we don't eat all that space immediately.
dd if=/dev/zero of="$DROPBOXFILE" bs=1 count=0 seek="$DROPBOXSIZE"
# Format it ext4, because Dropbox Inc. says so
sudo mkfs.ext4 "$DROPBOXFILE"
# Move the current Dropbox folder to the backup location
mv "$DROPBOXHOME" "$DROPBOXBACKUP"
# Make a new Dropbox folder to replace the old one. This will be the mount point
# under which the sparse file will be mounted
mkdir "$DROPBOXHOME"
# Make sure the mount point can't be written to if for some reason the partition
# doesn't get mounted. We don't want dropbox to see an empty folder and think 'yay, let's delete
# all his files because this folder is empty, that must be what they want'
sudo chattr +i "$DROPBOXHOME"
# Mount the sparse file at the dropbox mount point
sudo mount -o loop "$DROPBOXFILE" "$DROPBOXHOME"
# Copy the files from the existing dropbox folder to the new one, which will put them
# inside the sparse file. You should see the file grow as this runs.
sudo rsync -a "$DROPBOXBACKUP"/ "$DROPBOXHOME"/
# Create a line in our /etc/fstab so this gets mounted on every boot up
echo "$DROPBOXFILE" "$DROPBOXHOME" ext4 loop,defaults,rw,relatime,exec,user_xattr 0 0 | sudo tee -a /etc/fstab
# Let's unmount it so we can make sure the above line worked
sudo umount "$DROPBOXHOME"
# This will mount as per the fstab
sudo mount -a
# Set ownership and permissions on the new folder so Dropbox has access
sudo chown $(id -un) "$DROPBOXHOME"
sudo chgrp $(id -gn) "$DROPBOXHOME"
@mdebusk

This comment has been minimized.

Copy link

mdebusk commented Dec 31, 2018

The disk image doesn't mount at boot on my Ubuntu 18.04.01 system. Instead, the boot fails with a bad fstab.
I can mount it after boot finishes and everything works wonderfully.
The line in fstab is exactly this:
/home/michael/.dropbox.img /home/michael/Dropbox ext4 loop,defaults,rw,relatime,exec,user_xattr 0 0
Any idea what I'm doing wrong?

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