LINUX
Script de shell de Linux para sincronizar directorios de forma interactiva
Este guión sincronizar dos directorios entre dos servidores. Básicamente, estamos usando funciones de rsync para ejecutar este programa. El script solicitará de forma interactiva ingresar los detalles a continuación.
1) IP o nombre del servidor remoto
2) Nombre de usuario del sistema remoto
3) Directorio local para sincronizar
4) Directorio remoto para sincronizar
5) Compruebe que el usuario necesita copiar la marca de tiempo y conservar los permisos
Directorios de sincronización de scripts
#Tell with shell to use
#!/bin/bash
#Ask user to enter remote server ip or name
while [[ -z ${RS} ]]
do
echo -n "Remote syncing server ip/name: "
#Save input into variable with name RS
read RS
done
#Ask user to enter remote server user
while [[ -z ${RU} ]]
do
echo -n "Remote syncing server user: "
#Save input into variable with name RU
read RU
done
#Ask user if copy timestamp and preserve permissions, valid input is Yes or No, will ask until valid input
while [[ "$TS" != "Yes" && "$TS" != "No" ]]
do
echo -n "Copying with timestamp and preserve permissions : Yes/No "
#Save input into variable with name TS
read TS
done
#If user need to copy timestamp and preserve permissions we need to use key -p additionally.
if [ "$TS" = "Yes" ]
then
TS=" -p "
else
#If no need to copy timestamp and preserve permissions, we don't need to use any additional key
TS=""
fi
#Ask user to enter local directory to sync, also check if directory exist
while [[ -z ${LD} || !( -d ${LD}) ]]
do
echo -n "Syncing local directory location: "
#Save input into variable with name LD
read LD
done
#Ask user to enter remote directory
while [[ -z ${RD} ]]
do
echo -n "Remote Syncing directory location: "
#Save input into variable with name RD
read RD
done
echo -n "Syncing ..."
#Run sync with presented parameters
rsync -r $TS ${LD} ${RU}@${RS}:${RD} --progress
Salida de secuencia de comandos
#./sync.sh Remote syncing server ip/name: node246.linoxide.com Remote syncing server user: linuser Copying with timestamp and preserve permissions : Yes/No Yes Syncing local directory location: /home/linoxide/backup/database01 Remote Syncing directory location: /storage/backup/ Syncing … sending incremental file list CSR/file 1,120 100% 410.16kB/s 0:00:00 (xfr#1, to-chk=8/10) CSR/file1 1,110 100% 1.06MB/s 0:00:00 (xfr#2, to-chk=7/10) CSR/file2 1,675 100% 1.60MB/s 0:00:00 (xfr#3, to-chk=6/10) CSR/file3 2,479 100% 2.36MB/s 0:00:00 (xfr#4, to-chk=5/10) CSR/201406/file4 1,110 100% 1.06MB/s 0:00:00 (xfr#5, to-chk=3/10) CSR/201406/file5 1,679 100% 1.60MB/s 0:00:00 (xfr#6, to-chk=2/10) CSR/201406/file6 1,115 100% 1.06MB/s 0:00:00 (xfr#7, to-chk=1/10)