Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
2de61710
D
Docs
项目概览
OpenHarmony
/
Docs
大约 1 年 前同步成功
通知
159
Star
292
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
2de61710
编写于
5月 22, 2023
作者:
H
huaqingsimeng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
backup备份恢复开发者文档相关错误描述调整
Signed-off-by:
N
huaqingsimeng
<
qiukaiqing@huawei.com
>
上级
3fea5e52
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
203 addition
and
179 deletion
+203
-179
zh-cn/application-dev/file-management/app-file-backup.md
zh-cn/application-dev/file-management/app-file-backup.md
+202
-178
zh-cn/application-dev/reference/apis/js-apis-file-backup.md
zh-cn/application-dev/reference/apis/js-apis-file-backup.md
+1
-1
未找到文件。
zh-cn/application-dev/file-management/app-file-backup.md
浏览文件 @
2de61710
...
@@ -33,16 +33,18 @@
...
@@ -33,16 +33,18 @@
调用
`backup.getLocalCapabilities()`
获取能力文件。
调用
`backup.getLocalCapabilities()`
获取能力文件。
```
js
```
js
import
fs
from
'
@ohos.file.fs
'
;
import
fs
from
'
@ohos.file.fs
'
;
try
{
async
function
getLocalCapabilities
()
{
let
fileData
=
await
backup
.
getLocalCapabilities
();
try
{
console
.
info
(
'
getLocalCapabilities success
'
);
let
fileData
=
await
backup
.
getLocalCapabilities
();
let
fpath
=
await
globalThis
.
context
.
filesDir
+
'
/localCapabilities.json
'
;
console
.
info
(
'
getLocalCapabilities success
'
);
fs
.
copyFileSync
(
fileData
.
fd
,
fpath
);
let
fpath
=
await
globalThis
.
context
.
filesDir
+
'
/localCapabilities.json
'
;
fs
.
closeSync
(
fileData
.
fd
);
fs
.
copyFileSync
(
fileData
.
fd
,
fpath
);
}
catch
(
err
)
{
fs
.
closeSync
(
fileData
.
fd
);
console
.
error
(
'
getLocalCapabilities failed with err:
'
+
err
);
}
catch
(
err
)
{
}
console
.
error
(
'
getLocalCapabilities failed with err:
'
+
err
);
}
}
```
```
**返回的能力文件内容示例:**
**返回的能力文件内容示例:**
...
@@ -88,55 +90,64 @@
...
@@ -88,55 +90,64 @@
```
ts
```
ts
import
fs
from
'
@ohos.file.fs
'
;
import
fs
from
'
@ohos.file.fs
'
;
// 创建SessionBackup类的实例用于备份数据
// 创建SessionBackup类的实例用于备份数据
let
session
=
new
backup
.
SessionBackup
({
let
g_session
;
onFileReady
:
async
(
err
,
file
)
=>
{
function
createSessionBackup
()
{
if
(
err
)
{
let
sessionBackup
=
new
backup
.
SessionBackup
({
console
.
info
(
'
onFileReady err:
'
+
err
);
onFileReady
:
async
(
err
,
file
)
=>
{
}
if
(
err
)
{
try
{
console
.
info
(
'
onFileReady err:
'
+
err
);
let
bundlePath
=
await
globalThis
.
context
.
filesDir
+
'
/
'
+
file
.
bundleName
;
if
(
!
fs
.
accessSync
(
bundlePath
))
{
fs
.
mkdirSync
(
bundlePath
);
}
}
fs
.
copyFileSync
(
file
.
fd
,
bundlePath
+
`/
${
file
.
uri
}
`
);
try
{
fs
.
closeSync
(
file
.
fd
);
let
bundlePath
=
await
globalThis
.
context
.
filesDir
+
'
/
'
+
file
.
bundleName
;
console
.
info
(
'
onFileReady success
'
);
if
(
!
fs
.
accessSync
(
bundlePath
))
{
}
catch
(
e
)
{
fs
.
mkdirSync
(
bundlePath
);
console
.
error
(
'
onFileReady failed with err:
'
+
e
);
}
}
fs
.
copyFileSync
(
file
.
fd
,
bundlePath
+
`/
${
file
.
uri
}
`
);
},
fs
.
closeSync
(
file
.
fd
);
onBundleBegin
:
(
err
,
bundleName
)
=>
{
console
.
info
(
'
onFileReady success
'
);
if
(
err
)
{
}
catch
(
e
)
{
console
.
info
(
'
onBundleBegin err:
'
+
err
);
console
.
error
(
'
onFileReady failed with err:
'
+
e
);
}
else
{
}
console
.
info
(
'
onBundleBegin bundleName:
'
+
bundleName
);
},
}
onBundleBegin
:
(
err
,
bundleName
)
=>
{
},
if
(
err
)
{
onBundleEnd
:
(
err
,
bundleName
)
=>
{
console
.
info
(
'
onBundleBegin err:
'
+
err
);
if
(
err
)
{
}
else
{
console
.
info
(
'
onBundleEnd err:
'
+
err
);
console
.
info
(
'
onBundleBegin bundleName:
'
+
bundleName
);
}
else
{
}
console
.
info
(
'
onBundleEnd bundleName:
'
+
bundleName
);
},
}
onBundleEnd
:
(
err
,
bundleName
)
=>
{
},
if
(
err
)
{
onAllBundlesEnd
:
(
err
)
=>
{
console
.
info
(
'
onBundleEnd err:
'
+
err
);
if
(
err
)
{
}
else
{
console
.
info
(
'
onAllBundlesEnd err:
'
+
err
);
console
.
info
(
'
onBundleEnd bundleName:
'
+
bundleName
);
}
else
{
}
console
.
info
(
'
onAllBundlesEnd
'
);
},
}
onAllBundlesEnd
:
(
err
)
=>
{
},
if
(
err
)
{
onBackupServiceDied
:
()
=>
{
console
.
info
(
'
onAllBundlesEnd err:
'
+
err
);
console
.
info
(
'
onBackupServiceDied
'
);
}
else
{
},
console
.
info
(
'
onAllBundlesEnd
'
);
});
}
// 此处可根据backup.getLocalCapabilities()提供的能力文件,选择需要备份的应用
},
// 也可直接根据应用包名称进行备份
onBackupServiceDied
:
()
=>
{
const
backupApps
=
[
console
.
info
(
'
onBackupServiceDied
'
);
"
com.example.hiworld
"
,
},
]
});
await
session
.
appendBundles
(
backupApps
);
return
sessionBackup
;
console
.
info
(
'
appendBundles success
'
);
}
async
function
sessionBackup
()
{
g_session
=
createSessionBackup
();
// 此处可根据backup.getLocalCapabilities()提供的能力文件,选择需要备份的应用
// 也可直接根据应用包名称进行备份
const
backupApps
=
[
"
com.example.hiworld
"
,
]
await
g_session
.
appendBundles
(
backupApps
);
console
.
info
(
'
appendBundles success
'
);
}
```
```
## 应用恢复数据
## 应用恢复数据
...
@@ -152,72 +163,79 @@
...
@@ -152,72 +163,79 @@
```
ts
```
ts
import
fs
from
'
@ohos.file.fs
'
;
import
fs
from
'
@ohos.file.fs
'
;
// 创建SessionRestore类的实例用于恢复数据
// 创建SessionRestore类的实例用于恢复数据
let
session
=
new
backup
.
SessionRestore
({
let
g_session
;
onFileReady
:
async
(
err
,
file
)
=>
{
async
function
publishFile
(
file
)
if
(
err
)
{
{
console
.
info
(
'
onFileReady err:
'
+
err
);
await
g_session
.
publishFile
({
}
bundleName
:
file
.
bundleName
,
try
{
uri
:
file
.
uri
let
bundlePath
=
await
globalThis
.
context
.
filesDir
+
'
/
'
+
file
.
bundleName
+
'
/
'
+
file
.
uri
;
});
}
function
createSessionRestore
()
{
let
sessionRestore
=
new
backup
.
SessionRestore
({
onFileReady
:
(
err
,
file
)
=>
{
if
(
err
)
{
console
.
info
(
'
onFileReady err:
'
+
err
);
}
// 此处开发者请根据实际场景待恢复文件存放位置进行调整 bundlePath
let
bundlePath
;
if
(
!
fs
.
accessSync
(
bundlePath
))
{
if
(
!
fs
.
accessSync
(
bundlePath
))
{
console
.
info
(
'
onFileReady bundlePath err :
'
+
bundlePath
);
console
.
info
(
'
onFileReady bundlePath err :
'
+
bundlePath
);
}
}
fs
.
copyFileSync
(
bundlePath
,
file
.
fd
);
fs
.
copyFileSync
(
bundlePath
,
file
.
fd
);
fs
.
closeSync
(
file
.
fd
);
fs
.
closeSync
(
file
.
fd
);
// 恢复数据传输完成后,会通知服务端文件准备就绪
// 恢复数据传输完成后,会通知服务端文件准备就绪
await
session
.
publishFile
({
publishFile
(
file
);
bundleName
:
file
.
bundleName
,
uri
:
file
.
uri
,
});
console
.
info
(
'
onFileReady success
'
);
console
.
info
(
'
onFileReady success
'
);
}
catch
(
e
)
{
},
console
.
error
(
'
onFileReady failed with err:
'
+
e
);
onBundleBegin
:
(
err
,
bundleName
)
=>
{
}
if
(
err
)
{
},
console
.
error
(
'
onBundleBegin failed with err:
'
+
err
);
onBundleBegin
:
(
err
,
bundleName
)
=>
{
}
if
(
err
)
{
console
.
info
(
'
onBundleBegin success
'
);
console
.
info
(
'
onBundleBegin err:
'
+
err
);
},
}
else
{
onBundleEnd
:
(
err
,
bundleName
)
=>
{
console
.
info
(
'
onBundleBegin bundleName:
'
+
bundleName
);
if
(
err
)
{
}
console
.
error
(
'
onBundleEnd failed with err:
'
+
err
);
},
}
onBundleEnd
:
(
err
,
bundleName
)
=>
{
console
.
info
(
'
onBundleEnd success
'
);
if
(
err
)
{
},
console
.
info
(
'
onBundleEnd err:
'
+
err
);
onAllBundlesEnd
:
(
err
)
=>
{
}
else
{
if
(
err
)
{
console
.
info
(
'
onBundleEnd bundleName:
'
+
bundleName
);
console
.
error
(
'
onAllBundlesEnd failed with err:
'
+
err
);
}
}
},
console
.
info
(
'
onAllBundlesEnd success
'
);
onAllBundlesEnd
:
(
err
)
=>
{
},
if
(
err
)
{
onBackupServiceDied
:
()
=>
{
console
.
info
(
'
onAllBundlesEnd err:
'
+
err
);
console
.
info
(
'
service died
'
);
}
else
{
console
.
info
(
'
onAllBundlesEnd
'
);
}
}
},
onBackupServiceDied
:
()
=>
{
console
.
info
(
'
onBackupServiceDied
'
);
},
});
const
backupApps
=
[
"
com.example.hiworld
"
,
]
// 能力文件的获取方式可以根据开发者实际场景进行调整。此处仅为请求示例
// 开发者也可以根据能力文件内容的结构示例,自行构造能力文件内容
let
fileData
=
await
backup
.
getLocalCapabilities
();
await
session
.
appendBundles
(
fileData
.
fd
,
backupApps
);
console
.
info
(
'
appendBundles success
'
);
// 添加需要恢复的应用成功后,请根据需要恢复的应用名称,调用getFileHandle接口获取待恢复应用数文件的文件句柄
// 应用待恢复数据文件数请依据实际备份文件个数为准,此处仅为请求示例
await
session
.
getFileHandle
({
bundleName
:
restoreApps
[
0
],
uri
:
"
manage.json
"
});
await
session
.
getFileHandle
({
bundleName
:
restoreApps
[
0
],
uri
:
"
1.tar
"
});
});
console
.
info
(
'
getFileHandle success
'
);
return
sessionRestore
;
}
async
function
restore
()
{
g_session
=
createSessionRestore
();
const
backupApps
=
[
"
com.example.hiworld
"
,
]
// 能力文件的获取方式可以根据开发者实际场景进行调整。此处仅为请求示例
// 开发者也可以根据能力文件内容的结构示例,自行构造能力文件内容
let
fileData
=
await
backup
.
getLocalCapabilities
();
await
g_session
.
appendBundles
(
fileData
.
fd
,
backupApps
);
console
.
info
(
'
appendBundles success
'
);
// 添加需要恢复的应用成功后,请根据需要恢复的应用名称,调用getFileHandle接口获取待恢复应用数文件的文件句柄
// 应用待恢复数据文件数请依据实际备份文件个数为准,此处仅为请求示例
await
g_session
.
getFileHandle
({
bundleName
:
restoreApps
[
0
],
uri
:
"
manage.json
"
});
await
g_session
.
getFileHandle
({
bundleName
:
restoreApps
[
0
],
uri
:
"
1.tar
"
});
console
.
info
(
'
getFileHandle success
'
);
}
```
```
## 应用恢复数据时安装应用
## 应用恢复数据时安装应用
...
@@ -233,18 +251,26 @@
...
@@ -233,18 +251,26 @@
```
ts
```
ts
import
fs
from
'
@ohos.file.fs
'
;
import
fs
from
'
@ohos.file.fs
'
;
// 创建SessionRestore类的实例用于恢复数据
// 创建SessionRestore类的实例用于恢复数据
let
session
=
new
backup
.
SessionRestore
({
let
g_session
;
onFileReady
:
async
(
err
,
file
)
=>
{
async
function
publishFile
(
file
)
{
await
g_session
.
publishFile
({
bundleName
:
file
.
bundleName
,
uri
:
file
.
uri
});
}
function
createSessionRestore
()
{
let
sessionRestore
=
new
backup
.
SessionRestore
({
onFileReady
:
(
err
,
file
)
=>
{
if
(
err
)
{
if
(
err
)
{
console
.
info
(
'
onFileReady err:
'
+
err
);
console
.
info
(
'
onFileReady err:
'
+
err
);
}
}
try
{
let
bundlePath
;
let
bundlePath
;
if
(
file
.
uri
==
"
/data/storage/el2/restore/bundle.hap
"
)
if
(
file
.
uri
==
"
/data/storage/el2/restore/bundle.hap
"
)
{
{
// 此处开发者请根据实际场景安装包的存放位置进行调整
// 此处开发者请根据实际场景安装包的存放位置进行调整
}
else
{
}
else
{
bundlePath
=
await
globalThis
.
context
.
filesDir
+
'
/
'
+
file
.
bundleName
+
'
/
'
+
file
.
uri
;
// 此处开发者请根据实际场景待恢复文件存放位置进行调整 bundlePath
}
}
if
(
!
fs
.
accessSync
(
bundlePath
))
{
if
(
!
fs
.
accessSync
(
bundlePath
))
{
console
.
info
(
'
onFileReady bundlePath err :
'
+
bundlePath
);
console
.
info
(
'
onFileReady bundlePath err :
'
+
bundlePath
);
...
@@ -252,68 +278,66 @@
...
@@ -252,68 +278,66 @@
fs
.
copyFileSync
(
bundlePath
,
file
.
fd
);
fs
.
copyFileSync
(
bundlePath
,
file
.
fd
);
fs
.
closeSync
(
file
.
fd
);
fs
.
closeSync
(
file
.
fd
);
// 恢复数据传输完成后,会通知服务端文件准备就绪
// 恢复数据传输完成后,会通知服务端文件准备就绪
await
session
.
publishFile
({
publishFile
(
file
);
bundleName
:
file
.
bundleName
,
uri
:
file
.
uri
,
});
console
.
info
(
'
onFileReady success
'
);
console
.
info
(
'
onFileReady success
'
);
}
catch
(
e
)
{
},
console
.
error
(
'
onFileReady failed with err:
'
+
e
);
onBundleBegin
:
(
err
,
bundleName
)
=>
{
}
if
(
err
)
{
},
console
.
error
(
'
onBundleBegin failed with err:
'
+
err
);
onBundleBegin
:
(
err
,
bundleName
)
=>
{
}
if
(
err
)
{
console
.
info
(
'
onBundleBegin success
'
);
console
.
info
(
'
onBundleBegin err:
'
+
err
);
},
}
else
{
onBundleEnd
:
(
err
,
bundleName
)
=>
{
console
.
info
(
'
onBundleBegin bundleName:
'
+
bundleName
);
if
(
err
)
{
}
console
.
error
(
'
onBundleEnd failed with err:
'
+
err
);
},
}
onBundleEnd
:
(
err
,
bundleName
)
=>
{
console
.
info
(
'
onBundleEnd success
'
);
if
(
err
)
{
},
console
.
info
(
'
onBundleEnd err:
'
+
err
);
onAllBundlesEnd
:
(
err
)
=>
{
}
else
{
if
(
err
)
{
console
.
info
(
'
onBundleEnd bundleName:
'
+
bundleName
);
console
.
error
(
'
onAllBundlesEnd failed with err:
'
+
err
);
}
}
},
console
.
info
(
'
onAllBundlesEnd success
'
);
onAllBundlesEnd
:
(
err
)
=>
{
},
if
(
err
)
{
onBackupServiceDied
:
()
=>
{
console
.
info
(
'
onAllBundlesEnd err:
'
+
err
);
console
.
info
(
'
service died
'
);
}
else
{
console
.
info
(
'
onAllBundlesEnd
'
);
}
}
},
onBackupServiceDied
:
()
=>
{
console
.
info
(
'
onBackupServiceDied
'
);
},
});
const
backupApps
=
[
"
com.example.hiworld
"
,
]
let
fpath
=
await
globalThis
.
context
.
filesDir
+
'
/localCapabilities.json
'
;
let
file
=
fs
.
openSync
(
fpath
,
fileIO
.
OpenMode
.
CREATE
|
fileIO
.
OpenMode
.
READ_WRITE
);
let
content
=
"
{
\"
bundleInfos
\"
:[{
\"
allToBackup
\"
: false,
\"
extensionName
\"
:
\"\"
,
"
+
"
\"
name
\"
:
\"
cn.openharmony.inputmethodchoosedialog
\"
,
\"
needToInstall
\"
: true,
\"
spaceOccupied
\"
: 0,
"
+
"
\"
versionCode
\"
: 1000000,
\"
versionName
\"
:
\"
1.0.0
\"
}],
\"
deviceType
\"
:
\"
default
\"
,
\"
systemFullName
\"
:
\"
OpenHarmony-4.0.6.2(Canary1)
\"
}
"
;
fs
.
writeSync
(
file
.
fd
,
content
);
fs
.
fsyncSync
(
file
.
fd
);
await
session
.
appendBundles
(
file
.
fd
,
backupApps
);
console
.
info
(
'
appendBundles success
'
);
// 开发者需要请求安装应用的文件句柄
await
session
.
getFileHandle
({
bundleName
:
restoreApps
[
0
],
uri
:
"
/data/storage/el2/restore/bundle.hap
"
});
await
session
.
getFileHandle
({
bundleName
:
restoreApps
[
0
],
uri
:
"
manage.json
"
});
await
session
.
getFileHandle
({
bundleName
:
restoreApps
[
0
],
uri
:
"
1.tar
"
});
});
console
.
info
(
'
getFileHandle success
'
);
return
sessionRestore
;
}
async
function
restore
()
{
g_session
=
createSessionRestore
();
const
backupApps
=
[
"
com.example.hiworld
"
,
]
let
fpath
=
await
globalThis
.
context
.
filesDir
+
'
/localCapabilities.json
'
;
let
file
=
fs
.
openSync
(
fpath
,
fileIO
.
OpenMode
.
CREATE
|
fileIO
.
OpenMode
.
READ_WRITE
);
let
content
=
"
{
\"
bundleInfos
\"
:[{
\"
allToBackup
\"
: false,
\"
extensionName
\"
:
\"\"
,
"
+
"
\"
name
\"
:
\"
cn.openharmony.inputmethodchoosedialog
\"
,
\"
needToInstall
\"
: true,
\"
spaceOccupied
\"
: 0,
"
+
"
\"
versionCode
\"
: 1000000,
\"
versionName
\"
:
\"
1.0.0
\"
}],
\"
deviceType
\"
:
\"
default
\"
,
\"
systemFullName
\"
:
\"
OpenHarmony-4.0.6.2(Canary1)
\"
}
"
;
fs
.
writeSync
(
file
.
fd
,
content
);
fs
.
fsyncSync
(
file
.
fd
);
await
g_session
.
appendBundles
(
file
.
fd
,
backupApps
);
console
.
info
(
'
appendBundles success
'
);
// 开发者需要请求安装应用的文件句柄
await
g_session
.
getFileHandle
({
bundleName
:
restoreApps
[
0
],
uri
:
"
/data/storage/el2/restore/bundle.hap
"
});
await
g_session
.
getFileHandle
({
bundleName
:
restoreApps
[
0
],
uri
:
"
manage.json
"
});
await
g_session
.
getFileHandle
({
bundleName
:
restoreApps
[
0
],
uri
:
"
1.tar
"
});
console
.
info
(
'
getFileHandle success
'
);
}
```
```
**恢复数据时安装应用所需的能力文件内容示例:**
**恢复数据时安装应用所需的能力文件内容示例:**
...
...
zh-cn/application-dev/reference/apis/js-apis-file-backup.md
浏览文件 @
2de61710
...
@@ -63,7 +63,7 @@ onFileReady : AsyncCallback<File>
...
@@ -63,7 +63,7 @@ onFileReady : AsyncCallback<File>
> **说明:**
> **说明:**
>
>
> AsyncCallback回调中返回的File 所属file.backup.[File](#file)类型,返回的文件归备份服务所有,一旦文件关闭,备份服务将选择合适的
实际
去清理,但客户端必须关闭文件句柄。
> AsyncCallback回调中返回的File 所属file.backup.[File](#file)类型,返回的文件归备份服务所有,一旦文件关闭,备份服务将选择合适的
时机
去清理,但客户端必须关闭文件句柄。
**系统能力**
:SystemCapability.FileManagement.StorageService.Backup
**系统能力**
:SystemCapability.FileManagement.StorageService.Backup
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录