Creating a Snapshot without Generation Management

The following script creates a Snapshot without generation management. Use this script for operations that use SnapOPC to back up differential data without generation management. For more information, see the description of Using SnapOPC to Back Up Differential Data Without Generation Management.

Note

During script execution, to ensure the integrity of Snapshot data, operations volume I/O and applications must be stopped momentarily to create a quiescent point. After the script is complete, the logical copy of the update data is complete and business can resume.

The sequence of script processing is as follows.

  1. Create a Snapshot using POST /volume/{volume_id}/snapshot API.

snapopc_create.py


import sys

from eternus_rest import EtdxBaseApi

# Change the following values for your environment.
storage_url = "https://192.168.0.1:5665"
storage_user_name = "username"
storage_password = "password"

# Change the following values as required.
# Volume ID to create snapshot.
volume_id = 100001
# TPP ID to create a snapshot.
tpp_id = 0
# Name of the snapshot.
snapshot_name = "snap#0"


class EtdxApi(EtdxBaseApi):
    def __init__(self, base_url, boxid=None, proxies=None):
        super().__init__(base_url, boxid=boxid, proxies=proxies)

    def snapopc_create(self, volume_id, tpp_id, snapshot_name):
        """Create snapshot using SnapOPC.

        Args:
            volume_id (int): Volume ID to create snapshot.
            tpp_id (int): TPP ID to create a snapshot.
            snapshot_name (str): Name of the snapshot.

        Returns:
            int: Volume ID of the created snapshot. If failed, returns a value
              of -1. This value cannot be used as volume ID.
        """
        body = {
            "name": snapshot_name,
            "tpp_id": tpp_id,
            "is_manual_snapshot": True
        }
        r = super().post("/api/v1/volume/{0!s}/snapshot".format(volume_id),
                         body)
        if r.status_code != 202:
            print("Failed to request a snapshot creation.")
            print(r)
            print(r.json())
            return -1

        # Wait for the job completion
        job_r = super().wait_job(r.json()["job_id"])
        if job_r.json()["status"] != "Success":
            print("Failed to create a snapshot.")
            print(job_r)
            print(job_r.json())
            return -1
        backup_volume_href = job_r.json()["resource_href_list"][0]
        backup_volume_id = int(backup_volume_href.split("/")[4])

        return backup_volume_id


def main():
    storage = EtdxApi(storage_url)

    # Login
    if not storage.login(storage_user_name, storage_password):
        return False

    print("Creating snapshot of the volume (ID: {0!s}) using SnapOPC."
          .format(volume_id))

    # Create snapshot
    snapshot_volume_id = storage.snapopc_create(volume_id, tpp_id,
                                                snapshot_name)
    if snapshot_volume_id == -1:
        return False
    print("Created volume ID of snapshot:", snapshot_volume_id)

    # Get the snapshot information
    r = storage.get("/api/v1/volume/{0!s}/copysession"
                    .format(snapshot_volume_id))
    print(r.json())

    # Logout
    storage.logout()

    return True


if __name__ == '__main__':
    if not main():
        sys.exit(1)