Table of Contents

Ubuntu - Disk - Ramdisk - Persisting Ramdisk Data

A Ramdisk is a temporary storage device.

When the computer is shutdown all the data within the Ramdisk is erased.

To ensure this data is retained, the approach taken here is:

NOTE: The syncing from the Ramdisk to the hard disk and vice-versa will be slow, but at least the data will be stored permanently.

  • In essence, the Ramdisk acts here as a cache.

Create backup directory

Create a directory where the Ramdisk data will be saved on system shutdown:

sudo mkdir /mnt/ramdisk_backup

Create a system service file

vi /lib/systemd/system/ramdisk-sync.service

and populate with:

/lib/systemd/system/ramdisk-sync.service
[Unit]
Before=umount.target
 
[Service]
Type=oneshot
User=root
ExecStartPre=/bin/chown -Rf peter /mnt/ramdisk/
ExecStart=/usr/bin/rsync -ar /mnt/ramdisk_backup/ /mnt/ramdisk/
ExecStop=/usr/bin/rsync -ar /mnt/ramdisk/ /mnt/ramdisk_backup/
ExecStopPost=/bin/chown -Rf peter /mnt/ramdisk_backup
RemainAfterExit=yes
 
[Install]
WantedBy=multi-user.target

NOTE: The directory where the Ramdisk is mounted and where the backup data will be saved must have the same file permissions.


Enable the ramdisk-sync service

sudo systemctl enable ramdisk-sync.service

NOTE: The ramdisk-sync service will start automatically on system boot.


Reboot

sudo reboot

Check whether the ramdisk-sync service is running

sudo systemctl status ramdisk-sync

Check the contents of the /mnt/ramdisk and /mnt/ramdisk_backup directories

ls /mnt/ramdisk{,_backup}

NOTE: Both should be empty.


Test

Copy some files to the Ramdisk /mnt/ramdisk.

Reboot again.

Check again the contents of both the /mnt/ramdisk and /mnt/ramdisk_backup directories.