You can back up a RAM disk before your instance restarts to preserve the RAM disk data until the instance starts up again. Back up your data to a persistent disk to preserve it.
-
Create and mount a persistent disk to use as a backup disk for your RAM disk. Make sure the disk is big enough to contain the information in the RAM disk.
-
Create a shutdown script for your instance with an rsync command that writes the RAM disk contents to the backup volume. For this example, use the gcloud tool to add the shutdown-script metadata to the instance with the RAM disk mounted at /mnt/ram-disk and the persistent disk mounted at /mnt/ram-disk-backup.
gcloud compute instances add-metadata example-instance --metadata shutdown-script="#! /bin/bash
rsync -a --delete --recursive --force /mnt/ram-disk/ /mnt/ram-disk-backup/
EOF"
-
Optionally, you can also create a startup script that restores the files back to the RAM disk when the instance starts again. Use the gcloud tool to add the startup-script metadata to the instance.
gcloud compute instances add-metadata example-instance --metadata startup-script="#! /bin/bash
rsync -a --recursive --force /mnt/ram-disk-backup/ /mnt/ram-disk/
EOF"