I just updated my MDS past the versions in sk163300, which changed mds_backup to no longer gzip the final tar file. That broke my file renaming logic. Testing a fix.
Edited to add: This should work for versions which emit .mdsbk.tar files instead of .mdsbk.tgz. Also fixed a stupid ordering issue with the markerFile. The marker was created after running the mds backup:
Code:
#!/bin/env bash
MTA="<mail.example.com>"
RECIPIENTS="<someAddress@example.com; secondAddress@example.com>"
WORKING_DIR="/var/log/mds_backup"
NUMBER_TO_KEEP=1
. /etc/profile.d/CP.sh
############################################################
## Send a notice the backup is starting, then wait five
## minutes to give people a chance to see it.
printf "From: root@$(hostname)
To: ${RECIPIENTS}
Subject: MDS backup for $(date --iso-8601)
mds_backup beginning on $(hostname) in five minutes, at $(date -d '+5 minutes' --iso-8601=seconds)." \
| /sbin/sendmail --host="${MTA}" --read-envelope-from -t
sleep 300s
############################################################
## Run the backup.
if [ ! -d ${WORKING_DIR} ];then mkdir ${WORKING_DIR};fi
touch ${WORKING_DIR}/markerFile
MDS_ERROR=$( { mds_backup -b -i -l -d ${WORKING_DIR}>/dev/null; } 2>&1 )
MDS_EXIT=$?
cd ${WORKING_DIR}
for fileName in $(ls *.mdsbk.tgz | grep -v $(hostname)); do
dateStamp=$(echo $fileName | cut -d. -f1)
mv ${fileName} ${dateStamp}.$(hostname).mdsbk.tgz
done
for fileName in $(ls *.mdsbk.tar | grep -v $(hostname)); do
dateStamp=$(echo $fileName | cut -d. -f1)
mv ${fileName} ${dateStamp}.$(hostname).mdsbk.tar
done
cd "$OLDPWD"
############################################################
## Check the API afterwards. Sometimes it seems to die
## during an mds_backup. If dead, restart it.
api status>/dev/null
API_EXIT=$?
if [ ${API_EXIT} -ne 0 ];then api start>/dev/null;fi
############################################################
## SCP code here.
NEW_BACKUP="$(find ${WORKING_DIR} -newer markerFile -name '*.mdsbk.*')"
## SCP_EXIT=$?
SCP_EXIT=0
############################################################
## If the SCP worked, clean up old files to leave only the
## number specified in FILES_TO_KEEP. Older files are
## removed first.
FILES_REMOVED=""
if [ ${SCP_EXIT} -eq 0 ];then
NUMBER_TO_REMOVE=$(($(ls -t ${WORKING_DIR}/*.mdsbk.* | wc -l)-${NUMBER_TO_KEEP}))
if [ ${NUMBER_TO_REMOVE} -gt 0 ];then
FILES_REMOVED=$(ls -t ${WORKING_DIR}/*.mdsbk.* | tail -n ${NUMBER_TO_REMOVE})
echo "${FILES_REMOVED}" | xargs -L 1 /bin/rm
fi
fi
/bin/rm markerFile
############################################################
## Report backup status to the admins.
printf "From: root@$(hostname)
To: ${RECIPIENTS}
Subject: MDS backup for $(date --iso-8601)
mds_backup finished on $(hostname) at $(date --iso-8601=seconds).
mds_backup exit code: ${MDS_EXIT}
After backup, API was $(if [ ${API_EXIT} -ne 0 ];then printf "not running. Restarted.";else printf "running.";fi)
SCP exit code: ${SCP_EXIT}
mds_backup STDERR:
############################################################
${MDS_ERROR}
############################################################
Files removed:
${FILES_REMOVED}" \
| /sbin/sendmail --host="${MTA}" --read-envelope-from -t
Bookmarks