ownCloud - 9.1 Backup via Cron with several instances

Hello,
I'm looking for a possibility to backup my owncloud 9.1 instance to an hdd via a cronjob. I have allready looked at the provieded help-page by owncloud but want severeal instances. Like a backup every day and everytime tree instances. Like today is october 9th so I want a backup of october 9th, 8th and 7th. Best would be a bash-script I think, but I don't have really an idea how to do that. Maybe you can help me. This is where I'm at the moment:

#!/bin/bash

# database
mysqldump --lock-tables -h 127.0.0.1 -u oc_admin -p PASS > owncloud-sqlbkp_`date +"%Y%m%d"`.bak

# data - directory
rsync -Aax /owncloud/data/ /oc-backup/owncloud-dirbkp_`date +"%Y%m%d"`/

# config - directory
rsync -Aax /var/www/html/site.de/config/ /oc-backup/owncloud-config-dirbkp_`date +"%Y%m%d"`/

Isn't it also necessary to set owncloud into maintenance mode and how is this possible via bash?
Thank's allready for your help!

Moving out of the oC server category as doing backups and writing the scripts are not really oC specific.

oC should be set in maintenance mode during the backup. This can be achieved by using the occ command (see doc.owncloud.org).

If you are using Ubuntu or Debian server, you can cd in to the /var/www/owncloud directory, then use

sudo -u www-data php occ maintenance:mode --on

to set ownCloud into maintenance mode.

Then do your backup

Then use:

sudo -u www-data php occ maintenance:mode --off

to turn maintenance mode back off again.

If you are using RHEL or CentOS, then I think you replace www-date with apache as the user, but not 100% sure.

I have a similiar structure for my backup, using root cron as below:

#!/bin/bash
#
# Experimental script to backup owncloud

PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin

rsync -Aax /var/www/owncloud/config /media/joseph/80GBHDD/owncloud/
rsync -Aax /var/www/owncloud/data /media/joseph/80GBHDD/owncloud/
sqlite3 /var/www/owncloud/data/owncloud.db .dump > /media/joseph/80GBHDD/owncloud/owncloud-dbbackup_date +"%Y%m%d".bak

I run this cron job during the night.

Do I need to put oC into maintenance mode?

From my limited knowledge of linux scripting, would this script work?

#!/bin/bash
#
# Experimental script to backup owncloud

PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin

# maintenance mode on
sudo cd /var/www/owncloud
sudo -u www-data php occ maintenance:mode --on

#perform backup
rsync -Aax /var/www/owncloud/config /media/joseph/80GBHDD/owncloud/
rsync -Aax /var/www/owncloud/data /media/joseph/80GBHDD/owncloud/
sqlite3 /var/www/owncloud/data/owncloud.db .dump > /media/joseph/80GBHDD/owncloud/owncloud-dbbackup_date +"%Y%m%d".bak

# maintenance mode off
sudo cd /var/www/owncloud
sudo -u www-data php occ maintenance:mode --off

Yes, this works, but sudo is per default disabled on non tty environment, so it won't work as crontab. Do you have an other suggestion how to perform it?

PS: For RHEL or CentOS apache is correct :wink:

Thank you! I will follow your suggestion.

Hello, it wasn't really a suggestion, more a question :smiley:

To your problem. You don't need to cd into the directory, you can also do it in one line like this:

sudo -u www-data php /var/www/owncloud/occ maintenance:mode --on/off

But, and here comes my question again. On CentOS using the sudo command in an non tty environment is per default because of security disabled. You can enable it, but I would like to find an other way to do it. I'm not sure how this is on debian or ubuntu.