simon-dreher.de

As previously described I use seafile to keep my devices in sync. Unfortunately there is only a client for Android which packs the Webinterface into an app but doesn't offer automatic syncing so that I couldn't easily integrate my Jolla-device. Anyway I didn't want to sync all my images to my phone, but I thought it would be pretty nice if at least all my photos from the phone would be uploaded and backed up right after they are made.

So after searching for some app that would do what I want, I realized that my Jolla phone is like a normal linux distribution and I can use all the normal linux tools.

The classic tool for syncing is rsync and it perfectly fits for this job where you just have to sync in one direction.

Preparing the Client

First of all you need some way to get access to your server from your phone. We use a ssh login with a key to do this. Therefor you have to generate a ssh key on your phone.

ssh-keygen

Choose a filename and choose no passphrase. For comfortable use you have to add the host and the identity file to your ssh-config in '~/.ssh/config'.

Host ananas
    HostName ananas.simon-dreher.de
    User $username
    IdentityFile ~/.ssh/ananas

Of course you have to adjust the values to your needs. Now comes the main part of the work, we have to set a script up, that monitors a directory for us and executes a script if any file changes or gets added. Normally inotify would be used, but since our Sailfish OS is so bleeding edge we can already use systemd.

The setup is based on the tutorial on rsync in the arch wiki. BTW: vim is already installed, but available under 'vi' or you can use TinyEdit if you prefer a GUI.

Create a file '~/.config/systemd/user/backup.path' with following content:

[Unit]
Description=Checks if paths changed or contain new images.

[Path]
PathChanged=%h/Pictures/Camera

[Install]
WantedBy=default.target

Next you create the file '~/.config/systemd/user/backup.service' which contains

[Unit]
Description=Backs up files

[Service]
ExecStart=/home/nemo/backup.sh

Here comes a difference to the arch wiki, where rsync is called directly, whereas here we use a custom script where we can do additional computations. This script we have to create next:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#!/bin/sh

# Check if we're on wlan
if [ $(ifconfig | grep -o wlan0) ]; then
    echo "WLAN used"
    # Start backup using rsync
    /usr/bin/rsync Pictures/Camera/ -CErltm ananas:/mnt/Bilder/Jolla
else
    echo "No WLAN"
fi
exit

This text is saved into '/home/nemo/backup.sh'. It still misses some functionality that starts a sync when you come back to a WLAN and you made photos without WLAN connection. Until now you have to run the script manually or take another photo. If you instead like to automatically sync the photos when the WLAN comes back you can use the following source:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/bin/sh

# Check if we're on wlan
if [ $(ifconfig | grep -o wlan0) ]; then
    echo "WLAN used"
    # Start backup using rsync
    /usr/bin/rsync Pictures/Camera/ -CErltm ananas:/mnt/Bilder/Jolla
else
    echo "No WLAN"
    mkdir /tmp/backup.lock
    if [ $? -ne 0 ]; then
        echo "Another script is already waiting for WLAN to sync."
        exit 0
    else
        # Wait for WLAN to come back
        until [ $(ifconfig | grep -o wlan0) ]; do
            echo "Still no WLAN"
            sleep 60
        done
        /usr/bin/rsync Pictures/Camera/ -CErltm ananas:/mnt/Bilder/Jolla
        rm -rf /tmp/backup.lock
    fi
fi

exit

Since I found no way to trigger the script when WLAN comes back I had to fall back to polling. You can change the interval, how long the script sleeps.

Preparing the server

For the SSH connection the server has to know the public key and therefor you have to somehow get '~/.ssh/ananas.pub' or how you called the SSH keyfile in part one onto your server. There you have to append it to '~/.ssh/authorized_keys'.

cat ananas.pub >> .ssh/authorized_keys

I recommend that you secure this ssh access since the key file lies on a mobile device and is not protected by a passphrase.

You can add two v to the options of the rsync command in the shell script. Then it's more verbose and shows you which command is executed on the server.

rsync --server -vvlmtErCe.iLs . /mnt/Bilder/Jolla

This command can now pasted into the authorized_keys file before the appended key, on the same line:

command="rsync --server -lmtErCe.iLs . /mnt/Bilder/Jolla",no-agent-forwarding,no-port-forwarding,no-pty,no-user-rc,no-X11-forwarding ssh-rsa AA...

Now this is the only command that can be executed with this SSH key. Note that if you remove the vv from the script you also have to remove them from the authorized_keys file.

Finishing

Now run the script manually once to test and add fingerprint to known hosts. If everything worked we can now enable our systemd files:

systemctl --user start backup.path
systemctl --user enable backup.path

Now we can take a photo and magically it syncs to your server and all your other devices. Tadaa.