github.com/minio/madmin-go@v1.7.5/examples/heal-manual.go (about)

     1  //go:build ignore
     2  // +build ignore
     3  
     4  //
     5  // MinIO Object Storage (c) 2022 MinIO, Inc.
     6  //
     7  // Licensed under the Apache License, Version 2.0 (the "License");
     8  // you may not use this file except in compliance with the License.
     9  // You may obtain a copy of the License at
    10  //
    11  //      http://www.apache.org/licenses/LICENSE-2.0
    12  //
    13  // Unless required by applicable law or agreed to in writing, software
    14  // distributed under the License is distributed on an "AS IS" BASIS,
    15  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  // See the License for the specific language governing permissions and
    17  // limitations under the License.
    18  //
    19  
    20  package main
    21  
    22  import (
    23  	"context"
    24  	"encoding/json"
    25  	"fmt"
    26  	"log"
    27  	"os"
    28  	"time"
    29  
    30  	"github.com/minio/madmin-go"
    31  )
    32  
    33  func main() {
    34  	// Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY are
    35  	// dummy values, please replace them with original values.
    36  
    37  	// API requests are secure (HTTPS) if secure=true and insecure (HTTP) otherwise.
    38  	// New returns an MinIO Admin client object.
    39  	madmClnt, err := madmin.New("your-minio.example.com:9000", "YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY", true)
    40  	if err != nil {
    41  		log.Fatalln(err)
    42  	}
    43  
    44  	opts := madmin.HealOpts{
    45  		Recursive: true,                  // recursively heal all objects at 'prefix'
    46  		Remove:    true,                  // remove content that has lost quorum and not recoverable
    47  		Recreate:  true,                  // rewrite all old non-inlined xl.meta to new xl.meta
    48  		ScanMode:  madmin.HealNormalScan, // by default do not do 'deep' scanning
    49  	}
    50  
    51  	start, _, err := madmClnt.Heal(context.Background(), "healing-rewrite-bucket", "", opts, "", false, false)
    52  	if err != nil {
    53  		log.Fatalln(err)
    54  	}
    55  	fmt.Println("Healstart sequence ===")
    56  	enc := json.NewEncoder(os.Stdout)
    57  	if err = enc.Encode(&start); err != nil {
    58  		log.Fatalln(err)
    59  	}
    60  
    61  	fmt.Println()
    62  	for {
    63  		_, status, err := madmClnt.Heal(context.Background(), "healing-rewrite-bucket", "", opts, start.ClientToken, false, false)
    64  		if status.Summary == "finished" {
    65  			fmt.Println("Healstatus on items ===")
    66  			for _, item := range status.Items {
    67  				if err = enc.Encode(&item); err != nil {
    68  					log.Fatalln(err)
    69  				}
    70  			}
    71  			break
    72  		}
    73  		if status.Summary == "stopped" {
    74  			fmt.Println("Healstatus on items ===")
    75  			fmt.Println("Heal failed with", status.FailureDetail)
    76  			break
    77  		}
    78  
    79  		for _, item := range status.Items {
    80  			if err = enc.Encode(&item); err != nil {
    81  				log.Fatalln(err)
    82  			}
    83  		}
    84  
    85  		time.Sleep(time.Second)
    86  	}
    87  }