My Tech-Notes

Dspace Backup Commands

How to Setup Automatic Daily Backup of Dspace Database & Files

To setup Automatic Daily DSpace Backup, follow the steps below:


1. Create Backup Directory

First, login to root user and create a directory for backup:

$ sudo su

Enter Login password when prompted

$ mkdir dspace_backup
$ sudo chmod 777 dspace_backup
$ exit

2. Create Backup Script

Change directory to /usr/local/bin/ and create a script file:

$ cd /usr/local/bin/
$ nano backup.sh

Copy the below script into the file and save it:

#!/bin/bash

# Setup shell to use for backups
SHELL=/bin/bash

# Setup name of local server to be backed up
SERVER="%your_server_local_IP%"

# Setup event stamps
DOW=`date +%a`
TIME=`date`

# Setup paths
FOLDER="/home/dspace_backup"
FILE="/var/log/backup-$DOW.log"

# Do the backups
{
echo "Backup started: $TIME"

# Make the backup folder if it does not exist
if test ! -d /home/dspace_backup
then
  mkdir -p /home/dspace_backup
  chmod 777 dspace_backup

  echo "New backup folder created"
else
  echo ""
fi

# Make sure we're in / since backups are relative to that
cd /

## PostgreSQL database (Needs a /root/.pgpass file)
if command -v psql >/dev/null 2>&1 ; then
    echo "SQL dump of PostgreSQL database 'dspace'"

    # Dump database directly to backup folder
    su - postgres -c "pg_dump --inserts dspace > $FOLDER/dspace-db-$DOW.sql"

    # Run vacuum
    su - postgres -c "vacuumdb --analyze dspace > /dev/null 2>&1"
else
    echo "psql command not found — skipping PostgreSQL backup."
fi

# Backup '/dspace' folder
echo "Archive '/dspace' folder"
tar czf $FOLDER/dspace-$DOW.tgz dspace/

# View backup folder
echo ""
echo "** Backup folder **"
ls -lhS $FOLDER

# Show disk usage
echo ""
echo "** Disk usage **"
df -h

TIME=`date`
echo "Backup ended: $TIME"

} > $FILE

### EOF ###

Alternative Script

#!/bin/bash
PGPASSWORD="dspace" pg_dump -U dspace -h localhost -Fc dspace | gzip > dspace_backup/dspace-$(date +%Y-%m-%d-%H.%M.%S).sql.gz
now=$(date +"%d_%m_%Y")
zip -r  /home/server/dspace_backup/$now-assetstore.zip /dspace/assetstore
zip -r  /home/server/dspace_backup/$now-log.zip /dspace/log

Change [username] & [password] as per your DSpace Database Credentials.

3. Make Script Executable

$ sudo chmod +x backup.sh

Test the script manually:

$ sh backup.sh

4. Schedule Automatic Backup with Cron Job

Open crontab:

$ crontab -e

Choose nano editor if prompted. Add one of the following lines at the end:

55 04 * * * /usr/local/bin/backup.sh

or

15 13 * * * /usr/local/bin/backup.sh
00 14 * * * find dspace_backup/* -mtime +180 -exec rm {} ;

Save using Ctrl+O, press Enter, then exit with Ctrl+X.

Done

Now, this cron job will:

Take automatic daily backup of DSpace Database, Files & Logs.

Store backups inside dspace_backup folder.

Automatically delete backup files older than 180 days.