My goal is to have a certain directory to be available as tmpfs. There will be some modifications during server uptime in this dir and those modifications must be synced to non-tmpfs persistent dir on HDD over rsync.

After server boot the latest version from non-tmpfs persistent dir must be moved to tmpfs and rsync syncing to be started.

I'm afraid that rsync will erase non-tmpfs backup if tmpfs dir will be empty..

I'm doing it in this way right now:

  1. create tmpfs partition in /etc/fstab
  2. cat /etc/rc.local (pseudocode)

    delete "tmpfs rsync" cronjob from /var/spool/cron/crontabs if there is any

    cp -r /path/to/non-tmpfs-backup /path/to/tmpfs/dir

    append /var/spool/cron/crontabs with "tmpfs rsync" cronjob

What do you think?


Create some sort of seed file deep in your non-tmpfs directory and only rsync back to non-tmpfs if it exists (meaning the "boot" copy worked), so something like:


mount /path/tmpfs
rsync -aq --delete /path/non-tmpfs/ /path/tmpfs/


if [ -f /path/tmpfs/some/deep/location/filesgood.txt ]; then
  rsync -aq --delete /path/tmpfs/ /path/non-tmpfs/

It's not perfect but, if you enhance that (by looking for 5 "cookie" files during cron if you want to in different directories, e.g.), it should be pretty safe.


I wanted to just comment on troyengel's excellent answer above, but lack the rep on serverfault to do so.

One fix is that there should be a space between -aq and --delete.

Also, I've created a mirroring script that will check for the inited file lock in /tmpfs drive, and mirror from disk to RAM the first run, and from RAM to disk thereafter:

if [ -f /workspace/z/some/deep/location/inited ]; then
  ionice -c2 -n7 nice -n 19 rsync -aq --delete /workspace/* $HOME/workspace 1>/dev/null
  ionice -c2 -n0 nice -n 19 rsync -aq --delete $HOME/workspace/* /workspace 1>/dev/null

Note that your lock should be in /z/deep/location so it is always be the last file synced, to prevent data loss.

I've also included nice and ionice parameters so the initial mirroring is high priority, and mirroring from RAM to disk is lower priority.

If you are brave, use -c1 -n7 for realtime initial mirroring, and if you expect frequent idle time, you can use ionice with -c3 and no -n flag to only mirror back when the disk is not in use.

I save this script to /usr/bin/Mirror, and run it every five minutes with cron.

crontab -e

*/5 * * * * /usr/bin/Mirror

This is especially useful if you want to store your working files on an encrypted harddrive, but work on them in RAM.

If you wish to run mirror on system startup and shutdown, you can probably use /etc/inittab


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.