
# 应用备份
不同Region UCS华为云集群应用的迁移包含两个步骤：应用备份和应用迁移，即备份源Region集群中应用，然后通过数据恢复的方式迁移至目标Region集群。
k8clone是一个简便的Kubernetes元数据克隆工具，它可以将Kubernetes元数据（对象）保存为本地压缩包，然后将这些元数据恢复到目标集群中。
![](https://support.huaweicloud.com/usermanual-ucs/public_sys-resources/notice_3.0-zh-cn.png)
建议在用户业务量小时执行备份操作。
#### 前提条件
确认云原生应用依赖的服务（镜像、存储、数据库等非集群内的数据）都已经迁移完成。
#### k8clone数据备份原理
数据备份的流程参考如下：
图1数据备份流程   
![](https://support.huaweicloud.com/usermanual-ucs/zh-cn_image_0000001552293325.png "点击放大")
 #### k8clone备份使用方法
![](https://support.huaweicloud.com/usermanual-ucs/public_sys-resources/note_3.0-zh-cn.png)
k8clone工具支持在Linux（x86、arm）和Windows环境中运行，使用方法相似。本文将以Linux（x86）环境为例进行介绍。
若使用Linux（arm）或Windows环境，请将下述命令中的**k8clone-linux-amd64** 分别替换为**k8clone-linux-arm64** 或**k8clone-windows-amd64.exe**。
在使用k8s restore时， 默认跳过回收策略为Delete的PersistentVolume对象，用户可进行手动处理。
在k8clone工具所在目录下执行**./k8clone-linux-amd64 backup -h**，可以查看k8clone工具备份的使用方法。
- -k, --kubeconfig：指定kubectl的KubeConfig位置，默认是$HOME/.kube/config。kubeConfig文件：用于配置对Kubernetes集群的访问，KubeConfig文件中包含访问注册Kubernetes集群所需要的认证凭据以及Endpoint（访问地址），详细介绍可参见[Kubernetes文档](https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/)。
- -s, --api-server：Kubernetes API Server URL，默认是""。
- -q, --context：Kubernetes Configuration Context，默认是""。
- -n, --namespace：备份指定命名空间的云原生应用，多个命名空间用逗号分隔（如：ns1,ns2,ns3），默认是""，表示备份整个集群。
- -e, --exclude-namespaces：排除指定命名空间对象的备份，不能和--namespace一起使用。
- -x, --exclude-kind：排除指定资源类型的备份。
- -i, --include-kind : 指定资源类型的备份。
- -y, --exclude-object：排除指定资源对象的备份。
- -z, --include-object：指定资源对象的备份。
- -w, --exclude-having-owner-ref：排除拥有ownerReferences资源对象的备份，默认是false。布尔类型传参过程中需要带上等号，如："-w=true" ，"-w true"不会生效,它将使用默认值。
- -d, --local-dir：备份数据放置的路径，默认是当前目录下k8clone-dump文件夹。
```
$ ./k8clone-linux-amd64 backup -h
Backup Workload Data as yaml files
Usage:
    k8clone backup [flags]
Flags:
   -s, --api-server string            Kubernetes api-server url
   -q, --context string               Kubernetes configuration context
   -w, --exclude-having-owner-ref     Exclude all objects having an Owner Reference. The default value is false. The equal sign (=) must be added when a Boolean parameter is transferred, for example, -w=true. -w  true does not take effect, and the default value will be used.
   -x, --exclude-kind strings         Resource kind to exclude. Eg. 'deployment'
   -i, --include-kind strings         Resource kind to include. Eg. 'deployment'
   -e, --exclude-namespaces strings   Namespaces to exclude. Eg. 'temp.*' as regexes. This collects all namespaces and then filters them. Don't use it with the namespace flag.
   -y, --exclude-object strings       Object to exclude. The form is '<kind>:<namespace>/<name>',namespace can be empty when object is not namespaced. Eg. 'configmap:kube-system/kube-dns'
   -z, --include-object strings       Object to include. The form is '<kind>:<namespace>/<name>',namespace can be empty when object is not namespaced. Eg. 'configmap:kube-system/kube-dns' 
   -h, --help                         help for backup
   -k, --kubeconfig string            The kubeconfig of k8s cluster's. Default is the $HOME/.kube/config. 
   -d, --local-dir string             Where to dump yaml files (default "./k8clone-dump")
   -n, --namespace string             Only dump objects from this namespace
```
示例如下：
- 整个集群对象的备份，默认路径为当前目录下"k8clone-dump"文件夹 **./k8clone-linux-amd64 backup**
  
- 整个集群对象的备份，并指定备份数据路径 **./k8clone-linux-amd64 backup -d ./xxxx**
  
- 指定命名空间对象的备份 **./k8clone-linux-amd64 backup -n default**
  
- 排除命名空间对象的备份 **./k8clone-linux-amd64 backup -e kube-system,kube-public,kube-node-lease**
  
- 排除指定资源类型的备份 **./k8clone-linux-amd64 backup -x endpoints,endpointslice**
  
- 指定资源类型的备份 **./k8clone-linux-amd64 backup -i rolebinding**
  
- 排除指定资源对象的备份 **./k8clone-linux-amd64 backup -y configmap:kube-system/kube-dns**
  
- 指定资源对象的备份 **./k8clone-linux-amd64 backup -z configmap:kube-system/kube-dns**
  
- 排除拥有ownerReferences资源对象的备份 **./k8clone-linux-amd64 backup -w=true**
  
 
#### 应用备份操作步骤
1. 通过kubectl连接源集群。
2. 进入k8clone工具所在目录，执行备份命令，即可备份数据到本地目录，并打包成压缩包。 
   [k8clone备份使用方法]的示例中提供了几种常见的备份方式，您可以按需选择，也可以自定义备份方式。
   
   
 
