====== Realtime backup with inotify and rsync on Cubieboard ====== //__About this Article__// * **Author**: soloforce --- soloforce@126.com --- 2013/11/04 21:22 * **Copyrights**: [[http://creativecommons.org/licenses/by-sa/3.0/|CC Attribution-Share Alike 3.0 Unported]] * **Contributors**: [[http://cubieboard.org/|Cubieboard Community]] : ... ===== Abstract ===== In this tutorial, we introduce a method for realtime data-backup from/to a Cubieboard Linux system. This method is based on inotify and rsync which are rather usual in modern Unix/Linux systems. Cubieboard is small in size, low in power consuming and quiet enough, all of these make it suitable running some kind of daemon process in the room corner. How about make Cubieboard a moderate safe-box? Yes, that's we are talking about in this article. Assuming we have two machines here, one is a PC, the other one is a Cubieboard, both of them are running Linux systems. Now, we want realtime backup data from one machine to another, say, from PC to Cubieboard. Of course, you can alter their roles as you like, the mechanism is the same. ===== Inotify ===== Inotify is an API or some sort of mechanism in the Linux kernel, which could be leveraged by userspace programs for monitoring changes in file-system, such as creation, deletion, modification etc. We don't intend to describe inotify's details but concentrate on how to use it. First, install inotify-tools on the machine whose data need backup. $ sudo apt-get install inotify-tools Then, we will create a script-daemon, monitoring some file or folder on the machine. When changes detected, inotify will notify userspace applications (like some programs in inogify-tools), then these changes will be synchronized to Cubieboard. Here is the script named //inotify_rsync//, which mainly benefits from community work. Thanks go to the original author ((http://blog.leezhong.com/project/2010/12/13/inotify-rsync.html)): #!/bin/bash ########################### # script name: inotify_rsync # # sync[0], sync[1] are entries to be rsynced # local monitoring directory, target host(backup machine, say, Cubieboard), target rsync_module ( defined in /etc/rsyncd.conf on target host) # DONT add additional spaces near the commas. sync[0]='/home/soloforce/temp/rsync,10.0.0.10,test' sync[1]='/home/soloforce/temp/rsync2,10.0.0.10,test' ########################### for item in ${sync[@]}; do dir=`echo $item | awk -F"," '{print $1}'` host=`echo $item | awk -F"," '{print $2}'` module=`echo $item | awk -F"," '{print $3}'` inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f %e' \ --event CLOSE_WRITE,create,move,delete $dir | while read date time file event do echo $event'-'$file case $event in MODIFY|CREATE|MOVE|MODIFY,ISDIR|CREATE,ISDIR|MODIFY,ISDIR) if [ "${file: -4}" != '4913' ] && [ "${file: -1}" != '~' ]; then cmd="rsync -avz --exclude='*' --include=$file $dir $host::$module" # echo $cmd $cmd > /dev/null fi ;; MOVED_FROM|MOVED_FROM,ISDIR|DELETE|DELETE,ISDIR) if [ "${file: -4}" != '4913' ] && [ "${file: -1}" != '~' ]; then cmd="rsync -avz --delete-excluded --exclude="$file" $dir $host::$module" # echo $cmd $cmd > /dev/null fi ;; esac done & done Asign this script the necessary privileges, and run it if ready (not ready now, cauz target machine is not configured yet). $ chmod a+x $ ./inotify_rsync In order to make this script autorun while bootup, add this line to // /etc/rc.local // before "exit 0" sudo inotify_rsync ===== Rysnc ===== //rsync is a utility software and network protocol for Unix-like systems (with ports to Microsoft Windows and Apple Macintosh) that synchronizes files and directories from one location to another while minimizing data transfer by using delta encoding when appropriate.((http://en.wikipedia.org/wiki/Rsync))// OK, install this tool in the Cubieboard, so that it could receive change notifications of the PC. $ sudo apt-get install rsync And do the rsync modules configuration, create file ///etc/rsyncd.conf//, add following lines. uid=root gid=root [test] path=/home/rsync host allow = * read only = false Make it auto run while boot the Cubieboard. Modify file // /etc/default/rsync //, change this line RSYNC_ENABLE=false to RSYNC_ENABLE=true Then update rc.d files $ sudo update-rc.d rsync enable ===== Test ===== Make certain that both inotify_rsync script and target rsyncd are running, and then do some change in the script running side, meanwhile watch the changes on the rsync side. ===== References ===== * http://en.wikipedia.org/wiki/Inotify * http://en.wikipedia.org/wiki/Rsync * http://blog.leezhong.com/project/2010/12/13/inotify-rsync.html {{tag>Cubieboard Cubietruck}}