Details of two-way sync between two Ubuntu machines
In a previous post I discussed my frustrations with trying to get Dropbox or Spideroak to perform BOTH encrypted remote backup and AND fast two way file syncing. This is the detail of how I set up for two machines, both Ubuntu 10.10, to perform two way sync where a file change on either machine will result in that change being replicated on the other machine.
I initially tried running Unison on BOTH my laptop and the server and had the server Unison set to sync with my laptop back through an SSH reverse proxy. After testing this for a while I discovered this is totally the wrong way to do it. The problem is that the Unison process makes temp directories and files in the file system of the target. So my Unison job on the laptop would be trying to syn files and, in the process, create temp files which would kick off a Unison sync on the sever which would make temp files on the laptop… I think you can see how convoluted this gets.
So a much better solution is to only run Unison from one machine (I chose my laptop) and have the other machine (server in my case) send an SSH command (over the aforementioned reverse proxy) to the laptop asking the laptop to kick off a Unison sync. This way all of the syncs happen from the laptop.
So, in short, both machines run lsyncd which monitors files for changes. I keep up an SSH tunnel with reverse port forwarding which forwards a remote machine port back to my laptop’s port 22 (SSH). Unison need be installed ONLY on my laptop. When a change happens on my laptop, lsyncd fires off a Unison sync from my laptop that syncs it with the server. When a file changes on the server, the lsyncd job on the server makes a connection to my laptop via ssh and fires off a Unsion sync between my laptop and the server.
Here’s an example of my lsyncd config scripts:
Laptop:
settings = {
logfile = “/home/jal/lsyncd/laptop/lsyncd.log”,
statusFile = “/home/jal/lsyncd/laptop/lsyncd.status”,
maxDelays = 15,
–nodaemon = true,
}runUnison2 = {
maxProcesses = 1,
delay = 15,
onAttrib = “/usr/bin/unison -batch /home/jal/Documents ssh://12.34.56.78//home/jal/Documents”,
onCreate = “/usr/bin/unison -batch /home/jal/Documents ssh://12.34.56.78//home/jal/Documents”,
onDelete = “/usr/bin/unison -batch /home/jal/Documents ssh://12.34.56.78//home/jal/Documents”,
onModify = “/usr/bin/unison -batch /home/jal/Documents ssh://12.34.56.78//home/jal/Documents”,
onMove = “/usr/bin/unison -batch /home/jal/Documents ssh://12.34.56.78//home/jal/Documents”,
}sync{runUnison2, source=”/home/jal/Documents”}
Server:
settings = {
logfile = “/home/jal/lsyncd/server/lsyncd.log”,
statusFile = “/home/jal/lsyncd/server/lsyncd.status”,
maxDelays = 15,
–nodaemon = true,
}runUnison2 = {
maxProcesses = 1,
delay = 15,
onAttrib = “ssh localhost -p 5432 unison -batch /home/jal/Documents ssh://12.34.56.78//home/jal/Documents”,
onCreate = “ssh localhost -p 5432 unison -batch /home/jal/Documents ssh://12.34.56.78//home/jal/Documents”,
onDelete = “ssh localhost -p 5432 unison -batch /home/jal/Documents ssh://12.34.56.78//home/jal/Documents”,
onModify = “ssh localhost -p 5432 unison -batch /home/jal/Documents ssh://12.34.56.78//home/jal/Documents”,
onMove = “ssh localhost -p 5432 unison -batch /home/jal/Documents ssh://12.34.56.78//home/jal/Documents”,
}sync{runUnison2, source=”/home/jal/Documents”}
Keep in mind that I am using version 2 of lsyncd which can be downloaded here: http://code.google.com/p/lsyncd/
The version of lsyncd available in the Ubuntu repo is version 1.x which does not use the same config format as I illustrate above. However, if you run into dependency issues with v2, the easiest thing to do is install the repo version which will install dependencies and then manually download and install v2 from the above URL.
My reverse port forwarding set up looks like this:
autossh -2 -4 -X -R 5432:localhost:22 12.34.56.78
the -R bit forwards remote port 5432 to my laptop’s port 22 which is the ssh. So on my server if I run ssh localhost -p 5432 what actually happens is I am sshing from the remote machine to my laptop.
Notes:
- The IP address of my server in this example is 12.34.56.78.
- Don’t try and sync the directories where the lsyncd logs are kept. That will results in an endless sync cycle as each machine keeps noticing changes endlessly. Don’t ask me how I know this.
- The command to start the sync on the laptop is “lsyncd /home/jal/lsyncd/laptop/configfile” where configfile is the above lsyncd configuration file.
- lsyncd could, conceivably, tell Unison to sync only the part of the directory tree that changed. I have not been able to make that feature work right, however. And it only takes Unison a few seconds to sync, so I’ve not worried about it.
This has greatly sped up my RStudio based workflow when doing analysis with R. Now when I change files on my server using RStudio they are immediately (well it waits 15 seconds) replicated to my local machine and vice versa!
Good luck and if you have any suggestions please post a comment!
[...] In a follow up post I’ll put of the details of this configuration. Stay tuned. (EDIT: Update posted!) [...]
JD –
This helped me out immensely. I too am a bit leery of Dropbox, despite the ability to use truecrypt (which results in a rather clumsy method of syncing) and I wish Spideroak would provide more of a “realtime” syncing solution (I’ll still use SO for general backups as it’s really good at that). This fits the bill perfectly. Perhaps if I’m feeling adventurous tonight i’ll see if i can’t get things running on Cygwin. Many thanks again.
great job. i’m implementing this and needed the configs for lsyncd.
thank you
Hi,
Great Post!
Could you explain the arguments in the autossh command? The version I downloaded from http://www.harding.motd.ca/autossh/ doesn’t have these options.
Thanks,
Aditya
Aditya, the arguments I passed to autossh are actually ssh commands which it will pass along to ssh. Here’s an explination:
autossh -2 -4 -X -R 5432:localhost:22 12.34.56.78
-2: force SSH2 to prevent falling back to SSH1 which is less secure
-4: Force IPV4. I had problems on my system with IPV6 becoming the default. This overrides that behavior.
-R reverse port forward. Forwards port 22 on the remote machine to port 5432 on the local machine
you can get more details in the ssh man file. Feel free to give me a yell if any of that does not make sense.
Ah! ok…. The latest version of autossh requires a mandatory -M argument which got me confused..
Thanks for the quick reply!
JD,
Linux Journal [mentioned](http://www.linuxjournal.com/content/drop-your-dropbox-and-sparkleshare-instead) sparkleshare:
http://sparkleshare.org/
Wondering on your thoughts. I plan to implement one of these solutions in October.
Thank you for the tutorial. Of all the Dropbox alternatives I’ve tried (sparkleshare included) this setup has worked the best so far. Currently I’m trying to get a third machine into the mix. If I can manage to get it working, would you be willing to update the tutorial with that information?
I got three way syncing to work! I’ll have a blog post (with a backlink to your article) written up soon.
@JD I’m finally testing this out. I’m not even going to bother with 2-way sync yet; just one way for now (laptop to server) for having a current backup 24/7.
I installed lsyncd 2.04 and unison on my laptop. I use a nearly identical config file as you. I then issue the command
$ lsyncd ~/Documents/lsyncd.laptop.config
and the daemon starts.
No files transfer over to the server. If I create a foo.txt in my directory, then the status file and log file gets created. In the log, I get:
Mon Jul 11 14:39:15 2011 Normal: Event Delete spawns action ‘/usr/bin/unison -batch /home/vinh/Documents ssh://server.ip//home/vinh/BackupLaptop/Documents’
Contacting server…
bash: unison: command not found
Fatal error: Lost connection with the server
Mon Jul 11 14:39:15 2011 Normal: Finished Delete on /home/vinh/Documents//.#foo.txt = 3
Mon Jul 11 14:39:16 2011 Normal: Event Create spawns action ‘/usr/bin/unison -batch /home/vinh/Documents ssh://server.ip//home/vinh/BackupLaptop/Documents’
Contacting server…
bash: unison: command not found
Fatal error: Lost connection with the server
Mon Jul 11 14:39:16 2011 Normal: Finished Create on /home/vinh/Documents//foo.txt = 3
Do you know what I’m doing wrong?
$ which unison
/usr/bin/unison
$ killall lsyncd
Thanks!
@JD NM, Unison also needs to be installed on the server.
@vihn, you are correct. That’s a good clarification which I’ll make in the post. Did that solve your issue?
@JD I did manage to run unison by itself to make an initial first backup. I then ran started lsyncd with options identical to yours (just doing one way sync for now). I then modified some files. The status file:
Sync1 source=/home/vinh/Documents/
There are 86 delays
active Delete /.#foo.txt
wait Move /foo.txt -> /foo.txt~
block Create /foo.txt
wait Modify /UCI/2011DiscreteGroupSeq/#NguyenGillen_DiscreteGroupSeq.tex#
block Delete /.#foo.txt
wait Delete /.#bar.txt
wait Create /bar.txt
wait Create /Backup/SysFiles/hosts-201107121305
wait Modify /Backup/SysFiles/hosts
wait Create /Backup/SysFiles/fstab-201107121305
…
However, I’m expecting the sync to happen, but I don’t see anything updated on the server side. Am I doing something wrong?
Also, how come unison does not preserve dates on the server side? This is very non-ideal.
Vinh, can you double check that you’re using the “-batch” switch in your Unison command in your lsyncd config file?
@JD yes, it is using -batch. It is nearly identical to your posted config. Only difference is the URL to the server.
Vinh, it took me a second to understand what you’re seeing. I think those comments are lsyncd saying there’s a delay, not unison. So that makes me think lsyncd is not firing off the unison command.
Things to check:
1) What’s the delay set to in the configuration file? I set mine to 15 most days, but as low as 1 for testing.
2) does the lsyncd log have anything interesting in it?
3) does your lsyncd.status file show that it’s watching the right files?
@JD Sorry I wasn’t clear. Yes, that was lsyncd’s status file. I even killed my lsyncd job and resubmitted it. lsyncd does fire off a unison command:
$ ps aux | grep unison
vinh 15684 0.0 0.0 13972 2792 ? S 13:31 0:00 /usr/bin/unison -batch /home/vinh/Documents ssh://my.ip//home/vinh/BackupLaptop/Documents
vinh 15685 0.0 0.0 40824 2376 ? S 13:31 0:00 ssh school.nguyenvq.com -e none unison -server
1) my delay is 15, identical to yours.
2) lsyncd log:
Tue Jul 12 13:31:48 2011 Normal: Event Delete spawns action ‘/usr/bin/unison -batch /home/vinh/Documents ssh://my.ip//home/vinh/BackupLaptop/Documents’
Contacting server…
Connected [//vqn-x6//home/vinh/BackupLaptop/Documents -> //vqnportege//home/vinh/Documents]
Looking for changes
3) log:
Lsyncd status report at Tue Jul 12 13:34:56 2011
Sync1 source=/home/vinh/Documents/
There are 50 delays
active Delete /.#foo.txt
wait Move /foo.txt -> /foo.txt~
block Create /foo.txt
…
Excluding:
nothing.
Inotify watching 3447 directories
1: /home/vinh/Documents/
…
However, on the server side, I don’t see anything updated.
Vinh, I checked my lsync.status after running for a day and I see that I have 41 delays. However all of the actions associated with these delays *seems* to have actually taken place. Since mine was working as expected I had not looked at the lsyncd.status file at all.
I did some testing this morning and I’m not sure what I learned, but I see what I observed:
I have a synced folder called “euler” so I used Nautilus to copy it to a new directory. Nautilus defaults to “euler (copy)” as the name, which I then changed to “euler2″. It took about a minute, but my files synced to my remote machine. I then looked at my lsyncd.status and I see the following:
cat lsyncd.status | more
Lsyncd status report at Wed Jul 13 10:08:42 2011
Sync1 source=/home/jal/Documents/
There are 49 delays
active Create /euler (copy)/
block Create /euler (copy)/22.txt
block Create /euler (copy)/8.R
block Create /euler (copy)/9.R
block Create /euler (copy)/10.R
block Create /euler (copy)/11.R
block Create /euler (copy)/1.R
block Create /euler (copy)/2.R
block Create /euler (copy)/3.R
block Create /euler (copy)/11.dat
block Create /euler (copy)/12.R
block Create /euler (copy)/13.dat
block Create /euler (copy)/13.R
block Create /euler (copy)/14.R
block Create /euler (copy)/16.R
block Create /euler (copy)/17.R
block Create /euler (copy)/20.R
block Create /euler (copy)/4.R
block Create /euler (copy)/5.R
block Create /euler (copy)/6.R
block Create /euler (copy)/7.R
block Create /euler (copy)/21.R
block Create /euler (copy)/28.R
block Create /euler (copy)/22.R
block Delete /euler (copy)/
wait Create /euler2/
block Create /euler2/9.R
block Create /euler2/8.R
block Create /euler2/7.R
block Create /euler2/6.R
block Create /euler2/1.R
block Create /euler2/11.dat
block Create /euler2/4.R
block Create /euler2/3.R
block Create /euler2/2.R
block Create /euler2/10.R
block Create /euler2/22.R
block Create /euler2/12.R
block Create /euler2/11.R
block Create /euler2/21.R
block Create /euler2/20.R
block Create /euler2/13.R
block Create /euler2/14.R
block Create /euler2/16.R
block Create /euler2/17.R
block Create /euler2/22.txt
block Create /euler2/28.R
block Create /euler2/5.R
block Create /euler2/13.dat
Excluding:
nothing.
…
Oddly enough, at some point it fired off Unison because the files synced.
I have no idea what’s happening and why. I’ll keep playing with this. But I wanted to post a comment to let you know that I’m seeing much of what you see in the lsyncd.status however, mine is still firing off Unison.
I bet if you after you copied that euler directory and ran “ps aux | grep unison”, you’ll see your unison job immediately. For me, I’m not sure when the unison job will start syncing the files.
– Vinh
So, any updates? For me, lsyncd does indeed fire off unison, and I do see the unison job. However, it appears no file gets transferred.
$ unison -version
unison version 2.32.52
$ lsyncd -version
Version: 2.0.4
Any thoughts?
Vinh, I’m flummoxed. I’m running the same version of unison and lsyncd as you. But if you are sure Unison is being fired off, then my logic says either:
A) The lsyncd config file is firing off Unison with malformed params
or
B) Something is wrong with unison on either the desktop or server.
Some possible tests:
In the lsyncd config file located on your local machine, copy and paste the onCreate command line from the config file into the terminal window and make sure it runs. It should run verbatim and need no elevated privileges.
it should look something like “/usr/bin/unison -batch /home/you/Documents ssh://server.ip//home/you/Documents”
make sure that copies files and does not complain of files out of sync or such.
You should check your unison logs on the desktop to see if any info is there. something like tail ~/.unison/unison.log
You could also watch connections on the server side and see if the Unison process on your desktop is connecting to the server. That might give some clues.
Let me know if these ideas shed any light!
I did try the single command from my lsyncd config file to get the initial sync going, so it works.
Also, according to lsync.log file that I pasted earlier, it does show that unison has been executed:
Fri Jul 15 14:00:10 2011 Normal: Event Create spawns action ‘/usr/bin/unison -batch -times /home/vinh/Documents ssh://my.ip//home/vinh/Backup/Documents’
Contacting server…
Connected [//vqn-x6//home/vinh/Backup/Documents -> //vqnportege//home/vinh/Documents]
Looking for changes
Seems like its stuck at looking for changes. I’m also sure that an ssh is made and unison is executed on the server using “ps aux | grep unison”.
Can I have your email address so I can send you my config file? Or can you email me? You should have my address in wordpress.
Since lsyncd isn’t working for me, I found a solution based on the bleeding edge version of Unison, which includes a feature for monitoring files. I outlined it [here](http://blog.nguyenvq.com/2011/07/18/real-time-file-synchronization-like-dropbox-via-unison/).
Hello,
do you manage to have lsync aggregate events ?
On my machine it spawns unison for every single file I change, so if I deleted a folder with 100 files it will span unison 100 times !
Do you have such issue ?
Thanks
Bruno, I set a wait period of ~ 15 seconds. It appears that having a wait period works like an aggregation. I’m not seeing a huge number of Unison events stack up. This may be some logic in Unison that’s helping me out. Are you seeing Unison run 100 times in parallel or is it running unison 100 times in series? It may be possible that I’m getting Unison events in series and I don’t realize it.
They are launched in series not parallel.
I also found out that if you create / delete / recreate a file within the agregate period, only on unison shell is spawned.
This tend to show that events are aggregated when they belong to the same file.
Different file events for files in the same folder do not agregate though.
This is no blocking issue because it works, but still having unison launched for nothing is just not ideal.
Bruno, I bet this is happening to me and I didn’t realize it. I suspect I just saw Unison running and assumed that was the daemon. I agree it’s less than ideal.
Hello, very nice and interesting article!! I have followed your tutorial in order to apply it in two servers but i have a problem with lsyncd daemon in /init.d/. I have Ubuntu server (oneiric) installed.
My problem is that even if i have set up private keys for ssh in both servers, i get the following error in lsyncd.log file:
Contacting server…
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,gssapi-with-mic,password).
Fatal error: Lost connection with the server
It seems strange since when i run the command of the lsyncd.config file (i.e /usr/bin/unison -batch /home/jal/Documents ssh://12.34.56.78//home/jal/Documents) it connects to ssh without password (using ssh keys) and performs the synchronization just fine.
Any ideas?
P.S my ~/.ssh permissions are 755
Ugh… nothing jumps out at me as to why this is happening. Sorry that I’m not much help. Try looking at the lsyncd logs and possibly upping the level of verbosity of the logging.
Thanks for replying so fast, i have already found a solution to this. So i post it in case come in handy to someone else. The problem was that the lsyncd daemon is launched using root priviledges. So ssh was preventing access to my .ssh rsa files of my simple user account.
I just generated ssh keys for root account like this:
$ ssh-keygen
(type enter to all default options)
$ cat ~/.ssh/id_rsa.pub | ssh @ ‘cat >> .ssh/authorized_keys’
After that, your setup works like a charm!!!
[...] lsyncd to keep his R files (specifically, R Studio output) in sync with his local machine. Post 1. Post 2. At DSN, we use lsyncd to create a magic folder on our server that pushes R plots generated on the [...]
Nice tutorial, thanks, but I’d expect lsyncd to run continuously, but it seems it stops as soon as Unison is fired, even if I don’t really understand why :-S
Haven’t tried your solution yet but wanted to ask something before. My scenario is the following: I have one server on linode, a desktop and a laptop. Sometimes I work from my laptop and sometimes from the desktop. What I want to accomplish is to sync their Desktop folders while keeping a copy on the server. Is it doable with your solution? If so, I will improve your script to check if both computers are connected to the same network and then sync them locally.
Thanks in advance
I’ve since a long time experimented with this syncing setup and now use the following sysv init files on ubuntu, which makes automatic management of the syncing possible. I hope it can be of use to someone. lsyncd.conf is still identical and should be present on both systems.
—- Code starting for system 1 (client) here: —-
#!/bin/sh
#
### BEGIN INIT INFO
# Provides: lansync
# Required-Start: $ssh
# Short-Description: lansync init script
# Description: files synchronization using lsyncd and unison
### END INIT INFO
#
NAME=”lansync”
NICELEVEL=19
IONICECLASS=”idle”
export HOME=”/root”
DAEMON=”/usr/local/bin/lsyncd”
PIDFILE=”/var/run/$NAME.pid”
CONFIG_FILE1=”/etc/lansync/lsyncd.conf”
ROOT1=”/home/test/LanSync”
ROOT2=”ssh://serverip//home/test/LanSync”
LOG_FILE1=”/var/log/lansync.log”
if [ "$(id -u)" != "0" ]
then
log_failure_msg “You must be root to start, stop or restart $NAME.”
exit 1
fi
case “$1″ in
start)
echo -n “Starting $NAME daemon…”
echo -n “Starting $NAME daemon…” >> $LOG_FILE1
date >> $LOG_FILE1
start-stop-daemon –start –oknodo –nice $NICELEVEL –iosched $IONICECLASS \
–exec $DAEMON — $CONFIG_FILE1 -pidfile $PIDFILE
#debug alternative:
#$DAEMON $CONFIG_FILE1 -pidfile $PIDFILE
daemon — nice ionice -c 3 unison -batch -group -owner -ui text $ROOT1 $ROOT2 > $LOG_FILE1
#debug alternatives:
#unison -ui graphic -group -owner $ROOT1 $ROOT2
#unison -batch -group -owner -ui text $ROOT1 $ROOT2
echo “done”
exit 1
;;
stop)
echo -n “Stopping $NAME daemon…”
echo -n “Stopping $NAME daemon…” >> $LOG_FILE1
date >> $LOG_FILE1
start-stop-daemon –stop –oknodo –pidfile $PIDFILE –retry 5
rm -rf $PIDFILE
echo “done”
exit 1
;;
restart)
/etc/init.d/$NAME stop
/etc/init.d/$NAME start
exit 1
;;
*)
echo “Usage: /etc/init.d/$NAME {start|stop|restart}”
exit 1
esac
exit 0
—- END —-
—- Code starting for system 2 (server) here: —-
#!/bin/sh
#
### BEGIN INIT INFO
# Provides: lansync
# Required-Start: $ssh
# Short-Description: lansync init script
# Description: files synchronization using lsyncd and unison
### END INIT INFO
#
NAME=”lansync”
NICELEVEL=19
IONICECLASS=”idle”
export HOME=”/root”
DAEMON=”/usr/local/bin/lsyncd”
PIDFILE=”/var/run/$NAME.pid”
CONFIG_FILE1=”/etc/lansync/lsyncd.conf”
ROOT1=”/home/test/LanSync”
ROOT2=”ssh://clientip//home/test/LanSync”
LOG_FILE1=”/var/log/lansync.log”
if [ "$(id -u)" != "0" ]
then
log_failure_msg “You must be root to start, stop or restart $NAME.”
exit 1
fi
case “$1″ in
start)
echo -n “Starting $NAME daemon…”
echo -n “Starting $NAME daemon…” >> $LOG_FILE1
date >> $LOG_FILE1
start-stop-daemon –start –oknodo –nice $NICELEVEL –iosched $IONICECLASS \
–exec $DAEMON — $CONFIG_FILE1 -pidfile $PIDFILE
#debug alternative:
#$DAEMON $CONFIG_FILE1 -pidfile $PIDFILE
daemon — ssh clientip nice ionice -c 3 \
unison -batch -group -owner -ui text $ROOT1 $ROOT2 > $LOG_FILE1
echo “done”
exit 1
;;
stop)
echo -n “Stopping $NAME daemon…”
echo -n “Stopping $NAME daemon…” >> $LOG_FILE1
date >> $LOG_FILE1
start-stop-daemon –stop –oknodo –pidfile $PIDFILE –retry 5
rm -rf $PIDFILE
echo “done”
exit 1
;;
restart)
/etc/init.d/$NAME stop
/etc/init.d/$NAME start
exit 1
;;
*)
echo “Usage: /etc/init.d/$NAME {start|stop|restart}”
exit 1
esac
exit 0
—– END —-
This tip is still valuable. Since long I’ve used this syncing setup with reasonable succes. Over time I’ve developed the sysv init script as below. It allows automatic starting of the sync at boot and works reasonably well for me. I hope it can be of use to someone.
— /etc/init.d/lansync on client —
#!/bin/sh
#
### BEGIN INIT INFO
# Provides: lansync
# Required-Start: $ssh
# Short-Description: lansync init script
# Description: files synchronization using lsyncd and unison
### END INIT INFO
#
NAME=”lansync”
NICELEVEL=19
IONICECLASS=”idle”
export HOME=”/root”
DAEMON=”/usr/local/bin/lsyncd”
PIDFILE=”/var/run/$NAME.pid”
CONFIG_FILE1=”/etc/lansync/lsyncd.conf”
ROOT1=”/home/testuser/LanSync”
ROOT2=”ssh://serverip//home/testuser/LanSync”
LOG_FILE1=”/var/log/lansync.log”
if [ "$(id -u)" != "0" ]
then
log_failure_msg “You must be root to start, stop or restart $NAME.”
exit 1
fi
case “$1″ in
start)
echo -n “Starting $NAME daemon…”
echo -n “Starting $NAME daemon…” >> $LOG_FILE1
date >> $LOG_FILE1
start-stop-daemon –start –oknodo –nice $NICELEVEL –iosched $IONICECLASS \
–exec $DAEMON — $CONFIG_FILE1 -pidfile $PIDFILE
#debug alternative:
#$DAEMON $CONFIG_FILE1 -pidfile $PIDFILE
daemon — nice ionice -c 3 unison -batch -group -owner -ui text $ROOT1 $ROOT2 > $LOG_FILE1
#debug alternatives:
#unison -ui graphic -group -owner $ROOT1 $ROOT2
#unison -batch -group -owner -ui text $ROOT1 $ROOT2
echo “done”
exit 1
;;
stop)
echo -n “Stopping $NAME daemon…”
echo -n “Stopping $NAME daemon…” >> $LOG_FILE1
date >> $LOG_FILE1
start-stop-daemon –stop –oknodo –pidfile $PIDFILE –retry 5
rm -rf $PIDFILE
echo “done”
exit 1
;;
restart)
/etc/init.d/$NAME stop
/etc/init.d/$NAME start
exit 1
;;
*)
echo “Usage: /etc/init.d/$NAME {start|stop|restart}”
exit 1
esac
exit 0
— End —
— /etc/init.d/lansync on server —
#!/bin/sh
#
### BEGIN INIT INFO
# Provides: lansync
# Required-Start: $ssh
# Short-Description: lansync init script
# Description: files synchronization using lsyncd and unison
### END INIT INFO
#
NAME=”lansync”
NICELEVEL=19
IONICECLASS=”idle”
export HOME=”/root”
DAEMON=”/usr/local/bin/lsyncd”
PIDFILE=”/var/run/$NAME.pid”
CONFIG_FILE1=”/etc/lansync/lsyncd.conf”
ROOT1=”/home/testuser/LanSync”
ROOT2=”ssh://serverip//home/testuser/LanSync”
LOG_FILE1=”/var/log/lansync.log”
if [ "$(id -u)" != "0" ]
then
log_failure_msg “You must be root to start, stop or restart $NAME.”
exit 1
fi
case “$1″ in
start)
echo -n “Starting $NAME daemon…”
echo -n “Starting $NAME daemon…” >> $LOG_FILE1
date >> $LOG_FILE1
start-stop-daemon –start –oknodo –nice $NICELEVEL –iosched $IONICECLASS \
–exec $DAEMON — $CONFIG_FILE1 -pidfile $PIDFILE
#debug alternative:
#$DAEMON $CONFIG_FILE1 -pidfile $PIDFILE
— /etc/init.d/lansync on client —
#!/bin/sh
#
### BEGIN INIT INFO
# Provides: lansync
# Required-Start: $ssh
# Short-Description: lansync init script
# Description: files synchronization using lsyncd and unison
### END INIT INFO
#
NAME=”lansync”
NICELEVEL=19
IONICECLASS=”idle”
export HOME=”/root”
DAEMON=”/usr/local/bin/lsyncd”
PIDFILE=”/var/run/$NAME.pid”
CONFIG_FILE1=”/etc/lansync/lsyncd.conf”
ROOT1=”/home/testuser/LanSync”
ROOT2=”ssh://serverip//home/testuser/LanSync”
LOG_FILE1=”/var/log/lansync.log”
if [ "$(id -u)" != "0" ]
then
log_failure_msg “You must be root to start, stop or restart $NAME.”
exit 1
fi
case “$1″ in
start)
echo -n “Starting $NAME daemon…”
echo -n “Starting $NAME daemon…” >> $LOG_FILE1
date >> $LOG_FILE1
start-stop-daemon –start –oknodo –nice $NICELEVEL –iosched $IONICECLASS \
–exec $DAEMON — $CONFIG_FILE1 -pidfile $PIDFILE
#debug alternative:
#$DAEMON $CONFIG_FILE1 -pidfile $PIDFILE
daemon — ssh clientip nice ionice -c 3 \
unison -batch -group -owner -ui text $ROOT1 $ROOT2 > $LOG_FILE1
echo “done”
exit 1
;;
stop)
echo -n “Stopping $NAME daemon…”
echo -n “Stopping $NAME daemon…” >> $LOG_FILE1
date >> $LOG_FILE1
start-stop-daemon –stop –oknodo –pidfile $PIDFILE –retry 5
rm -rf $PIDFILE
echo “done”
exit 1
;;
restart)
/etc/init.d/$NAME stop
/etc/init.d/$NAME start
exit 1
;;
*)
echo “Usage: /etc/init.d/$NAME {start|stop|restart}”
exit 1
esac
exit 0
— End —
echo “done”
exit 1
;;
stop)
echo -n “Stopping $NAME daemon…”
echo -n “Stopping $NAME daemon…” >> $LOG_FILE1
date >> $LOG_FILE1
start-stop-daemon –stop –oknodo –pidfile $PIDFILE –retry 5
rm -rf $PIDFILE
echo “done”
exit 1
;;
restart)
/etc/init.d/$NAME stop
/etc/init.d/$NAME start
exit 1
;;
*)
echo “Usage: /etc/init.d/$NAME {start|stop|restart}”
exit 1
esac
exit 0
— End —