Scheduling DSpace Backups with Cron jobs

Share this post on:

For DSpace installations running on Linux distributions, cron is a simple and reliable way to automate backups of the assetstore, log directories, and PostgreSQL database. This guide shows how to schedule daily backups using cron and how to take a manual database backup when needed.

Note: Commands and paths here reflect a typical older DSpace installation (e.g. /dspace/assetstore and Linux user dspace). Adjust them for your environment.

What You Should Back Up

At a minimum, a DSpace install needs regular backups of:

  • Assetstore directory (all uploaded bitstreams).​
  • Log directory (useful for debugging and audit trails).
  • PostgreSQL database (all metadata, configuration stored in DB).

We focuses on automating backups for the assetstore and log folders, plus a manual method for backing up the PostgreSQL database.

Understanding the Cron Schedule Format

Cron uses a simple five‑field format:

minute hour day_of_month month day_of_week command

For example:​

  • 45 16 * * * means “run at 16:45 (4:45 PM) every day, every month, every weekday.”​
  • * in a field means “all valid values” for that field (every minute, every hour, etc.).

We will use this format to schedule the DSpace backup commands once per day.

Schedule Assetstore and Log Backups with Cron

The goal is to create two cron entries that compress the assetstore and log directories into a backup folder at a fixed time each day.​

1. Open a Terminal and switch to root

On older desktop-style Linux setups, you can open Terminal from the Applications menu, then run:​

sudo su

Enter your password when prompted.​

2. Edit the root crontab

Run:

crontab -e
  • On first use, the system may ask you to choose an editor; select nano for simplicity.​
  • The crontab file will open in the chosen editor.​

3. Add cron entries for assetstore and logs

Scroll to the end of the file and add these two lines:​

45 16 * * * zip -r /home/dspace/backup/assetstore.zip /dspace/assetstore
45 16 * * * zip -r /home/dspace/backup/log.zip /dspace/log

Explanation:

  • 45 16 * * * runs the command each day at 16:45 (4:45 PM).
  • zip -r recursively creates a compressed archive.
  • /home/dspace/backup/assetstore.zip and /home/dspace/backup/log.zip are the backup files to create/overwrite.
  • /dspace/assetstore and /dspace/log are the source directories from your DSpace installation.

You must adjust the destination folder to match your system, for example:​

/home/dspace/backup

If your dspace user or home directory is different, update the path accordingly (e.g. /home/server/dspace_backup).

4. Save and exit the crontab

In nano:​

  • Press Ctrl + o, then Enter to save the file.
  • Press Ctrl + x to exit.

The cron daemon will automatically pick up the new jobs, and backups will now run daily at 4:45 PM and deposit compressed files in your chosen backup folder.

Customize and Extend the Backup

You can easily adjust timing and add more folders in the same style.​

  • To change the time, modify the minute and hour fields. For example, 15 1 * * * runs at 1:15 PM.
  • To back up another directory (e.g. config), add another line:bash45 16 * * * zip -r /home/dspace/backup/config.zip /dspace/config ```[1][3]

Make sure the target backup directory exists and has enough space; create it if needed:​

bashmkdir -p /home/dspace/backup
chown dspace:dspace /home/dspace/backup

Manual PostgreSQL Database Backup

For older DSpace installs using PostgreSQL, you can take an ad‑hoc database backup with pg_dump.

1. Open Terminal and become root

sudo su

2. Run pg_dump with gzip compression

Use the typical DSpace database name (dspace) and database user (dspace), adjusting as needed:

pg_dump -U dspace -h localhost -Fc dspace | gzip > dspace-$(date +%Y-%m-%d-%H.%M.%S).gz

Explanation:

  • -U dspace connects as the dspace database user.
  • -h localhost connects to PostgreSQL on the local server.
  • -Fc uses PostgreSQL’s custom format, which is suitable for restore and compression.​
  • The output is piped to gzip and written to a file named with the current date/time, for example:
    dspace-2016-01-26-16.45.00.gz.​

When prompted, enter the password for the dspace database user or the Linux user as configured.

You can then move this .gz file to a safe location such as another disk, external storage, or an off‑site server.


Notes and Caveats

  • Paths like /dspace/assetstore and user names like dspace are conventional for older DSpace installs; verify them on your system with ls /dspace and id dspace.
  • The examples above overwrite assetstore.zip and log.zip each day; if you want daily archives with unique names, include the date in the filename (e.g. assetstore-$(date +%Y-%m-%d).zip).
  • For a fully automated solution (database + assetstore + log + cleanup of old backups), you can wrap these commands into a shell script and call it from cron, similar to more recent DSpace backup practices.

Author: Rupinder Singh

I am a tireless intelligence seeker, coincidentally I am a computer guy too, who is passionate about Information Tools and Open-Source software. I Read Books, play Computer Games, Climb Mountains, when I am not changing the code.

View all posts by Rupinder Singh >

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.