I created a Google Cloud Compute Engine and have it backuped (snapshot) nightly. I have a second VM in the same project, which is a clone of the first one. Is it possible to updated the second VM instance every time with the new just created snapshot of the first? So i have a failback server which is at worst 24h old?
Asked
Active
Viewed 202 times
2 Answers
3
You will most like have to create a new instance from the snapshot, validate the new instance is healthy and delete your second VM instance. The gcloud sdk has a flag for that -
gcloud compute instances create
with --source-snapshot=SNAPSHOT
flag.
I am not sure about your case, but it seems the newly release GCP machine images might be a good alternative to consider. It provides features not available in disk snapshot such as multiple disk backup, instance cloning and replication. You can create an instance from a machine image with all the instance properties, configuration, data intact.

Daniel t.
- 9,291
- 1
- 33
- 36
-
Okay, i will try it out with daily snapshot instances and a static IP that i will attach. Maybe i also give Images a shot. Thanks – Jonas Laux Mar 28 '20 at 10:47
1
If anyone faces the same problem, i wrote a Cloud Function and scheduled it every day. The steps are:
- Get latest Snapshot
- Delete old VM
- Create new VM
- Put the newly created VM IP to Cloudflare LoadBalancer
This is the Code for it:
const Compute = require("@google-cloud/compute");
const axios = require("axios");
const compute = new Compute({
projectId: "myProjectID",
keyFilename: "./gcloud_key.json"
});
exports.updateFailOverVM = async (pubSubEvent, context) => {
try {
const snapshots = await compute.getSnapshots({
orderBy: "creationTimestamp desc"
});
const snapshot = snapshots[0][0];
console.log(`Going to use Snapshot: ${snapshot.name}`);
const sourceSnapshot = `https://www.googleapis.com/compute/v1/projects/myProjectID/global/snapshots/${snapshot.name}`;
const zone = compute.zone("MySelectedZone");
const vm = zone.vm("MyVMName");
// Delete old FailOver
console.log("start deleting old VM");
const [deleteOperation] = await vm.delete();
deleteOperation.on("complete", async function(metadata) {
console.log("finished deleting old VM");
// Create new VM
console.log("start creating new VM");
const [newVM, createOperation] = await vm.create({
http: false,
https: true,
machineType: "n1-standard-1",
disks: [
{
initializeParams: {
sourceSnapshot
},
sizeGb: 50,
type:
"projects/MyProjectID/zones/mySelectedZone/diskTypes/pd-standard",
boot: true
}
]
});
createOperation.on("complete", async function(metadata) {
const [newVMMeta] = await vm.getMetadata();
const newIP = newVMMeta.networkInterfaces[0].accessConfigs[0].natIP;
console.log("finished creating new VM");
// Route Cloudflare to new IP
console.log("Put new IP to Cloudflare");
await axios({
method: "patch",
url:
"https://api.cloudflare.com/client/v4/user/load_balancers/pools/MyPoolID",
data: {
origins: [
{
name: "MyPoolName",
address: newIP,
enabled: true,
weight: 1
}
]
},
headers: {
"X-Auth-Email": "MyCloudflareEmail",
"X-Auth-Key": "MyCloudflareAuthKey"
}
});
console.log("Finished putting IP to Cloudflare");
});
});
} catch (err) {
console.error(err);
}
};

Jonas Laux
- 123
- 5