Outils pour utilisateurs

Outils du site


issue62:c_c

Ceci est une ancienne révision du document !


DRAFT

Before I begin with the actual article, I thought it might be useful to address a question I got via email. A reader was asking where best to place my wallpapers.sh script, and, in doing so, reminded me that I haven't ever explained how best to go about this. My approach is to store all my scripts in a single folder (either a folder called “scripts,” or, if I want to save some visual space in my home folder, I'll call it .bin or .scripts so the folder is hidden). Make sure any script you place here is executable (chmod +x), and that they specify in the first line which interpreter is required (#!/bin/bash, #!/usr/env python, etc.) - otherwise calling it will not work. Once that is done, you could create a symbolic link to the script in /usr/bin with sudo ln -s /path/to/script /usr/bin/script

Avant de commencer avec le sujet de cet article, je pense qu'il serait utile de répondre à une question que j'ai reçue par email. Un lecteur demandait le meilleur endroit pour placer mon script wallpapers.sh, et, ce faisant, il m'a rappelé que je n'avais jamais expliqué comment faire cela au mieux. Mon approche est de placer tous mes scripts dans un dossier unique (par exemple un dossier nommé “scripts”, ou, si je veux épargner un peu de place “visuelle” dans mon dossier personnel, je l'appelle .bin ou .scripts et le dossier est alors caché. Assurez-vous que chaque script que vous placez à cet endroit soit exécutable (chmod +x), et que la première ligne du fichier précise bien l'interpréteur requis (#!/bin/bash, #!/usr/env python, etc.) - sinon tout appel au script échouera. Une fois cela terminé, vous pouvez créer un lien symbolique vers le script dans /usr/bin avec

sudo ln -s /path/to/script /usr/bin/script

However, this can be complicated if you start removing scripts (it will leave broken links in your /usr/bin directory). Another method is to add the folder into your $PATH variable. This can be done with export PATH=$PATH:<folder>

Cependant, cela peut se compliquer si vous commencez à supprimer des scripts (cela laissera des liens brisés dans votre dossier /usr/bin). Une autre approche est d'ajouter le dossier à votre variable $PATH. Cela peut se faire avec

export PATH=$PATH:<folder>

(replace folder with the actual path to the folder you created). To make it permanent, you can either place it in your .bashrc, your .xinitrc (if you start your interface with startx), or in any other script that gets called when you log in. I've had some hit-and-miss experiences with this, but generally /etc/environment works well. Since, however, the PATH is created in /etc/profile, you can also simply add the path to the scripts folder at the end of this path. I recommend avoiding any files in /etc/, and instead organize all your scripts within $HOME. This is due to the problems that can arise when creating system-wide settings using files that not all users can access. The problem is that anything in $HOME is restricted access wise to your user account. And now, on to the actual article.

(remplacez “folder” par le chemin complet vers le dossier que vous avez créé). Pour rendre la modification permanente, vous pouvez la placer au choix dans votre .bashrc, votre .xinitrc (si vous démarrez votre interface graphique avec startx), ou dans n'importe quel autre script appelé lors de votre ouverture de session. J'ai expérimenter cela avec plus ou moins de succès, mais habituellement /etc/environment fonctionne bien. Comme le PATH est créé dans /etc/profile, vous pouvez aussi tout simplement ajouter le chemin vers le dossier des scripts à la fin de ce path. Je recommande d'éviter tout fichier dans /etc/, et plutôt d'organiser tous vos scripts dans $HOME. C'est lié aux problèmes qui peuvent se produire lors de la création de configurations système qui utiliseraient des fichiers auxquels tous les utilisateurs n'ont pas accès. Le problème : tout ce qui se trouve dans $HOME est limité à votre compte utilisateur. Et maintenant, revenons au sujet de cet article.

The rest of my family has recently moved over to MacBooks for mobile workstations, and, for most of them, keeping files organized is fairly easy. For one of them, the MacBook is the only computer, and, for the other, it's a work machine where only company-related files need to be synchronized. However, for the latter person, whose PC runs both Arch Linux and Windows, it's a little more complicated. At first, I thought I'd use rsync in a custom script to synchronize the folders one after the other, but, since changes may be made on either machine, I needed a robust method to synchronize them (including deleted files). Rsync may be able to do this, but I have yet to get it working at 100%. As such, I began looking at unison again, which has resulted in me adopting it for my backup needs as well. Below, I describe my method for configuring Unison (which is available from the universe repository).

Les autres membres de ma famille sont passés récemment sur des portables MacBooks, et, pour la plupart d'entre eux, conserver l'organisation des fichiers est facile. Pour l'un d'eux, le MacBook est son seul ordinateur, et, pour l'autre c'est une machine professionnelle où seulement les fichiers de la société doivent être synchronisés. Néanmoins, pour cette dernière personne, dont le PC tourne sous Arch Linux et Windows, c'est un peu plus compliqué. En premier, j'ai pensé utiliser rsync dans un script personnalisé pour synchroniser les dossiers les uns après les autres, mais, comme les modifications peuvent se faire sur n'importe quelle machine, j'avais besoin d'un méthode robuste pour les synchroniser (y compris pour les fichiers supprimés). Rsync peut le faire, mais je dois encore l'amener à remplir cette tâche à 100%. En conséquence, j'ai à nouveau regardé unison, ce qui a eu pour résultat que je l'ai adopté aussi pour mes propres besoins de backup. Ci-dessous, je décris ma méthode pour configurer Unison (qui est disponible dans le dépôt universe).

From the Unison manual “Unison is a file-synchronization tool for Unix and Windows. It allows two replicas of a collection of files and directories to be stored on different hosts (or different disks on the same host), modified separately, and then brought up to date by propagating the changes in each replica to the other.”

D'après le manuel d'Unison : “Unison est un utilitaire de synchronisation de fichiers pour Unix et Windows”. Il permet de maintenir deux copies identiques d'une collection de fichiers et de répertoires sur différents hôtes (ou différents disques sur le même hôte), modifiés séparément, qui seront ensuite mis à jour par propagation des modifications de chaque copie vers l'autre.

To synchronize across multiple machines, the first thing you need to do is configure SSH on one of the machines (I did this on my mother’s PC, to minimize any possible security holes which would arise from a laptop with SSH enabled in public networks). It is also extremely important that you enable key-based logins on SSH. This is fairly easy with the following two commands: ssh-keygen -b 521 -t ecdsa -C“$(id -un)@$(hostname)-$(date –rfc-3339=date)”

Pour synchroniser des machines différentes, la première étape nécessaire est la configuration de SSH sur l'une des machines (J'ai choisi le PC de ma maman, pour minimiser les risques de trous de sécurité possibles sur un portable connecté sur un réseau public avec SSH activé). Il est aussi très important d'activer la connexion SSH basée sur une clé. C'est très simple avec les deux commandes suivantes :

ssh-keygen -b 521 -t ecdsa -C“$(id -un)@$(hostname)-$(date –rfc-3339=date)”

This creates the key. Follow the on-screen instructions. Keep in mind that entering a passphrase requires you to type this in when you use it – which defeats the point of a key-based login.

Cela va créer la clé. Suivez les instructions à l'écran. N'oubliez pas que si vous définissez une passphrase, vous devrez l'introduire lors de l'utilisation de la clé - ce qui réduit l'intérêt d'une connexion basée sur clé.

scp ~/.ssh/id_ecdsa.pub username@remote-server.org:~/.ssh/authorized_keys This will copy the file into the authorized keys of the server, meaning that if you try ssh <IP>, without a username, it will automatically log you in using the key, without prompting for a password. Note: If your version of ssh doesn't support ecdsa, rsa keys are fine too.

scp ~/.ssh/id_ecdsa.pub username@remote-server.org:~/.ssh/authorized_keys

Copie du fichier de clé publique dans le fichier des clés autorisées sur le serveur, cela signifie que si vous essayez ssh <IP>, sans non d'utilisateur, cela vous connectera automatiquement en utilisant la clé, sans demander de mot de passe. Remarque : si votre version de ssh ne supporte pas ecdsa, les clés rsa sont bien aussi.

Now then, on to Unison. The easiest way to manage multiple synchronization folders is to create multiple profiles (.prf files). Since the options are all the same, I will cover only a single example. My Music sync profile looks like this: # Unison preferences label = Music sync root = /home/lswest/Music root = ssh:user@IPhome/lswest/Music/Hyperion/ fastcheck = true dontchmod = true ignore = Name *.ini ignore = Name *.jpg ignore = Name *.jpeg sshargs = -C

Et maintenant, passons à Unison. La manière la plus simple de gérer des synchronisations de dossiers multiples et de créer plusieurs profils (fichiers .prf). Commes les options sont les mêmes, je vais expliquer un seul exemple. Mon profil de synchronisation musique ressemble à ceci :

# Unison preferences label = Music sync root = /home/lswest/Music root = ssh:user@IPhome/lswest/Music/Hyperion/ fastcheck = true dontchmod = true ignore = Name *.ini ignore = Name *.jpg ignore = Name *.jpeg sshargs = -C

From top to bottom, the settings do the following:

En partant du début vers la fin, les paramètres font :

label – assigns the name of the profile, for the GUI's list of profiles.

label - définit le nom du profil, pour la liste des profils dans l'IHM.

root – These are the sender/receiver (in that order). It shouldn't be possible to use more than 2 roots, though I haven't tested this.

root - Ce sont les source / destination (dans cet ordre). Il ne devrait pas être possible d'utiliser plus de 2 roots, bien que je n'ai pas testé cela.

fastcheck – I find that this reduces the search time immensely. It is supposed to be the default for Unix systems, but it doesn't hurt to be certain. The way this works is that it reads a file's modification times during the first pass, so it can ignore any files that weren't changed (meaning the modification times are the same as what was stored in the database). On the second pass. it generates a fingerprint. and compares it to the last contents to be synchronized.

fastcheck - Je trouve que cela réduit très significativement le temps de recherche. C'est supposé être par défaut pour les systèmes Unix, mais cela ne fait pas de mal de préciser. Le principe de fonctionnement est qu'il lit l'heure de modification du fichier en première passe, ainsi il peut ignorer les fichiers qui n'ont pas été modifiés (heure de modification identique du fichier dans la base de données). En seconde passe, il génère une signature et la compare au dernier contenu à synchroniser.

dontchmod – Unison tries to keep permissions the same, which is fine for syncs with Linux/Unix systems on either end, but for anything stored on NTFS (or with a Windows server), you will need to turn this off, to prevent dozens of permission warnings. Leaving this option isn't bad, as the permission settings simply fail, but it does slow down the transfers.

dontchmod - Unison essaie de conserver des permissions identiques, ce qui est bien lors de synchronisations entre système Linux/Unix, mais pour n'importe quoi stocké sur NTFS (ou sur un seveur Windows), vous devrez désactiver cette option, pour éviter des dizaines d'avertissements sur les permissions. Laisser cette option n'est pas un mauvais choix, le paramétrage de permission échoue, mais cela ralentit les transferts.

ignore – This option tells Unison what files to ignore. In my case, I ignore all the .ini files that Windows is so fond of creating (since my media is on an NTFS hard drive shared between Windows and Arch on my PC), and any of the cover art, since I use a different method in Linux.

ignore - Cette option indique à Unison quels fichiers ignorer. Dans mon cas, j'ignore tous les fichiers .ini que Windows prend un malin plaisir à créer (mon média est sur un disque dur NTFS partagé entre Windows et Arch sur mon PC), ainsi que les pochettes parce que j'utilise une autre méthode sous Linux.

sshargs – passes arguments to the ssh session. The option “-C” tells SSH to compress any of the information being sent, theoretically reducing transfer time per file (I haven't tested to see if this slows the entire process down, since the files need to be compressed, but for my rather large Music folder, it doesn't take too long to synchronize the changes these days).

sshargs - fournit les arguments à la session ssh. L'option “-C” indique à SSH de compresser les informations envoyées, théoriquement cela réduit le temps de transfert par fichier (Je n'ai pas testé pour vérifier si cela ralentit tout le processus, puisque les fichiers doivent être compressés, mais pour mon dossier de musique de taille modérée, cela ne prend pas trop de temps pour synchroniser les modifications en ce moment).

Keep in mind that the first run-through will take a while, since Unison has to assume every file is new.

N'oubliez pas que la première exécution prendra du temps, puisque Unison supposera que chaque fichier est nouveau.

The first few times you run Unison, I highly recommend manually checking the sync settings for each file that pops up (options are: right to left, left to right, skip), to make sure you're not losing/gaining any files you don't want. Once you're satisfied with how Unison is handling it, you can configure it to automatically accept non-conflicting changes. Do this by adding “auto = true” to the .prf file. You can also have Unison accept the non-conflicting changes, while skipping any conflicts with “batch = true”. Whether or not you can make this a completely automatic process, I'm not sure. However, you can minimize the input that is required using the above steps. To find out more about Unison, check the Further Reading section for a link to the user manual. If you want to simply mirror a folder in one direction (i.e. copy it from one machine to the next, without synchronizing), then I would still highly recommend rsync instead, due to the fact that it doesn't require any input when run properly. It's also extremely well documented in the manpage, making it quite easy to use. As a final note, I want to point out that both Unison and Rsync work for local folders too, meaning you can use it to create backups on a USB drive as well. If you decide to run these commands automatically on a schedule, I would recommend creating a script to first check if the USB drive is connected, or to check that you have the correct remote IP address (or can ping the router's IP). This is to avoid using resources to run a command that will simply fail. It also avoids opening SSH connections with computers that have the same IP but aren't your intended target, which could lead to problems down the line.

Les quelques premières fois que vous exécuterez Unison, je vous recommande vivement de verifier manuellement les paramètres de synchronisation pour chaque fichiers qui apparait (les options sont : de droite à gauche, de gauche à droite, ignorer), pour être certain de ne pas perdre ou gagner des fichiers que vous ne voulez pas.

to do

I hope at least some of you have found this interesting or helpful. I'm going to continue expanding my preferences files as I get more comfortable with Unison, and will mention any further tips I discover as time goes on. As always, if you have any questions, suggestions, or comments, you can reach me via email at lswest34@gmail.com. If you do decide to email me, please put “C&C”or “FCM” in the subject line, so that I don't overlook it.

Further Reading:

Unison Manual: http://www.cis.upenn.edu/~bcpierce/unison/download/releases/stable/unison-manual.html#usingit

Ubuntu Wiki page on SSH keys: https://help.ubuntu.com/community/SSH/OpenSSH/Keys

issue62/c_c.1345533762.txt.gz · Dernière modification : 2012/08/21 09:22 de thierry43