URL: https://linuxfr.org/forums/programmation-shell/posts/script-bash-tronquer-noms-de-fichiers-pour-ecryptfs Title: Script Bash, tronquer noms de fichiers pour eCryptFS Authors: Space_e_man Date: 2014年09月16日T13:04:14+02:00 License: CC By-SA Tags: bash, trunc, ecryptfs et optimisation Score: 3 Ce script fonctionne déjà ici en production. Je cherche à l'optimiser car je l'utilise depuis peu sur un plus gros dossier. Contexte : ========== Nous souhaitons effectuer des sauvegardes externes de sorte à prévenir l'incendie ou le cambriolage. Nous souhaitons que cette copie externe soit chiffrée. Actuellement, elle se trouve sur un disque externe mais sera bientôt synchronisée à partir d'une machine extérieur, via internet (rsync). eCryptfs est utilisé dans un premier temps pour obtenir une version chiffrée d'une sauvegarde locale. Mais eCryptfs limite à 110 caractères les noms de fichiers et dossiers. J'ai donc écrit un script qui parcours l'ensemble du dossier de sorte à tronquer si nécessaire le nom des fichiers et dossier trop long. Le Script : =========== Ça marche mais c'est très lent :( Auriez-vous des idée pour optimiser ce script ? :) spm_namelength_config.sh ```bash # Define here the maximum end of file name # to be consider as extension. # The truncated file name version will keep extension. # # Should be greater or equal to 3 # # Example: 6 # EXTENTION_MAX_SIZE=6 # Define the hash size for truncated file name. # Greater hash better avoid collisions but keep # less original name chars. # # Example: 10 # HASH_SIZE=10 ``` spm_namelength.sh ```bash #!/bin/bash ## Up is a bash function to simplify vertical file system navigation. ## ## Copyright (C) 2013 Serge Smeesters ## ## This program is free software: you can redistribute it ## and/or modify it under the terms of the GNU General Public License ## as published by the Free Software Foundation, either version 3 ## of the License, or (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ## See the GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program. If not, see http://www.gnu.org/licenses/. mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" [ -d $mydir ] || exit 4 configfile="$mydir/spm_namelength_config.sh" if [ -f "$configfile" ] then . "$configfile" else echo "No config file find :(" exit 4 fi trunc_item () { local filefullpathname="1ドル" local shortsize="2ドル" local filefullname=$(basename "$filefullpathname") if [ ${#filefullname} -gt $sizemax ] ; then local filepath=$(dirname "$filefullpathname") local filename=${filefullname%.*} local filextension=${filefullname##*.} local hashvalue="$(echo "$filename" | md5sum | cut -c1-$HASH_SIZE)" echo "---" echo "$filefullname" local filextensionsize=${#filextension} if [ $filextensionsize -gt $EXTENTION_MAX_SIZE ] ; then local fileshortfullname="$(echo "$filefullname" | cut -c1-$shortsize)$hashvalue" else local newsize (( newsize = shortsize - ( filextensionsize + 1 ) )) local fileshortname="$(echo "$filename" | cut -c1-$newsize)$hashvalue" local fileshortfullname="$fileshortname.$filextension" fi echo "→ $fileshortfullname" local fileshortfullpathname="$filepath/$fileshortfullname" # echo "$fileshortfullpathname" if [ -e "$fileshortfullpathname" ] ; then echo "Waw... the shorten file seam to yet exists ! :(" exit 4 fi pushd "$filepath">/dev/null echo "mv \"$fileshortfullname\" \"$filefullname\"">> .namelengthrestore mv "$filefullname" "$fileshortfullname" popd>/dev/null fi } trunc_directory () { local root="$(readlink -f "1ドル")" local shortsize="2ドル" # echo "(trunc_directory \"$root\"" local directoryfullpathname find "$root" -mindepth 1 -maxdepth 1 -type d | \ while read directoryfullpathname ; do trunc_directory "$directoryfullpathname" "$shortsize" trunc_item "$directoryfullpathname" "$shortsize" done local filefullpathname find "$root" -maxdepth 1 -type f | \ while read filefullpathname ; do trunc_item "$filefullpathname" "$shortsize" done } restore_directory () { local root="$(readlink -f "1ドル")" # echo "(restore_directory \"$root\"" local directoryfullpathname find "$root" -mindepth 1 -maxdepth 1 -type d | \ while read directoryfullpathname ; do restore_directory "$directoryfullpathname" done if [ -f "$root/.namelengthrestore" ] ; then pushd "$root">/dev/null cat .namelengthrestore . .namelengthrestore rm .namelengthrestore popd>/dev/null fi } main () { if [ $# -lt 2 ] ; then echo "Less than 2 arguments :(" exit 1 fi # echo "\2ドル : \"2ドル\"" local root="$(readlink -f "2ドル")" # echo "root : \"$root\"" if [ ! -d "$root" ] ; then echo "\"$root\" don't appear to be a directory :(" exit 1 fi case "1ドル" in trunc) if [ $# -lt 3 ] ; then echo "Trunc need 2 arguments..." exit 2 fi local sizemax="3ドル" if [ ! "$sizemax" ] ; then exit 1 ; fi local minsizemax (( minsizemax = 5 * HASH_SIZE + EXTENTION_MAX_SIZE )) # echo "A reasonable sizemax should be $minsizemax" if [ "$sizemax" -lt "$minsizemax" ] ; then echo "sizemax, $sizemax less then minsizemax, $minsizemax :(" exit 1 fi local shortsize (( shortsize = sizemax - HASH_SIZE )) trunc_directory "$root" "$shortsize" ;; restore) restore_directory "$root" ;; *) echo "Usage: 0ドル {trunc |restore } ">&2 exit 1 ;; esac } main "$@" ```

AltStyle によって変換されたページ (->オリジナル) /