Android and Linux

Wednesday, July 21, 2010

Backup photos with rsync

I've never used rsync much but have always wanted to try it more. Maybe it was my inexperience or maybe it's the non-standard Android version, but it turned out to be a pain!

I started with a generic rsync command and customized it, but every step failed and I had to find the answer why. Then, after a successful connection and transfer, it threw errors on every file, which I found was due to a bug in some versions of rsync, so I had to find a workaround to that.

At any rate, here is a basic, working rsync command for transferring photos from the phone to a computer.

It can be used to back up any directory by changing "/sdcard/DCIM/Camera" to the directory you want to backup. It can also transfer files from the computer to the phone by changing the order of the two paths from /sdcard/DCIM/Camera COMPUSER@IP:/PATH/TO/SYNC/TO to COMPUSER@IP:/PATH/ON/COMP /PATH/ON/PHONE
#! /system/bin/sh
rsync -rltDv -e "ssh -l USER -i /PATH/TO/SSHKEY" /sdcard/DCIM/Camera USER@IP:/DIR/TO/SYNC/TO
Copy "syncpics" to your clipboard with this QR code, but don't forget to edit it to fill out your login information.

Monday, July 19, 2010

Various Linux/phone ssh transfer commands

ssh is wonderful for computer to computer communication and here are a few scripts which I use for PC <-> phone communication:

First, most of these use ssh and to log in without a password, they use ssh keys. There are numerous tutorials for setting up ssh keys, but I always point people to this site because it's a relatively clear explanation. Since this is an Android related blog, the Cyanogen wiki article on using Dropbear for ssh is more geared to our phones.

Once ssh keys are set up, one problem that may be encountered is your home IP address. You don't want to hard code it into a bunch of scripts in case it were to change, plus you may not be able to log in if it changes unless the computer notifies you in some way.

My solution is to store my home IP address in a hidden file on my sdcard named ".hip", which stands for "Home IP." Then I have a simple script named "hip" to display the contents of that file:
#! /system/bin/sh
cat /sdcard/.hip
Any time I write a script or command to connect to my computer, I put the "hip" command in a subshell so that it executes and places my IP address wherever it belongs in the script.

If my home IP is and that is contained in the .hip file, then "ssh user@" is the same thing as "ssh user@$(hip)"

I have a cable connection and my IP changes any time the cable goes out, which is once every couple of months. I have this script running on a cron job at home every hour to check for an IP change.
#! /bin/sh
ip=$(lynx -dump | grep "IP Information" | awk '{ print $3 }')
grep -q $ip .ip
then echo "IP matched, not sending mail"
echo "Sending IP change notification"
echo $ip > .ip
nail < .ip
It records the address in a file, checks the current IP and compares the two. It then sends a text message to my phone if they change. I can then put the new address in my .hip file and all my scripts work again.

Now, on to the fun stuff. Here are a few for transferring files:

ton1 - used to grab a file from my computer to the phone. To use, type "ton1 /home/me/somedirectory/file.txt"
#! /system/bin/sh
scp -i /PATH/TO/KEY USER@$(hip):"$1" /sdcard
ton1r - recursively transfer a directory. To use, type "ton1r /home/me/somedirectory"
#! /system/bin/sh
scp -i /PATH/TO/KEY -r USER@$(hip):"$1" /sdcard
tohome - Sends a file to my computer. To use, type "tohome /sdcard/somedirectory/file.txt"
#! /system/bin/sh
tohomer - Sends a directory to my computer. To use, type "tohomer /sdcard/somedirectory"
#! /system/bin/sh
scp -i /PATH/TO/KEY -r "$1" USER@$(hip):/PATH/TO/DIRECTORY
With my copy/paste trick for the terminal, I use these scripts:

First, I have this script named "paste2n1" on my home computer so I can log in at any time and get the clipboard contents without having to export the display:
#! /bin/sh
export DISPLAY=:0.0 && xsel -p && unset DISPLAY
And on the phone, this one is named "homeclip" and it logs in and runs the paste2n1 script, displaying the PC clipboard in the phone's terminal.
#! /system/bin/sh
ssh USER@$(hip) -i /PATH/TO/SSH/KEY 'paste2n1'
This one, named "tohomeclip" will take the contents of the phone's clipboard and copy it to the computer.
#! /system/bin/sh
clip | ssh USER@$(hip) -i /PATH/TO/SSH/KEY 'export DISPLAY=:0.0 && xclip && unset DISPLAY' && echo "copied to PC:
I also have these commands set up in GScript, which allows you to run commands or scripts from a home screen shortcut.
homeclip | copy
- transfers PC's clipboard contents to the phone's clipboard
ton1 $(homeclip)
- Allows me to copy a file name on the PC and transfer it to the phone.
ton1r $(homeclip)
- Same thing, except it works with a directory.

Once you can access your computer with your phone from anywhere in the world, and do it with very simple scripts, your mind tends to think of ways to use it, and these are just some examples of simple fun you can have with ssh.