-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplitXrsync.sh
More file actions
112 lines (96 loc) · 4.21 KB
/
splitXrsync.sh
File metadata and controls
112 lines (96 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/bin/bash
#Autor: @joseccnet
#Configuraciones Obligatorias:
remotesshaccount="" #Coloque su configuracion para acceso por ssh al servidor remoto. Ejem: userbob@192.168.1.5 , o tambien: userbob@myowndomain.xyz.com
remotedir="~/myremotedir" #Ejem: ~/myremotedir , o tambien /home/userbob/myremotedir . ESTE DIRECTORIO ES EL QUE SE SINCRONIZARA hacia el directorio local 'localdir'
localdir="~/mylocaldir" #Ejem: ~/mylocaldir , o tambien: /home/userjhon/mylocaldir
#Configuraciones opcionales:
numprocesos=4 #numero de procesos o hilos concurrentes para sincronizar con rsync.
sizesplitfile="30M" #Tamano de pedazo de rchivo en Megabytes. Ejem: 50M , 512M , 1024M , etc.
bwlimit="--bwlimit=1024" #Limite en Kbps, por proceso.
sshport=22
optsrsync1="$bwlimit -azPhc --partial-dir=$localdir/tmprsync --progress --rsh='ssh -p $sshport'"
optsrsync2="--remove-source-files" #**ATENCION!!!** Eliminará los archivos remotos. Opcion predeterminada.
if [ "$localdir" == "" -o "$remotedir" == "" -o "$remotesshaccount" == "" ] ; then
echo -e "\nEdite el script y configure almenos las siguientes variables: remotesshaccount , remotedir y localdir"
echo "Nota: No olvide configurar 'ssh key authentication'. Referencia: https://www.google.com/#q=ssh%20key%20authentication"
exit -1
fi
remotedirscaped="$(echo $remotedir | sed -e 's/\//\\\//g' -e 's/\./\\\./g')" #'Escapar caracteres especiales.'
function myrsync(){
green='\033[0;32m'
red='\033[0;31m'
yellow='\033[1;33m'
blue='\033[0;34m'
NC='\033[0m' # No Color
echo -e "${yellow}+${NC}$1${yellow}+${NC}" && eval "rsync $optsrsync1 $remotesshaccount:$remotedir/$1 $localdir/$1" && echo -e "${blue}++${NC}$1${blue}++${NC}" && eval "rsync $optsrsync1 $optsrsync2 $remotesshaccount:$remotedir/$1 $localdir/$1" && echo -e "+++${red}$1${NC}+++ ${green}Done!${NC}"
[ "$?" != "0" ] && exit -1
#Junta los pedazos de archivos:
if [[ "$1" == *[0-9][0-9][0-9][0-9]z ]] ; then
outputfile=$(echo $1 | sed 's/\.[0-9][0-9][0-9][0-9]z//g')
cd $localdir
touch $outputfile
while true
do
ls .$outputfile.* > /dev/null 2>&1
if [ $? == "0" ] ; then
sleep 5
continue
fi
break
done
if [ -f $outputfile\.[0-9][0-9][0-9][0-9]a ] ; then
file=$(find . -empty -name $outputfile)
if [ "$file" != "" ] ; then
echo "Creando $outputfile ..."
cat $outputfile.* > $outputfile && rm -f $outputfile.* && echo -e "${yellow}Archivo + + +$outputfile+ + + creado!!!${NC}"
fi
fi
fi
return 0
}
read -d '' comandosremotosTemplate << 'EOF'
cd REMOTEDIR
lista=$(find . -maxdepth 1 -type f | sort)
for i in $lista
do
if [[ $(find ./$i -type f -size +SIZESPLITFILE 2>/dev/null) ]]; then
split -a 4 -d -b SIZESPLITFILE $i $i. && rm $i
f1=$(echo $(ls $i.*) | awk '{print $1}')
f2=$(echo $(ls $i.*) | awk '{print $NF}')
mv="mv $f1 ${f1}a; mv $f2 ${f2}z"
eval "$mv"
fi
done
files=$(find . -maxdepth 1 -type f | sort)
echo $files
exit 0
EOF
IFS=$' '
comandosremotos=$(echo $comandosremotosTemplate | sed -e "s/REMOTEDIR/$remotedirscaped/g" -e "s/SIZESPLITFILE/$sizesplitfile/g")
echo -e "Conectando al servidor remoto y haciendo 'split' a los archivos...\n"
files=$(ssh -p $sshport $remotesshaccount "$comandosremotos" | sed -e 's/\.\///g' -e 's/ /\n/g')
echo -e "\nSe incronizaran los siguientes archivos de ${sizesplitfile}B cada uno:\n"
echo "$files" | sed ':a;N;$!ba;s/\n/ , /g'
echo -e "\nSincronizando archivos con $numprocesos procesos concurrentes ...\n"
export -f myrsync
export bwlimit remotesshaccount remotedir localdir optsrsync1 optsrsync2
echo $files | xargs -I '{}' -P $numprocesos -n1 bash -c "myrsync '{}'" || exit -1
echo ""
cd $localdir/
lista=$(find . -maxdepth 1 -type f -name "*.0000a" )
IFS=$'\n'
for i in $lista
do
outputfile=$(echo $i | sed 's/\.0000a//g')
if [ -f $outputfile\.[0-9][0-9][0-9][0-9]z ] ; then
if [ ! -f $outputfile ] ; then
yellow='\033[1;33m'
NC='\033[0m' # No Color
echo -e "${yellow}Creando $outputfile ...${NC}"
cat $outputfile.* > $outputfile && rm -f $outputfile.* && echo -e "${yellow}Archivo + + +$outputfile+ + + creado!!!${NC}"
fi
fi
done
echo "Done."
exit 0