Community

Automate snapshot creation

We have been trying to use the API + Python to automate regular snapshot creation for all or some of the servers in a specific DC. We are having some difficulty (one, that we are not phyton experts :) ...yet) Any pointers of samples on how to accomplish this would be greta.

The main idea is to deleted old snapshots and create new one. Ideally we then would automate this script to run once a week o month based on a disaster recovery/back schedule.

thanks!

 
  • **bold**
  • _italics_
  • `code`
  • ```code block```
  • # Heading 1
  • ## Heading 2
  • > Quote
 

If you're trying to use Python with our API, I would strongly suggest you to take a look at the Python SDK. I would also not recommend you to take a snapshot of your volumes very often, since the snapshot is a clone of the entire volume and not an incremental/differential image of your previous one. For a backup purpose, I would advise you to implement a backup solution using a 3rd party tool like Amanda, R1soft, Bacula, BitCalm etc., something specialized in backups.

A way to approach the problem would be by having two separate scrips, one for deletion and one for creation. You can run a cron job(linux) or a planned Task(windows) for the two scripts on a weekly basis. You could also combine the two procedures in the same script.

Creating Snapshots: For the desired datacenter, you could identify the volumes(volume_id) and create_snapshot(snapshot_id):

volume = client.create_snapshot( 
                                datacenter_id=datacenter_id,
                                volume_id=volume_id,snapshot_name='<URLENCODED_SNAPSHOT_NAME>',
                                snapshot_description='<URLENCODED_SNAPSHOT_DESCRIPTION>')

During creation you could also add a string to the name of the snapshot and that way it is easier to identify it. Something like: snapshot_name = "Save" + volume_id.

Deleting Snapshots: In a similar way you can perform a snapshot deletion:

from profitbricks.client import ProfitBricksService
                                                snapshot_id = '7df81087-5835-41c6-a10b-3e098593bba4'
                                                client = ProfitBricksService(
                                                username='username', password='password')
                                                snapshot = client.delete_snapshot(snapshot_id=snapshot_id)

After retrieving the list of all snapshots using list_snapshots(), you can identify the ones that start with the word "Save" and have a certain time stamp and perform the deletion.

Please check here for more samples using the Python SDK.