Instantly share code, notes, and snippets.

Embed
What would you like to do?
ownCloud Backup script
#!/bin/bash
# Author: noah huetter - dlatch.com
#
# v2:
# - added database backup
# - partial file transfer for no big file junk on /tmp
# v3:
# - Variables for user settings
# - Comments and functions
################################################################################
## USER SETTINGS
################################################################################
## Set to 1 to make a dry run without writing anything or 0 for normal mode
DRY_RUN=0
## Set to 1 to delete the oldest backup on the destination before this backup
## starts.
DELETE_OLDEST_BACKUP=1
## Log file location
LOG_FILE=/var/services/homes/admin/ocbackup/logs/ocbackup_log_`date +%Y-%m-%d.%H%M%S`.txt
## Default backup detination if none is passed to the script
DEFAULT_DEST=/volume1/remotes/usbshare1/ocbackup/
## Location of the owncloud command line tool
OCC_LOCATION=/volume1/web/owncloud/occ
## owncloud Installation directory
OC_LOCATION=/volume1/web/owncloud
## Web user to run occ
OC_WEB_USR=http
## Owncloud data location
SOURCE=/volume3/clouddata/clouddata/
## Database user, needs SELECT access on DB_NAME
DB_USR=backup
## Database user password
DB_PW=supersecretpassword42
## owncloud database name
DB_NAME=owncloud
################################################################################
## FUNCTIONS
################################################################################
## On Exit
function finish {
echo "Disable maintenance.."
if [ "$DRY_RUN" == "0" ]; then
sudo -u $OC_WEB_USR php56 $OCC_LOCATION maintenance:mode --off
fi
echo "Done"
# copy log file to destination
cp -v $LOG_FILE $DEST/$FOLDERNAME
}
trap finish EXIT
##
## @brief Check folder Access and exit if no write permission
##
function checkFolderAccess {
USER=$(whoami)
INFO=( $(stat -L -c "%a %G %U" $DEST) )
PERM=${INFO[0]}
GROUP=${INFO[1]}
OWNER=${INFO[2]}
ACCESS=no
if [[ $PERM && 0002 != 0 ]]; then
# Everyone has write access
ACCESS=yes
elif [[ $PERM && 0020 != 0 ]]; then
# Some group has write access.
# Is user in that group?
gs=( $(groups $USER) )
for g in "${gs[@]}"; do
if [[ $GROUP == $g ]]; then
ACCESS=yes
break
fi
done
elif [[ $PERM && 0200 != 0 ]]; then
# The owner has write access.
# Does the user own the file?
[[ $USER == $OWNER ]] && ACCESS=yes
fi
if [ "$ACCESS" == "no" ]; then
echo "No write access on destination directory!"
exit
fi
}
##
## @brief Check for read access on the specified database and exit if failed
##
function checkDbAccess {
echo "USE $DB_NAME; show tables;" | mysql -u $DB_USR --password=$DB_PW > /dev/null
STATUS=$?
if [ $STATUS != 0 ]; then
echo "DB Access denied"
exit
fi
}
##
## @brief Enables the maintennance.
##
function enableMaintennance {
if [ "$DRY_RUN" == "0" ]; then
sudo -u $OC_WEB_USR php56 $OCC_LOCATION maintenance:mode --on
fi
}
##
## @brief Disables the maintennance.
##
function disableMaintennance {
if [ "$DRY_RUN" == "0" ]; then
sudo -u $OC_WEB_USR php56 $OCC_LOCATION maintenance:mode --off
fi
}
##
## @brief Calculates the data size.
##
function calcDataSize {
SIZE=`sudo du -sk $SOURCE | cut -f 1`
return $SIZE
}
##
## @brief Specify target directory
##
function specifyTarget {
if [ ! $1 ]; then
echo "No target specified! Using default."
DEST=$DEFAULT_DEST
else
DEST=$1
fi
}
##
## @brief Delete old backup and create new folder
##
function prepareDestination {
ls -al $DEST
# delete oldest folder
DIRS=$(ls $DEST)
OLDDIR=$(echo $DIRS | head -n1 | cut -d " " -f1)
if [ "$DRY_RUN" == "0" ]; then
if [ "$DELETE_OLDEST_BACKUP" == "1" ]; then
echo "Delete old Folder $OLDDIR"
rm -rf $DEST/$OLDDIR
echo "Deleted!"
fi
fi
FOLDERNAME=`date +%Y-%m-%d.%H%M%S`
if [ "$DRY_RUN" == "0" ]; then
mkdir $DEST/$FOLDERNAME
fi
}
################################################################################
## ENTRY
################################################################################
# Start logging
exec > >(tee -i $LOG_FILE)
exec 2>&1
echo "####################################################################"
date
echo "Starting ocbackup"
echo "Specify Target directory"
specifyTarget
echo "Check for write permission on target"
checkFolderAccess
echo "Check for DB access"
checkDbAccess
echo "Reading clouddata source directory filesize..."
calcDataSize
echo Approx $(expr $SIZE / 1024 / 1024)GB of Data
echo "Enable maintenance.."
enableMaintennance
echo "Check for old directory"
prepareDestination
echo "####################################################################"
echo "Destination Foldername:"$DEST/$FOLDERNAME
echo "Dump database"
if [ "$DRY_RUN" == "0" ]; then
mysqldump -u $DB_USR --password=$DB_PW $DB_NAME > $DEST/$FOLDERNAME/owncloud.sql
fi
STATUS=$?
if [ $STATUS != 0 ]; then
echo "DB Access denied"
exit
fi
echo "Copy data and compress"
if [ "$DRY_RUN" == "0" ]; then
# unfortunately pv is not available on synology...
# tar cpf - $SOURCE | pv -p -t -e -r -s ${SIZE}k | split -b 1024MiB --suffix-length=4 --numeric-suffix - $DEST/$FOLDERNAME/$NAME.tar_
tar cpf - $SOURCE | split -b 1024MiB --suffix-length=4 --numeric-suffix - $DEST/$FOLDERNAME/ocbackup.tar_
fi
echo "Split into" `find $DEST/$FOLDERNAME -type f | wc -l` "Files"
echo "Copy config dir"
if [ "$DRY_RUN" == "0" ]; then
cp -rv $OC_LOCATION/config $DEST/$FOLDERNAME/
fi
echo "Fixing permissions"
if [ "$DRY_RUN" == "0" ]; then
chown -R root:root $DEST/$FOLDERNAME
fi
echo "####################################################################"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment