Ejemplo n.º 1
0
        private static async Task BackupContainerMounts(ContainerListResponse container, DockerClient client)
        {
            Console.WriteLine($"┏━ Starting backup for: {container.Names.First()}");

            var fields = ContainerBackupFields.Create(container);

            if (!fields.IsValid())
            {
                Console.WriteLine($"Container {container.ID} is marked for backups, but does not have all required labels");
                return;
            }

            try
            {
                await client.Containers.PauseContainerAsync(container.ID);

                var mountBackupTasks = new List <Task>();

                foreach (var mount in container.Mounts)
                {
                    var task = BackupMount(mount, client, fields).ContinueWith(status =>
                    {
                        Console.WriteLine($"┗> {mount.Source}:{mount.Destination}");
                        return(status);
                    });

                    mountBackupTasks.Add(task);
                }

                await Task.WhenAll(mountBackupTasks);

                Console.WriteLine(); // add an enter to create distance between containers
            }
            catch (Exception e)
            {
                Console.Error.WriteLine($"Error while running backup for container {container.Names.First()}. \n {e.Message}");
            }
            finally
            {
                await client.Containers.UnpauseContainerAsync(container.ID);
            }
        }
Ejemplo n.º 2
0
        private static async Task <Task> BackupMount(MountPoint mount, DockerClient client, ContainerBackupFields fields)
        {
            var cleanContainerPath = mount.Destination.Replace("/", "_");

            var backupContainer = await client.Containers.CreateContainerAsync(new CreateContainerParameters
            {
                Image = "jorik/docker-mount-backup-tool",
                Env   = new List <string>
                {
                    $"BACKUP_PROJECT={fields.Project}",
                    $"BACKUP_ROLE={fields.Role}",
                    $"BACKUP_CONTAINER_PATH={cleanContainerPath}",
                    $"AWS_SECRET_ACCESS_KEY={_appSettings.AWSSecretAccessKey}",
                    $"AWS_ACCESS_KEY_ID={_appSettings.AWSAccessKeyId}"
                },
                HostConfig = new HostConfig
                {
                    AutoRemove = true,
                    Binds      = new List <string>
                    {
                        $"{mount.Source}:/data:ro"
                    }
                }
            });

            var startResult =
                await client.Containers.StartContainerAsync(backupContainer.ID, new ContainerStartParameters());

            if (!startResult)
            {
                Console.Error.WriteLine($"Backup container created but unable to start: {backupContainer.ID}");
                return(Task.CompletedTask);
            }

            return(client.Containers.WaitContainerAsync(backupContainer.ID));
        }