I am creating a simple script ( backup_home.sh ) to make backups of a user, I have the following:
#Configuracion del backup home
#--------------------------------
current_date=$(date +%d-%m-%Y)
user='juanito'
dir_backup="/home/.backups/home_${user}"
dir_guardar="/home/${user}"
dir_ignorar="/home/${user}/Documentos/.backups"
nombre_fichero_backup="backup_home_${user}_${current_date}.tar.gz"
####################################################################
echo "------------------------------";
echo "Comienzo del backup de '${dir_guardar}'($(date +%d-%m-%Y))";
echo "------------------------------";
echo "fecha = ${current_date}"
echo "directorio backup = ${dir_backup}"
echo "directorio a guardar = ${dir_guardar}"
echo "directorio a ignorar = ${dir_ignorar}"
echo "nombre fichero backup = ${nombre_fichero_backup}"
tar --exclude=´${dir_ignorar}´ -zcvpf ${dir_guardar} ${dir_backup}
echo "------------------------------";
echo "Fin del backup de '${dir_guardar}'";
echo "------------------------------";
when I run it ( sh backup_home.sh ) it throws me the following error:
tar (child): /home/juanito: No se puede efectuar open: Es un directorio
tar (child): Error is not recoverable: exiting now
/home/.backups/home_juanito/
tar: Child returned status 2
tar: Error is not recoverable: exiting now
The backticks `` execute the command between them and are replaced by the result of the command. You don't need them in
--exclude
, you can use "".With the options you passed to
tar
, it expects the output file and the directory to compress, but you are passing it the directory to compress and the output directory.The corrected command:
In case it helps, back in the day I made a script to make backups of my files. Doesn't answer the question, but I guess it might be useful to the site:
I understand that it is amply commented, so I see it unnecessary to explain the code. Luck.
Edit: According to @Cuauhtli's proposal, I propose a more "cool" variant, to avoid both
echo
. Only for the first few lines, after all, I still haven't answered the question. Thanks, @Cuauhtli, the truth is that the code is much more elegant with a small here-cument: