Skip to content

Instantly share code, notes, and snippets.

@themouette
Last active March 30, 2016 19:07
Show Gist options
  • Save themouette/71d79bf22ecdb1474ea6 to your computer and use it in GitHub Desktop.
Save themouette/71d79bf22ecdb1474ea6 to your computer and use it in GitHub Desktop.
backup-owncloud
#!/usr/bin/env bash
BACKUP_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )"
# Log a message.
function log {
echo -e " \033[32m*\033[0m ${1}"
}
# write an error message.
function error {
echo -e "\033[31m${1}\033[0m"
}
# Backups the owncloud server
log "Backup database"
echo "Please enter database password"
mysqldump --lock-tables -h localhost -u owncloud -p owncloud > "${BACKUP_DIR}/owncloud-sqlbkp_$(date +"%Y%m%d").bak"
log "Sync data"
rsync -Aax /home/owncloud/data/ "${BACKUP_DIR}/owncloud-dirbkp/"
#!/bin/sh
# full and incremental backup script
# created 07 February 2000
# Based on a script by Daniel O'Callaghan <danny@freebsd.org>
# and modified by Gerhard Mourani <gmourani@videotron.ca>
#
# Taken from http://www.tldp.org/LDP/solrhe/Securing-Optimizing-Linux-RH-Edition-v1.3/chap29sec306.html
# Updated by Julien Muetton <julien@muetton.me>
#
# To verify archive
#
# $ tar -tf /volume1/homes/admin/backups/owncloud-Sun.tar
#
# To restore from full backup
#
# $ cd / && tar xpf /volume1/homes/admin/backups/owncloud/owncloud-Sun.tar
# $ rsync --recursive --archive --verbose \
# /volume1/homes/admin/owncloud -e ssh owncloud:/home/owncloud/
# $ ssh owncloud
# $ cp /home/owncloud/config.php /var/www/owncloud/config/config.php
# $ # restore database as defined in owncloud manual
#Change the 5 variables below to fit your computer/backup
COMPUTER=owncloud # name of this computer
DIRECTORIES="/volume1/homes/admin/owncloud" # directoris to backup
BACKUPDIR=/volume1/homes/admin/backups # where to store the backups
TIMEDIR=/volume1/homes/admin/backups/last-full # where to store time of full backup
TAR=/bin/tar # name and locaction of tar
RSYNC=/usr/bin/rsync
SSH=/usr/bin/ssh
# ----------------------- Sync Remote Data
# Backup mysql
# Following this: http://stackoverflow.com/a/9293090/1070271
# password and users are stored in server's /etc/mysql/.my.cnf
COMMAND="mysqldump --lock-tables -h localhost owncloud > '/home/owncloud/owncloud-sql-latest.bak'"
$SSH julien@owncloud "$COMMAND"
# Copy owncloud config to backup directory
# Note that I use a custom configuration location
COMMAND="chmod +w /home/owncloud/config.php \
&& cp /var/www/owncloud/config/config.php /home/owncloud/config.php"
$SSH julien@owncloud "$COMMAND"
# Sync remote data locally
$RSYNC --recursive --archive --verbose \
-e $SSH owncloud:/home/owncloud/ $DIRECTORIES
# ---------------------- Create Archives
#You should not have to change anything below here
PATH=/usr/local/bin:/usr/bin:/bin
DOW=`date +%a` # Day of the week e.g. Mon
DOM=`date +%d` # Date of the Month e.g. 27
DM=`date +%d%b` # Date and Month e.g. 27Sep
# Create required directories
mkdir -p $BACKUPDIR $TIMEDIR
# On the 1st of the month a permanet full backup is made
# Every Sunday a full backup is made - overwriting last Sundays backup
# The rest of the time an incremental backup is made. Each incremental
# backup overwrites last weeks incremental backup of the same name.
#
# if NEWER = "", then tar backs up all files in the directories
# otherwise it backs up files newer than the NEWER date. NEWER
# gets it date from the file written every Sunday.
# Monthly full backup
if [ $DOM = "01" -o ! -e $BACKUPDIR/$COMPUTER-FULL.tar ]; then
# keep last month backup in case of a crash on Sunday 1st
if [ -e "$BACKUPDIR/$COMPUTER-FULL.tar" ]; then
mv $BACKUPDIR/$COMPUTER-FULL.tar $BACKUPDIR/$COMPUTER-FULL-PREVIOUS.tar
fi
NEWER=""
NOW=`date +%d-%b`
# Update full backup date
echo $NOW > $TIMEDIR/$COMPUTER-full-date
$TAR $NEWER -cf $BACKUPDIR/$COMPUTER-FULL.tar $DIRECTORIES
fi
# Weekly full backup
if [ $DOW = "Sun" ]; then
NEWER=""
NOW=`date +%d-%b`
# Update full backup date
echo $NOW > $TIMEDIR/$COMPUTER-full-date
$TAR $NEWER -cf $BACKUPDIR/$COMPUTER-$DOW.tar $DIRECTORIES
# Make incremental backup - overwrite last weeks
else
# Get date of last full backup
NEWER="--newer `cat $TIMEDIR/$COMPUTER-full-date`"
$TAR $NEWER -cf $BACKUPDIR/$COMPUTER-$DOW.tar $DIRECTORIES
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment