I need to back up a folder of pdf files on a daily basis. This is a large number of files so I first pack it in Gzip
and then delete the old Gzip
. To achieve this I have the following code in a file .sh
which is run by a cron every day at 5:00 AM:
#!/bin/sh
#Empaqueto los archivos y lo dejo en la carpeta archivos_empresas
cd /var/www/html/sistema/respaldo/respaldopdf && tar -cpzf $(date +%d%m%Y)-respaldo.tar.gz /var/www/html/sistema/archivos_empresas
#Luego busco el anterior gz y lo elimino
find /var/www/html/sistema/respaldo/respaldopdf/*.gz -mtime +0 -exec rm {} \;
All good, but something curious happens: It doesn't always delete the previous file and the worst thing is that I can't find the pattern to foresee it. So every day I have to check if the file was deleted or not to do it manually with an RM since being very heavy it uses a lot of disk space.
Scopes:
1.- This would be solved by keeping the same file name since it would be replaced, but I don't want to do that since, for security reasons, I need the new one to be generated first and if it is generated correctly, delete the old one.
2.- I know that the command -mtime +0
should be -mtime +1
but that way the file from the day before yesterday deletes it, not the one from yesterday.
What I need is to do a professional backup (this solution I made seems to be kind of amateurish). Thanks for your help.
Possible solution
Change the line
find /var/www/html/sistema/respaldo/respaldopdf/*.gz -mtime +0 -exec rm {} \;
tofind /var/www/html/sistema/respaldo/respaldopdf/*.gz -daystart -mtime 1 -exec rm -f {} \;
Improving your script
In your script, whether or not the security backup is generated, it deletes (or tries to) the previous backup, you have nothing to control if the previous command was executed correctly (this is solved by adding a
&&
and the command aftertar -cpzf $(date +%d%m%Y)-respaldo.tar.gz /var/www/html/sistema/archivos_empresas
). Also comment that the cd you make is unnecessary, since you can (and should) use the absolute path in the tar.Also comment that if you want to make your script more 'professional', you have to consider keeping the permissions on the backup (
--same-owner
,-p
, etc). Save a log of errors (2> error.log
for example). Also consider doing differential, incremental backups,... it is a good idea, to avoid doing only full backups (which take up more).How would I leave the script (it can be improved much more, but more or less an idea)