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.
Create a directory where the Ramdisk data will be saved on system shutdown:
sudo mkdir /mnt/ramdisk_backup
vi /lib/systemd/system/ramdisk-sync.service
and populate with:
[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.
sudo systemctl enable ramdisk-sync.service
NOTE: The ramdisk-sync service will start automatically on system boot.
sudo reboot
sudo systemctl status ramdisk-sync
ls /mnt/ramdisk{,_backup}
NOTE: Both should be empty.
Copy some files to the Ramdisk /mnt/ramdisk.
Reboot again.
Check again the contents of both the /mnt/ramdisk and /mnt/ramdisk_backup directories.