16/09/2021

Dspace Backup Commands | How to Setup Automatic Daily Backup of Dspace Database & Files

By Technical Digit

Create a script for daily automatic DSpace backup of Database & Repository using Dspace Backup Commands in Terminal.

Here is how you take the backup of your Dspace Server. In this process we will create dspace backup file for below 3 things:

  1. PostgreSQL Database.

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

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

Dspace Backup Commands - Schedule Dspace backup using cron job

To setup Automatic Daily Dspace Backup, Follow the below Dspace Backup Commands.

First of all, Login to root user and create 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 gedit 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
(Change [username] & [password] as per your Dspace Database Credentials).

Make this file executable by following command:

sudo chmod +x dspace_backup/backup.sh
Now, Execute this file for testing by following command:
sh dspace_backup/backup.sh

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

crontab -e
It will ask to select an editor to open crontab, select nano editor.
Copy & Paste following lines at the end of crontab.
15 13 * * * 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 01:15PM daily and save it to dspace_backup folder. It will automatically delete backup files older than 180 days. 

Total Views: 1654