Automatic backup of disks in Yandex Cloud (with deletion of old versions)

Home button
Home button

Something bothered me to manually make backups of one project, and having driven away laziness, and gathering my will into a fist, I decided to automate this business.

I used this as a base original article from Yandex blog (view it first to understand what is at stake below).

But since I was too lazy to bother with zipping the archive, as described in the article, I simply copied the script into the script editor:

Don’t forget to create a file package.json something like this content:

{
  "name": "snappy-yc",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "author": "bogdoslavik",
  "license": "ISC",
  "dependencies": {
    "yandex-cloud": "^1.3.3"
  }
}

Version is important here. "yandex-cloud": "^1.3.3" for Node.js 12.

After testing the script, I made sure that it works, and created a trigger to run this function on a schedule.

But! My lazy nature understood that I would still have to manually delete old snapshots: new ones would not be created after the quota for the total size of snapshots or their number was exceeded.

Therefore, I again took my will into a fist, and decided to bring the holy backup business to the end.

Without going into details about a little fuss with created_at snapshot stored in magic format google.protobuf.timestamp this simple script was born:

const ycsdk = require("yandex-cloud/api/compute/v1");
const FOLDER_ID = process.env.FOLDER_ID;
const MAX_DAYS = process.env.MAX_DAYS;
async function handler(event, context) {
    const snapshotService = new ycsdk.SnapshotService();
    const diskService = new ycsdk.DiskService();
    const diskList = await diskService.list({
        folderId: FOLDER_ID,
    });
    
    console.log('Removing old snapshots');
    const {snapshots} = await snapshotService.list({folderId: FOLDER_ID});
    for ( let i in snapshots ) {
        const snapshot = snapshots[i];
        const createdMin = Date.now() / 1000 - (60 * 60 * 24 * MAX_DAYS);
        if ( snapshot && snapshot.createdAt && 
            snapshot.createdAt.seconds.low < createdMin ) {
            snapshotService.delete({snapshotId: snapshot.id});
        }
    }

    console.log('Iterating disks');
    for (const disk of diskList.disks) {
        console.log('disk.id',disk.id, 'name:', disk.name);
        if ('snapshot' in disk.labels) {
            snapshotService.create({
                folderId: FOLDER_ID,
                diskId: disk.id,
                description: disk.name
            });
            console.log('Creating snapshot');
        }
    }
    return {body: 'OK' }
}
exports.handler = handler;

Perfectionists can tweak console.log 🙂

It remains only to add the environment variable for the MAX_DAYS script – how many days to keep snapshots.

Add the MAX_DAYS variable in the function script editor.
Add the MAX_DAYS variable in the function script editor.

I’m sure it will save someone a couple of hours of precious time.
All the best and reliable bucks!

Similar Posts

Leave a Reply