Creating Snapshot

The following script creates Snapshot volumes for each generation. This script is an operation that uses SnapOPC+ to create a backup of generation-managed incremental data and is executed only once when the environment is built. For more information, see the description of Generation Managed Differential Data Backup with SnapOPC+.

The sequence of processing the script processing is as follows:

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

  2. Repeat 1 for the number of generations.

snapopcplus_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 of the source volume to create SnapOPC+.
volume_id = 100001
# TPP ID to create snapshots.
tpp_id = 0
# Base name of the snapshots.
snapshot_basename = "snap#"
# Number of generations of the SnapOPC+.
generation_count = 5


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

    def snapopcplus_create(self, volume_id, tpp_id, snapshot_basename,
                           generation_count):
        """Create snapshots using SnapOPC+.

        Args:
            volume_id (int): Volume ID of the source volume.
            tpp_id (int): TPP ID to create snapshots.
            snapshot_basename (str): Base name of the snapshots.
            generation_count (int): Number of generations of the SnapOPC+.

        Returns:
            list[int]: List of volume IDs of the created snapshot. If failed
              in the middle, returns list of the created snapshot volume IDs.
              If all the snapshots failed, returns empty list.
        """
        snapshot_volume_id_list = []
        for i in range(generation_count):
            # Create the Snapshot Volume name.
            snapshot_volume_name = snapshot_basename + str(i)
            body = {
                "name": snapshot_volume_name,
                "tpp_id": tpp_id,
                "is_manual_snapshot": False,
            }
            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())
                break

            # 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())
                break
            snapshot_volume_id_list.append(
                    int(job_r.json()["resource_href_list"][0].split("/")[4]))

        return snapshot_volume_id_list


def main():
    if generation_count < 1 or 512 <= generation_count:
        print("The generation_count is out of range. "
              "(Must be 0 < generation_count <= 512.)")
        return False

    storage = EtdxApi(storage_url)

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

    # Initialize return value as True, since the procedure continues even if
    # error occurs.
    rtn = True

    print("Creating snapshots of the volume (ID: {0!s}) using SnapOPC+."
          .format(volume_id))
    print("Generation count:", generation_count)

    # Create SnapOPC+
    snapshot_volume_id_list = storage.snapopcplus_create(volume_id,
                                                         tpp_id,
                                                         snapshot_basename,
                                                         generation_count)
    if snapshot_volume_id_list == []:
        print("Failed to create all snapshots.")
        return False
    if len(snapshot_volume_id_list) != generation_count:
        print("Failed to create some snapshots.")
        print("The number of generations of SnapOPC+:",
              len(snapshot_volume_id_list))
        rtn = False
    print("List of the created snapshot volume IDs:", snapshot_volume_id_list)

    # Get information of the SnapOPC+
    r = storage.get("/api/v1/volume/{0!s}/copysession?backup_type=snapshot"
                    "&is_manual_snapshot=false".format(volume_id))
    print(r)
    print(r.json())

    # Logout
    storage.logout()
    return rtn


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