Backing up your Raspberry Pi code using rsync
This is how I develop code on my Raspberry Pi:
ssh into the Pi from my Macbook Pro from 2 Terminals, and use emacs on one to edit the code, while I run the code from the other. This way I don’t need to use the annoyingly slow X-server or VNC solutions to display UI from the PI for editing.
Installing emacs on the Pi is exceedingly simple, by the way:
sudo apt-get install emacs
There are other editors, but really nothing beats emacs or vi when it comes to editing code on Linux, and I highly recommend that you spend some time learning one of these editors.
Since I develop code on the Pi directly, I need a backup plan, and the plan is rsync.
rsync is a Unix utility that can synchronyze your files between directories and other machines (via ssh).
rsync is very powerful, and not to be meddled with unless you are paying attention. If you are mucking around it with it, (a) back up your test files and (b) use the -n or “dry run” flag so it only tells you what will be done and not actually do it, till you are satisfied.
Here is my script to back up my code directory (recursively) from the Raspberry Pi on to my Macbook:
# backup script for Raspberry Pi # # electronut.in # #!/bin/bash echo Backing up RPi \#1... # set this to Raspberry Pi IP address PI_ADDR="192.168.4.31" # set this to Raspberry Pi code directory # note that the trailing / is important with rsync PI_DIR="code/" # set this to local code (backup) directory BKUP_DIR="/Users/mahesh/code/rpi1/" # run rsync # use this first to test: # rsync -uvrn pi@$PI_ADDR:$PI_DIR $BKUP_DIR rsync -uvr pi@$PI_ADDR:$PI_DIR $BKUP_DIR echo ... echo done. # play sound - optional afplay /System/Library/Sounds/Basso.aiff
Here is a typical run:
$./bkup-pi.sh
Backing up RPi #1...
pi@192.168.4.31's password:
receiving file list ... done
python/hello.py
python/hello.py~
python/cat9532/
python/cat9532/pi-cat9532.py~
python/cat9532/rpi-cat9532.py
python/cat9532/rpi-cat9532.py~
python/gpiotest/
python/gpiotest/testGPIO.py
python/i2ctest/
python/i2ctest/i2ctest.py
python/i2ctest/i2ctest.py~
python/mma7660/
python/mma7660/mma7455.c
python/mma7660/rpi-mma7660.c
python/mma7660/rpi-mma7660.py
python/mma7660/rpi-mma7660.py~
python/opencv/
python/opencv/ocvtest.py
python/servo-test/
python/servo-test/servo-test.py
python/servo-test/servo-test.py~
python/speech/
python/speech/hello.py~
python/speech/speech-test.py
python/speech/speech-test.py~
python/speech/test.py
python/speech/test.py~</p>
sent 502 bytes received 24631 bytes 4569.64 bytes/sec
total size is 23024 speedup is 0.92
...
done.
</code>