'I cant find why this script sometimes works and sometimes doesn't
I took over someone's else backup script and change it into something a bit more robust. Initially, it would create a single huge tar.gz file (280GB+), but I change it so it would create many smaller tar.gz files (one tar.gz per folder). If I run the script manually, it does the compression but the log sometimes fails. I need to filter the folder by date and send that by email. Can't figure out why it fails. So I removed the date and used the wildcard so I get the entire folder with previous tar.gz files. Is not what I want but it works. For each email I get I check by eye if all folders are there.
I schedule it on a crontab job to do it every day (* 19 * * * /home/user/backup.sh). Sometimes only a few folders are compressed and the others are not. Sometimes doesn't do the compression at all and the logs come empty and then I check if the files are there, if they are accessible, and they are. I don't understand why doesn't work. It's driving me mad.
Any help?
The script:
#/bin/bash
find /home/bkp/ -mtime +4 -type f -exec rm {} \;
# Gets the date to calculate the time spent.
BEFORE="$(date +%s)"
mount -a
IS_MOUNTED=`mount | grep /mnt/poa`
if [ -z "$IS_MOUNTED" ]; then
echo "ERROR" | mutt -s "Backup Server" [email protected]
echo "ERROR" | mutt -s "Backup Server" [email protected]
exit 1
else
# This loop takes any non-folder files and moves them into a specific folder.
for PATH_TO_FILE in "/mnt/poa"/*
do
# Is a file?
if [[ -f $PATH_TO_FILE ]]; then
# Ignore Thumbs.db
if [[ $PATH_TO_FILE != "/mnt/poa/Thumbs.db" ]]; then
mv "$PATH_TO_FILE" "/mnt/poa/Random/"
fi
fi
done
# Gets the date.
DATE=`date +%d_%h_%y`
#echo $DATE
# This loop does the backup.
for PATH_TO_FILE in "/mnt/poa"/*
do
# Is a folder?
if [[ -d $PATH_TO_FILE ]]; then
FOLDER_NAME=$(basename "$PATH_TO_FILE")
#echo "Path --> $PATH_TO_FILE"
#echo "Basename --> $FOLDER_NAME"
#echo "Full name --> ${FOLDER_NAME}__${DATE}.tar.gz"
tar -czcvf /home/bkp/${FOLDER_NAME}__${DATE}.tar.gz /mnt/poa/${FOLDER_NAME}
#echo "/home/bkp/${FOLDER_NAME}__${DATE}.tar.gz /mnt/poa/${FOLDER_NAME}"
fi
done
# Log file
LOG=/var/log/backup/${DATE}.log
echo "# Backup Log # ">> $LOG
echo "" >> $LOG
echo "" >> $LOG
echo " -- Files :" >> $LOG
echo "" >> $LOG
# Fails most of the time
#du -sch /home/bkp/*_${DATE}.tar.gz >> $LOG
du -sch /home/bkp/*.tar.gz >> $LOG
echo "" >> $LOG
echo "" >> $LOG
# Math for time spent on this backup.
AFTER="$(date +%s)"
TIME_SPENT="$(expr $AFTER - $BEFORE)"
HOURS=$(($TIME_SPENT / 3600))
TIME_SPENT=$(($TIME_SPENT - $HOURS * 3600))
MINUTES=$(($TIME_SPENT / 60))
SECONDS=$(($TIME_SPENT - $MINUTES * 60))
echo "Time spent" : "$HOURS HOURS $MINUTES MINUTES $SECONDS SECONDS ">> $LOG
cat /var/log/backup/${DATE}.log | mutt -s "Backup Server" [email protected]
cat /var/log/backup/${DATE}.log | mutt -s "Backup Server" [email protected]
cat /var/log/backup/${DATE}.log | mutt -s "Backup Server" [email protected]
fi
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
