Example #1
0
    private async Task DeleteStaleBackupsAsync()
    {
        var instanceName = InstanceName.FromProjectInstance(ProjectId, InstanceId);
        var backups      = DatabaseAdminClient.ListBackups(instanceName, pageSize: 200).ToList();

        if (backups.Count < 50)
        {
            return;
        }

        var deleteCount = Math.Max(30, backups.Count - 50);

        var backupsToDelete = backups
                              .OrderBy(db => long.TryParse(
                                           db.BackupName.BackupId.Replace("my-enc-backup-", ""),
                                           out long creationDate) ? creationDate : long.MaxValue)
                              .Take(deleteCount);

        // Delete the backups.
        foreach (var backup in backupsToDelete)
        {
            try
            {
                await DatabaseAdminClient.DeleteBackupAsync(backup.BackupName);
            }
            catch (Exception) { }
        }
    }
    private async Task DeleteStaleBackupsAndDatabasesAsync()
    {
        DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.Create();
        var instanceName = InstanceName.FromProjectInstance(ProjectId, InstanceId);
        var databases    = databaseAdminClient.ListDatabases(instanceName)
                           .Where(c => c.DatabaseName.DatabaseId.StartsWith("my-db-") || c.DatabaseName.DatabaseId.StartsWith("my-restore-db-"));
        var databasesToDelete = new List <string>();

        // Delete all the databases created before 48 hrs.
        var timestamp = DateTimeOffset.UtcNow.AddHours(-48).ToUnixTimeMilliseconds();

        foreach (var database in databases)
        {
            var databaseId = database.DatabaseName.DatabaseId.Replace("my-restore-db-", "").Replace("my-db-", "");
            if (long.TryParse(databaseId, out long dbCreationTime) && dbCreationTime <= timestamp)
            {
                databasesToDelete.Add(database.DatabaseName.DatabaseId);
            }
        }

        await Console.Out.WriteLineAsync($"{databasesToDelete.Count} old databases found.");

        // Get backups.
        ListBackupsRequest request = new ListBackupsRequest
        {
            ParentAsInstanceName = instanceName,
            Filter = $"database:my-db-"
        };
        var backups = databaseAdminClient.ListBackups(request);

        // Backups that belong to the databases to be deleted.
        var backupsToDelete = backups.Where(c => databasesToDelete.Contains(DatabaseName.Parse(c.Database).DatabaseId));

        await Console.Out.WriteLineAsync($"{backupsToDelete.Count()} old backups found.");

        // Delete the backups.
        foreach (var backup in backupsToDelete)
        {
            try
            {
                DeleteBackupSample deleteBackupSample = new DeleteBackupSample();
                deleteBackupSample.DeleteBackup(ProjectId, InstanceId, backup.BackupName.BackupId);
            }
            catch (Exception) { }
        }

        // Delete the databases.
        foreach (var databaseId in databasesToDelete)
        {
            try
            {
                await DeleteDatabaseAsync(databaseId);
            }
            catch (Exception) { }
        }
    }
        public static void DelelteBackups()
        {
            DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.Create();

            //delete backup contains "a"
            var dataTime          = DateTime.UtcNow.AddDays(1).ToString("yyyy-MM-dd");
            var listBackupRequest = new ListBackupsRequest
            {
                Parent = InstanceName.Format(projectId, instanceId),
                Filter = $"create_time < {dataTime}"
            };

            var backups = databaseAdminClient.ListBackups(listBackupRequest);

            foreach (var backup in backups)
            {
                var deleteBackupRequest = new DeleteBackupRequest()
                {
                    Name = backup.Name
                };
                databaseAdminClient.DeleteBackup(deleteBackupRequest);
            }
        }
