Saturday, February 15, 2025

How to automate the backup of DSpace database and folders

Here is a method to automate the backup of the PostgreSQL database and DSpace folders (e.g. assetstore) using a shell script and scheduling through cronjob.

Find the pg_hba.conf file change the method to 'trust'

The pg_hba. conf file resides within the PostgreSQL data directory and consists of a series of lines, each specifying a connection type, database, user, IP address, authentication method, and other details.
The file is located at Ubuntu 24.04 at /etc/postgresql/16/main/pg_hba.conf. Location may be different in Debian Linux. Check the location manually and open the file.

sudo mousepad /etc/postgresql/16/main/pg_hba.conf

Find the following lines and change the term peer to trust,

local   all             postgres                                peer
local   all             all                                     peer
host    all             all             127.0.0.1/32            peer

The output will look like this,

Save and close the file. Restart the PostgreSQL service.

sudo systemctl restart  postgresql

Make a shell script to take the backup

Create a backup folder on your computer. Apply the following command to create a local backup folder on your home folder. Change the location of the folder in the below command before applying it.

sudo mkdir /home/username/backup

The backup location can be a folder inside your cloud storage service like PCloud or Dropbox.
Open a new file inside the folder /usr/local/bin/ to create a shell script.

sudo mousepad /usr/local/bin/dspace-backup.sh

Copy the following content into the file. Make necessary changes in the file, e.g. location of the backup folder, PostgreSQL database name and user. 

#!/bin/bash
#backup of PostgreSQL database
BACKUP_DIR="/home/username/backup/"
FILE_NAME=$BACKUP_DIR`date +%d-%m-%Y-%I-%M-%S-%p`.sql
pg_dump -U db_user db_name > $FILE_NAME

# backup of assetstore folder
zip -r /home/username/backup/assetstore-$(date +%d-%m-%Y-%H.%M).zip /dspace/assetstore

#Delete older files more than 7 days will delete.
find /home/username/backup/* -mtime +7 -exec rm {} \;

Save and close the file.

Make the shell script executable

sudo chmod a+x /usr/local/bin/dspace-backup.sh

Add the crontab entry

Apply the following command to open crontab

sudo crontab -e

Copy and paste the following entries at the end of the editor.
Copy the below lines to the bottom part of the crontab file.

#DSpace backup
10 20 * * * /usr/local/bin/dspace-backup.sh

Change the timing of the backup. Here, backup takes at 8:10 PM.

If you want a backup every 5 hours, apply the following entry.

# DSpace backup
0 */5 * * * /usr/local/bin/dspace-backup.sh

Reference