Community

How to delete snapshots after X days (Windows cli master)

Creating a snapshot everyday with an automated task is easy. But what if you do not want to end up with endless snapshots without manually deleting them?

Here is a powershell script which uses the windows cli master to delete every snapshot that is older than X days:

#$count_days indicates after how many days a snapshot shall be deleted
$count_days = 11;
#all snapshots set in var $snapshots as string
$snapshots = profitbricks snapshot list | Out-String;
#$count_chars indictaes the chars in the 2nd line. used to measure and split the strings
$count_chars = ((profitbricks snapshot list)[1]).Length + 2;
#skip the first 11 chars and 3 first lines (not part of the snapshots)
$snapshots = $snapshots.substring(11 + $count_chars * 3);

#read snapshot-IDs and set them in $id_array as well as the date in $date_array
while ($snapshots.Length -gt 10){

    $count_skip = $count_chars - 33;

    [string[]] $id_array += $snapshots.substring(0,36) | Out-String;
    [string[]] $date_array += $snapshots.substring($count_skip,10) | Out-String;

    $snapshots = $snapshots.substring($count_chars);

}

#todays date
$today = Get-Date -format o | Out-String;
$today = $today.Substring(0,10);

#loop to compare time_spans
for ($i=0;$i -lt $id_array.Length;$i++){

$time_span = [datetime]$today - [datetime]$date_array[$i];

    #look for the number of days
    if ($time_span.TotalDays -gt $count_days){

    #read snapshot-ID
    $snap_del = $id_array[$i].substring(0,$id_array[$i].Length-2).ToString();

    #delete snapshot
    profitbricks snapshot delete -f -i "$snap_del"

    }
}

Save it as a ".ps1" file. Windows task is as following:

program: %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe

arguments: -noprofile -NoLogo -noninteractive -ExecutionPolicy Bypass -File "C:\Your path\scriptname.ps1"

path: C:\Your path

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

Hi Vincent,

thanks for your helpful contribution.

Please keep in mind that snapshots are not backups. ProfitBricks snapshots are a clone of your volume with all its contents at the time you created the snapshot. If you have an application that keeps some data cached in memory without writing it to disk, this data is not included in the snapshot. Especially for data bases this may lead to an inconsistant state. Note also, that these snapshots always are a complete copy, which also has an impact on the restore time for large volumes. If you only need to restore partial data, e.g. a configuration file, you're better off with a backup solution, that has features like incremental backups, file level restore etc.

I also want to delete the snapshots from X days from my 3D printer Whenever I having a problem with my printer I getting the instant connectivity to the Epson Printer Support team. Having an issue then anyone connects to the support expert team.

You need to write the script using Python. You have to find the date which is 30 days old with respect to the current date. For that we will use time delta objects comes with DateTime class. View Printer Not Activated Error Code 20 for printer error suggestions.