NOTE: Layout of my blog is getting to me (i.e. it's bad), and I will fix it.

These days, we create images and video of our personal lives constantly. Such files are uploaded to cloud, but there are other files that are not.

I was looking for a way to backup data that offered deduplication and security. I thought about building a solution that would keep index of files and only store their content once - using encryption solution of choice. I came across Borg (can't remember how, could be from one of the HN posts), and it ticked off those features. For now, I'm only using it to backup my phone data but eventually I will move onto other backups.

This process syncs an Android phone's internal storage to a local directory and uses borg to backup important paths from that local directory (ex. DCIM) to a borg target. In my case, it was an external USB-C drive.

Environment

  • Fedora 32
  • linuxbrew
  • sudo user
  • Samsung Galaxy S10+ (Android 12)
  • Samasung Portable SSD T7

Installation

$ # install borg
$ brew install borgbackup

$ # install MTP tool to mount phone's storage 
$ sudo dnf install simple-mtpfs

Setup

Note: When connecting the phone to read files, set the USB mode to "Tranferring Files/Android Auto"

$ # list connected phones
$ simple-mtpfs -l

$ # Mount the phone to /mnt/s10, and set mount's file permissions to a correct user
$ sudo simple-mtpfs --device 1: /mnt/s10 -o allow_other -o uid=`id -u` -o gid=`id -g`

$ # Mount the external drive to /run/media/user/T7, and initialize it with borg
$ borg init --encryption=repokey /run/media/user/T7

$ # Sync phone DCIM folder to /home/s10
$ rsync -av /mnt/s10/DCIM /home/s10/

$ # Don't forget to unmount the phone afterward
$ sudo umount /mnt/s10

Backup

Creating a backup is easy, just need to pick a good naming scheme and issue following command:

$ # backup
$ export BORG_PASSPHRASE=<passphrase>
$ export BORG_REPO=/run/media/user/T7
$ borg create --progress $BORG_REPO::S10PlusDCIM`date -I` DCIM

Now sync and backup steps can be automated and run regularly, where borg dedups and store new files only.

Listing backups, files and restoring them

$ # list all backups
$ export BORG_PASSPHRASE=<passphrase>
$ export BORG_REPO=/run/media/user/T7
$ borg list

$ # list files in a backup
$ borg list $BORG_REPO::<backup_name>
...
<list of paths in backup>


$ # restore a file
$ # create an empty dir
$ mkdir borg_backup && cd borg_backup
$ borg extract $BORG_REPO::<backup_name> path/to/extract