Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Owncloud backup with database and directories
#!/bin/bash
set -e
# @explain this script is run by a cronjob every week
### “At 04:00 on Saturday.”
### 0 4 * * 6 /opt/backup-owncloud.sh > /home/alex/backup-owncloud.log 2>&1
# @vars defined
MAX_BACKUP_FILES=52 # keep my backup files during one year
# @vars global
CURRENT_DATE=`date +"%Y_%m_%d-%H_%M_%S"`
# @vars backup
BACKUP_FROM="/home/web/html/owncloud"
BACKUP_DESTINATION="/home/alex/backup"
BACKUP_OWNCLOUD="${BACKUP_DESTINATION}/owncloud/${CURRENT_DATE}"
# @vars directories
DIR_OWNCLOUD="/var/www/html/owncloud"
# @ init
mkdir -vp "${BACKUP_OWNCLOUD}"
echo "-|--- Owncloud backup started on ${CURRENT_DATE}"
# @backup database
echo "-| Backup : Database"
DB_USER="owncloud"
DB_PASS=''
mysqldump --lock-tables -h localhost -u "${DB_USER}" -p${DB_PASS} owncloud >> "${BACKUP_OWNCLOUD}/owncloud.sql"
# @backup directory
echo "-| Backup : Directory"
# rsync -AazPq "${DIR_OWNCLOUD}" "${BACKUP_OWNCLOUD}"
zip -0 -r -qq "${BACKUP_OWNCLOUD}/files.zip" "${DIR_OWNCLOUD}"
# free disk space
echo "-| Backup : Freeing space"
NUMBER_OF_FILES=`ls -1 | wc -l`
if [ "${NUMBER_OF_FILES}" -gt "${MAX_BACKUP_FILES}" ]; then
FILE_TO_DELETE=`ls -tr ${BACKUP_DESTINATION}/owncloud | head -n 1`
rm -Rf "${FILE_TO_DELETE}"
fi
echo "-| Backup size : `du -hs ${BACKUP_OWNCLOUD}`"
# @ end
echo "-|--- Owncloud backup ended on `date +"%d/%m/%Y at %H:%M:%S"`"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.