Example #4
0
        public static object SpannerGetBackups(
            string projectId, string instanceId, string databaseId, string backupId)
        {
            // Create the DatabaseAdminClient instance.
            DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.Create();

            InstanceName parentAsInstanceName = InstanceName.FromProjectInstance(projectId, instanceId);

            // List all backups.
            Console.WriteLine("All backups:");
            var allBackups = databaseAdminClient.ListBackups(parentAsInstanceName);

            PrintBackups(allBackups);

            ListBackupsRequest request = new ListBackupsRequest
            {
                ParentAsInstanceName = parentAsInstanceName,
            };

            // List backups containing backup name.
            Console.WriteLine($"Backups with backup name containing {backupId}:");
            request.Filter = $"name:{backupId}";
            var backupsWithName = databaseAdminClient.ListBackups(request);

            PrintBackups(backupsWithName);

            // List backups on a database containing name.
            Console.WriteLine($"Backups with database name containing {databaseId}:");
            request.Filter = $"database:{databaseId}";
            var backupsWithDatabaseName = databaseAdminClient.ListBackups(request);

            PrintBackups(backupsWithDatabaseName);

            // List backups that expire within 30 days.
            Console.WriteLine("Backups expiring within 30 days:");
            string expireTime = DateTime.UtcNow.AddDays(30).ToString("O");

            request.Filter = $"expire_time < \"{expireTime}\"";
            var expiringBackups = databaseAdminClient.ListBackups(request);

            PrintBackups(expiringBackups);

            // List backups with a size greater than 100 bytes.
            Console.WriteLine("Backups with size > 100 bytes:");
            request.Filter = "size_bytes > 100";
            var backupsWithSize = databaseAdminClient.ListBackups(request);

            PrintBackups(backupsWithSize);

            // List backups created in the last day that are ready.
            Console.WriteLine("Backups created within last day that are ready:");
            string createTime = DateTime.UtcNow.AddDays(-1).ToString("O");

            request.Filter = $"create_time >= \"{createTime}\" AND state:READY";
            var recentReadyBackups = databaseAdminClient.ListBackups(request);

            PrintBackups(recentReadyBackups);

            // List backups in pages.
            Console.WriteLine("Backups in batches of 5:");
            int    pageSize      = 5;
            string nextPageToken = string.Empty;

            do
            {
                var           response    = databaseAdminClient.ListBackups(parentAsInstanceName, nextPageToken);
                Page <Backup> currentPage = response.ReadPage(pageSize);
                PrintBackups(currentPage);
                nextPageToken = currentPage.NextPageToken;
            } while (!string.IsNullOrEmpty(nextPageToken));

            return(0);
        }
    public IEnumerable <Backup> ListBackups(string projectId, string instanceId, string databaseId, string backupId)
    {
        // Create the DatabaseAdminClient instance.
        DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.Create();

        InstanceName parentAsInstanceName = InstanceName.FromProjectInstance(projectId, instanceId);

        // List all backups.
        Console.WriteLine("All backups:");
        var allBackups = databaseAdminClient.ListBackups(parentAsInstanceName);

        PrintBackups(allBackups);

        ListBackupsRequest request = new ListBackupsRequest
        {
            ParentAsInstanceName = parentAsInstanceName,
        };

        // List backups containing backup name.
        Console.WriteLine($"Backups with backup name containing {backupId}:");
        request.Filter = $"name:{backupId}";
        var backupsWithName = databaseAdminClient.ListBackups(request);

        PrintBackups(backupsWithName);

        // List backups on a database containing name.
        Console.WriteLine($"Backups with database name containing {databaseId}:");
        request.Filter = $"database:{databaseId}";
        var backupsWithDatabaseName = databaseAdminClient.ListBackups(request);

        PrintBackups(backupsWithDatabaseName);

        // List backups that expire within 30 days.
        Console.WriteLine("Backups expiring within 30 days:");
        string expireTime = DateTime.UtcNow.AddDays(30).ToString("O");

        request.Filter = $"expire_time < \"{expireTime}\"";
        var expiringBackups = databaseAdminClient.ListBackups(request);

        PrintBackups(expiringBackups);

        // List backups with a size greater than 100 bytes.
        Console.WriteLine("Backups with size > 100 bytes:");
        request.Filter = "size_bytes > 100";
        var backupsWithSize = databaseAdminClient.ListBackups(request);

        PrintBackups(backupsWithSize);

        // List backups created in the last day that are ready.
        Console.WriteLine("Backups created within last day that are ready:");
        string createTime = DateTime.UtcNow.AddDays(-1).ToString("O");

        request.Filter = $"create_time >= \"{createTime}\" AND state:READY";
        var recentReadyBackups = databaseAdminClient.ListBackups(request);

        PrintBackups(recentReadyBackups);

        // List backups in pages.
        foreach (var page in databaseAdminClient.ListBackups(parentAsInstanceName, pageSize: 5).AsRawResponses())
        {
            PrintBackups(page);
        }

        return(allBackups);
    }