All Downloads are FREE. Search and download functionalities are using the official Maven repository.

refcodes-batch.1.0.2.source-code.create-folder-backup.job Maven / Gradle / Ivy

There is a newer version: 1.0.5
Show newest version
#!/bin/bash

. $SCRIPT_DIR/lib-filesystem.inc

if [[ $HELP = y ]] ; then
	echo "The switch \"-j $JOB\" uses a given configuration to backup                     " >&2
	echo "a given folder. The folder is archived and placed into a given folder with.     " >&2
	echo "rotating filenames.                                                             " >&2
	# echo "--------------------------------------------------------------------------------" >&2
	logSeparator
fi

. $SCRIPT_DIR/validate-folder-config.job
. $SCRIPT_DIR/validate-folder-backup-config.job
. $SCRIPT_DIR/validate-dump-dir-config.job

if [[ $HELP = y ]] ; then
	exit $EXIT_SUCCESS
fi

exitOnConfigOff "FOLDER"
exitOnConfigOff "FOLDER_BACKUP"

DAY_OF_MONTH=`date '+%d'`
# if [ "$DAY_OF_MONTH" -lt "10" ] ; then
# 	DAY_OF_MONTH=0$DAY_OF_MONTH
# fi

ARCHIVE_NAME=folder-backup-$DAY_OF_MONTH-$FOLDER_BACKUP_BASE_NAME
ARCHIVE_FILE=$FOLDER_BACKUP_DIR/$ARCHIVE_NAME.tar.gz

if [[ $DUMP = n ]] ; then
	if [[ $VERBOSE = y ]] ; then
		echo "No dedicated dump dir specified, using path \"$HOME\" ..." >&2
	fi
	DUMP_DIR="$HOME"
fi

DUMP_FILE="$DUMP_DIR/$ARCHIVE_NAME.tar"
if [ ! -e "$FOLDER_BACKUP_DIR" ] ; then
	if [[ $VERBOSE = y ]] ; then
		echo "Creating/configuring folder backup path \"$FOLDER_BACKUP_DIR\" ..." >&2
	fi
	makePath "$FOLDER_BACKUP_DIR" "$FOLDER_BACKUP_FS_USER" "$FOLDER_BACKUP_FS_GROUP" "u+rw,og-rwx"
	exitOnError "Unable to create directory \"$FOLDER_BACKUP_DIR\" !!!"
fi

CURRENT_DIR=$(pwd)
cd $FOLDER_DIR
if [[ $VERBOSE = y ]] ; then
	echo "Archiving folder \"$FOLDER_DIR\" to temporary (dump) archive \"${DUMP_FILE}\" (should be local folder as of performance reasons) ..." >&2
fi


tar -cf ${DUMP_FILE} *

EXIT_CODE=$?
if [ "${EXIT_CODE}" == "2" ] ; then
	$(exit 2)
	warnOnError "Errors occured on executing \"tar\" resulting in an error"
	$(exit 0)
else
	$(exit ${EXIT_CODE})
fi
exitOnError "Unable to tar \"$FOLDER_DIR\" to archive \"${DUMP_FILE}\" !!!"

cd $CURRENT_DIR
if [[ $VERBOSE = y ]] ; then
	echo "GZip'ing temporary archive \"${DUMP_FILE}\" ..." >&2
fi
gzip -f ${DUMP_FILE}
exitOnError "Unable to gzip archive \"${DUMP_FILE}\" !!!"

DUMP_FILE="${DUMP_FILE}.gz"

if [[ $VERBOSE = y ]] ; then
	echo "Applying file access rights \"$FOLDER_BACKUP_FS_USER:$FOLDER_BACKUP_FS_GROUP\" to \"${DUMP_FILE}\" ..." >&2
fi

chown $FOLDER_BACKUP_FS_USER:$FOLDER_BACKUP_FS_GROUP ${DUMP_FILE}
# exitOnError "Unable to change owner to \"$FOLDER_BACKUP_FS_USER:$FOLDER_BACKUP_FS_GROUP\" for \"${DUMP_FILE}\" !!!"
warnOnError "Unable to change owner to \"$FOLDER_BACKUP_FS_USER:$FOLDER_BACKUP_FS_GROUP\" for \"${DUMP_FILE}\" !!!"

chmod u+rw,g-rwx,o-rwx ${DUMP_FILE}
exitOnError "Unable to change access rights for \"${DUMP_FILE}\" !!!"

if [[ $VERBOSE = y ]] ; then
	echo "Moving \"${DUMP_FILE}\" to \"${ARCHIVE_FILE}\" ..." >&2
fi

mv ${DUMP_FILE} ${ARCHIVE_FILE}
exitOnError "Unable to move \"${DUMP_FILE}\" to \"${ARCHIVE_FILE}\" !!!"


if [[ $VERBOSE = y ]] ; then
	# echo "--------------------------------------------------------------------------------" >&2
	logSeparator
fi

echo "Backup for files in \"$FOLDER_DIR\" was created:" >&2
# echo "--------------------------------------------------------------------------------" >&2
logSeparator
echo "+ For backup files archive see: \"${ARCHIVE_FILE}\"" >&2
# echo "--------------------------------------------------------------------------------" >&2
logSeparator

showSuccess "Created files backup of \"$FOLDER_DIR\" !"




© 2015 - 2024 Weber Informatics LLC | Privacy Policy