Para algunas pruebas requiero mantener sincronizados los archivos de un servidor web con otro, esto para levantar una replica, para hacer un tipo CDN y principalmente trastear con una maquina virtual que tengo en casa. Mi primera opción fue rscyn, facil efectivo y lo mejor casi lo conozco de memoria. Pero después de una breve búsqueda en la red me encontré unison.
Características de Unison
- Bajo licencia General Public License (GPL)
- Multiplataforma funciona en Linux, Unix, BSD, Windows, OSX
- Sincronizacion multiplataforma, es decir se puede sincronizar un windows con algún Linux
- Sincronización por SSH por lo que puede funcionar por internet sin problemas de seguridad.
Para esta pequeña guía utilizare Debian y Ubuntu, la carpeta a sincronizar sera /var/www
Instalación de Unison
Como en cada instalación nueva se recomiendo hacer actualizar
apt-get update apt-get dist-upgrade
Procedemos con la instalación
apt-get install -f unison
Como utilizarlo
La manera de utilizarlo es sencilla, basta con saber la sintaxis
unison ruta/absoluta/de/root Protocolo://[usuario]@hostremoto:Numerodepuerto//ruta/absoluta/de/root
Protocolo puede ser:
- file
- socket
- ssh
- rsh
El parámetro Usuario es opcional y solo es para especificar un usuario distinto para la conexión, en caso de que no sea el mismo de ambos lados
Hostremoto, la ip o nombre del equipo remoto a donde vamos enviar la replica
Numerodepuerto esto es sencillo, puerto por el cual escucha el servicio de Protocolo
Y el ultimo parámetro es la ruta absoluta iniciando con //
unison /var/www/blog ssh://192.168.30.20//var/www/blog
Al ejecutar nos solicitara la contraseña del equipo remoto, la primera vez nos mostrara un mensaje como este, el cual nos indica que es la primera vez que lo vamos o que se a actualizado
Looking for changes Warning: No archive files were found for these roots, whose canonical names are: /home/server/Desktop /home/server/desk-back This can happen either because this is the first time you have synchronized these roots, or because you have upgraded Unison to a new version with a different archive format. Update detection may take a while on this run if the replicas are large. Unison will assume that the 'last synchronized state' of both replicas was completely empty. This means that any files that are different will be reported as conflicts, and any files that exist only on one replica will be judged as new and propagated to the other replica. If the two replicas are identical, then no changes will be reported.If you see this message repeatedly, it may be because one of your machines is getting its address from DHCP, which is causing its host name to change between synchronizations. See the documentation for the UNISONLOCALHOSTNAME environment variable for advice on how to correct this. Donations to the Unison project are gratefully accepted: http://www.cis.upenn.edu/~bcpierce/unison Press return to continue.[]
Las siguientes preguntas serán similares a estas, nos preguntara que hacer con los archivos nuevos, la opcion [f] dejar que unison decida es la mejor opción para la primer sincronización
file ----> .htaccess [f] file ----> index.php [f] file ----> licencia.txt [f] file ----> license.txt [f] file ----> readme.html [f] file ----> wp-activate.php [f] dir ----> wp-admin [f]
y al final nos preguntara si deseamos propagar la actualización
Proceed with propagating updates? [] y
Aqui les muestro el log de mi primer ejecución
unison /var/www/blog ssh://192.168.30.20//var/www/blog Contacting server... The authenticity of host '192.168.30.20 (192.168.30.20)' can't be established. ECDSA key fingerprint is fd:bf:4b:45:02:ba:dd:12:d2:a0:63:e2:02:f0:5a:92. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.30.20' (ECDSA) to the list of known hosts. root@192.168.30.20's password: Contacting server... Connected [//hostdestino//var/www/blog -> //hostfuente//var/www/blog] Looking for changes Warning: No archive files were found for these roots, whose canonical names are: /var/www/blog /var/www/blog This can happen either because this is the first time you have synchronized these roots, or because you have upgraded Unison to a new version with a different archive format. Update detection may take a while on this run if the replicas are large. Unison will assume that the 'last synchronized state' of both replicas was completely empty. This means that any files that are different will be reported as conflicts, and any files that exist only on one replica will be judged as new and propagated to the other replica. If the two replicas are identical, then no changes will be reported.If you see this message repeatedly, it may be because one of your machines is getting its address from DHCP, which is causing its host name to change between synchronizations. See the documentation for the UNISONLOCALHOSTNAME environment variable for advice on how to correct this. Donations to the Unison project are gratefully accepted: http://www.cis.upenn.edu/~bcpierce/unison Press return to continue.[] ... ... Saving synchronizer state Warning: No archive files were found for these roots, whose canonical names are: /var/www/blog //hostdestino//var/www/blog This can happen either because this is the first time you have synchronized these roots, or because you have upgraded Unison to a new version with a different archive format. Update detection may take a while on this run if the replicas are large. Unison will assume that the 'last synchronized state' of both replicas was completely empty. This means that any files that are different will be reported as conflicts, and any files that exist only on one replica will be judged as new and propagated to the other replica. If the two replicas are identical, then no changes will be reported. If you see this message repeatedly, it may be because one of your machines is getting its address from DHCP, which is causing its host name to change between synchronizations. See the documentation for the UNISONLOCALHOSTNAME environment variable for advice on how to correct this. Donations to the Unison project are gratefully accepted: http://www.cis.upenn.edu/~bcpierce/unison Waiting for changes from server Reconciling changes local 192.168.... file ----> .htaccess [f] file ----> index.php [f] file ----> licencia.txt [f] file ----> license.txt [f] file ----> readme.html [f] file ----> wp-activate.php [f] dir ----> wp-admin [f] file ----> wp-blog-header.php [f] file ----> wp-comments-post.php [f] file ----> wp-config-sample.php [f] file ----> wp-config.php [f] dir ----> wp-content [f] file ----> wp-cron.php [f] dir ----> wp-includes [f] file ----> wp-links-opml.php [f] file ----> wp-load.php [f] file ----> wp-login.php [f] file ----> wp-mail.php [f] file ----> wp-settings.php [f] file ----> wp-signup.php [f] file ----> wp-trackback.php [f] file ----> xmlrpc.php [f] Proceed with propagating updates? [] y Propagating updates UNISON 2.40.102 started propagating changes at 16:19:17.73 on 30 Oct 2015 [BGN] Copying .htaccess from /var/www/blog to //hostdestino//var/www/blog [BGN] Copying index.php from /var/www/blog to //hostdestino//var/www/blog Shortcut: copied /var/www/blog/wp-includes/images/xit.gif from local file /var/www/blog/.unison.wp-admin.ba8d24c0f72663305beacb5de247c07e.unison.tmp/images/xit.gif [END] Copying wp-includes [END] Copying wp-content UNISON 2.40.102 finished propagating changes at 16:21:12.77 on 30 Oct 2015
Notas
Si deseas preservar el propietario del archivo, el grupo propietario deberás ejecutar unison con los siguientes parámetros.
- -owner: para respetar el usuario propietario
- -group: para respetar el grupo propietario
unison -owner -group /path ssh:host/path
Puedes agregar -auto para aceptar automáticamente la opción que elija unison siempre y cuando no genere conflictos
unison -auto
Si deseas correr todo desde algun script o crontab pues basta con agregar -batch
unison -batch
Notas finales
Unison tiene bastantes opciones de sincronización, es sencillo de utilizar y dispone de una GUI en debian y ubuntu.