Horizontal Navigation

Friday 7 October 2022

Daily Automatic DSpace Backup of Database & Repository

The detailed procedure to take daily automatic DSpace backup of Database & Repository using DSpace Backup Commands in Terminal.

In this process, we will create DSpace backup file for the following:

      1. PostgreSQL Database.

      2. DSpace File System (dspace/assetstore directory).

      3. DSpace Log Files. (dspace/log directory).

To set up Automatic Daily DSpace Backup, Please do follow the below DSpace Backup Commands.

First of all, log in to the root user and create a directory for backup.

sudo su

Enter Login password when prompted.

mkdir dspace_backup

Allow permissions to this newly created folder.

sudo chmod 777 dspace_backup

Next, exit from root directory.

exit

Now Create a script file for automatic dpsace backup. 

sudo nano dspace_backup/backup.sh

Copy below text into file and save it.

#!/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

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

Make this file executable by the following command:

sudo chmod +x dspace_backup/backup.sh

Now, execute this file for testing by the following command:

sh dspace_backup/backup.sh

Next, Schedule DSpace backup using a cron job, Open Crontab using nano editor.

crontab -e

It will ask to select an editor to open crontab, select nano editor.

Copy & paste the following lines at the end of crontab.

15 16 * * * dspace_backup/backup.sh

00 14 * * * find dspace_backup/* -mtime +180 -exec rm {} ;

Save this file using Ctrl+O and ENTER, then press Ctrl+X to exit the cron.

Now, This cron job will take Backup of Dspace Database, files & logs  automatically at 04:15PM daily and save it to dspace_backup folder. It will automatically delete backup files older than 180 days.