#! /bin/bash
if [ $# -ne 2 ] || [ $1 != "-r" ] && [ $1 != "-c" ] || [ `expr index $2 \/` -ne 1 ] ; then
echo "Los parametros admitidos son los siguientes: ";
echo "Primer parametro debe ser -c (crea copia de seguridad) o -r (restaura copia de seguridad)";
echo "Segundo parametro debe ser direccionamiento absoluto al lugar donde realizar la accion";
exit;
fi
if [ $1 == "-c" ]; then
cont=0;
echo "busco los que son mayores de uid=1000";
for i in $(cut -d: -f 1 /etc/passwd); do
if [ `id -u $i` -gt 999 ] ; then
usuario[$cont]=$i;
cont=$((cont+1));
fi
done
rm /tmp/copiac
conta=0;
echo "busco los usuarios en el fichero shadow";
while [ $conta -lt ${#usuario[@]} ] ; do
grep ${usuario[$conta]} /etc/shadow >> /tmp/copiac;
conta=$((conta+1));
done
cont=0;
unset usuario;
echo "elimino los que no tienen contraseña";
for i in $(sed -e '/:\*:/d' /tmp/copiac | cut -d : -f 1); do
usuario[$cont]=$i;
cont=$((cont+1));
done
echo ${usuario[@]};
for i in ${usuario[@]}; do
echo $i
if [ -d $(grep ${usuario[$i]} /etc/passwd | cut -d : -f 6) ] ; then
ruta=$(grep ${usuario[$i]} /etc/passwd | cut -d : -f 6);
echo $ruta ;
cd $ruta ;
pwd ;
tar -czvf $2/${usuario[$i]}.tgz ./;
fi
done
echo "creando el fichero usuarios";
for i in ${usuario[@]} ; do
echo ${usuario[$i]}:$(grep ${usuario[$i]} /etc/passwd | cut -d : -f 5):$(grep ${usuario[$i]} /etc/shadow | cut -d : -f 2):$(grep ${usuario[$i]} /etc/passwd | cut -d : -f 6):$(grep ${usuario[$i]} /etc/passwd | cut -d : -f 7) >> $2/usuarios ;
echo " creado " ;
done
fi
if [ $1 == "-r" ]; then
./copiar $2;
fi