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
...
...
@@ -34,6 +34,7 @@
```
js
import
fs
from
'
@ohos.file.fs
'
;
async
function
getLocalCapabilities
()
{
try
{
let
fileData
=
await
backup
.
getLocalCapabilities
();
console
.
info
(
'
getLocalCapabilities success
'
);
...
...
@@ -43,6 +44,7 @@
}
catch
(
err
)
{
console
.
error
(
'
getLocalCapabilities failed with err:
'
+
err
);
}
}
```
**返回的能力文件内容示例:**
...
...
@@ -88,7 +90,9 @@
```
ts
import
fs
from
'
@ohos.file.fs
'
;
// 创建SessionBackup类的实例用于备份数据
let
session
=
new
backup
.
SessionBackup
({
let
g_session
;
function
createSessionBackup
()
{
let
sessionBackup
=
new
backup
.
SessionBackup
({
onFileReady
:
async
(
err
,
file
)
=>
{
if
(
err
)
{
console
.
info
(
'
onFileReady err:
'
+
err
);
...
...
@@ -130,13 +134,20 @@
console
.
info
(
'
onBackupServiceDied
'
);
},
});
return
sessionBackup
;
}
async
function
sessionBackup
()
{
g_session
=
createSessionBackup
();
// 此处可根据backup.getLocalCapabilities()提供的能力文件,选择需要备份的应用
// 也可直接根据应用包名称进行备份
const
backupApps
=
[
"
com.example.hiworld
"
,
]
await
session
.
appendBundles
(
backupApps
);
await
g_
session
.
appendBundles
(
backupApps
);
console
.
info
(
'
appendBundles success
'
);
}
```
## 应用恢复数据
...
...
@@ -152,72 +163,79 @@
```
ts
import
fs
from
'
@ohos.file.fs
'
;
// 创建SessionRestore类的实例用于恢复数据
let
session
=
new
backup
.
SessionRestore
({
onFileReady
:
async
(
err
,
file
)
=>
{
let
g_session
;
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
)
{
console
.
info
(
'
onFileReady err:
'
+
err
);
}
try
{
let
bundlePath
=
await
globalThis
.
context
.
filesDir
+
'
/
'
+
file
.
bundleName
+
'
/
'
+
file
.
uri
;
// 此处开发者请根据实际场景待恢复文件存放位置进行调整 bundlePath
let
bundlePath
;
if
(
!
fs
.
accessSync
(
bundlePath
))
{
console
.
info
(
'
onFileReady bundlePath err :
'
+
bundlePath
);
}
fs
.
copyFileSync
(
bundlePath
,
file
.
fd
);
fs
.
closeSync
(
file
.
fd
);
// 恢复数据传输完成后,会通知服务端文件准备就绪
await
session
.
publishFile
({
bundleName
:
file
.
bundleName
,
uri
:
file
.
uri
,
});
publishFile
(
file
);
console
.
info
(
'
onFileReady success
'
);
}
catch
(
e
)
{
console
.
error
(
'
onFileReady failed with err:
'
+
e
);
}
},
onBundleBegin
:
(
err
,
bundleName
)
=>
{
if
(
err
)
{
console
.
info
(
'
onBundleBegin err:
'
+
err
);
}
else
{
console
.
info
(
'
onBundleBegin bundleName:
'
+
bundleName
);
console
.
error
(
'
onBundleBegin failed with err:
'
+
err
);
}
console
.
info
(
'
onBundleBegin success
'
);
},
onBundleEnd
:
(
err
,
bundleName
)
=>
{
if
(
err
)
{
console
.
info
(
'
onBundleEnd err:
'
+
err
);
}
else
{
console
.
info
(
'
onBundleEnd bundleName:
'
+
bundleName
);
console
.
error
(
'
onBundleEnd failed with err:
'
+
err
);
}
console
.
info
(
'
onBundleEnd success
'
);
},
onAllBundlesEnd
:
(
err
)
=>
{
if
(
err
)
{
console
.
info
(
'
onAllBundlesEnd err:
'
+
err
);
}
else
{
console
.
info
(
'
onAllBundlesEnd
'
);
console
.
error
(
'
onAllBundlesEnd failed with err:
'
+
err
);
}
console
.
info
(
'
onAllBundlesEnd success
'
);
},
onBackupServiceDied
:
()
=>
{
console
.
info
(
'
onBackupServiceD
ied
'
);
},
console
.
info
(
'
service d
ied
'
);
}
});
return
sessionRestore
;
}
async
function
restore
()
{
g_session
=
createSessionRestore
();
const
backupApps
=
[
"
com.example.hiworld
"
,
]
// 能力文件的获取方式可以根据开发者实际场景进行调整。此处仅为请求示例
// 开发者也可以根据能力文件内容的结构示例,自行构造能力文件内容
let
fileData
=
await
backup
.
getLocalCapabilities
();
await
session
.
appendBundles
(
fileData
.
fd
,
backupApps
);
await
g_
session
.
appendBundles
(
fileData
.
fd
,
backupApps
);
console
.
info
(
'
appendBundles success
'
);
// 添加需要恢复的应用成功后,请根据需要恢复的应用名称,调用getFileHandle接口获取待恢复应用数文件的文件句柄
// 应用待恢复数据文件数请依据实际备份文件个数为准,此处仅为请求示例
await
session
.
getFileHandle
({
await
g_
session
.
getFileHandle
({
bundleName
:
restoreApps
[
0
],
uri
:
"
manage.json
"
});
await
session
.
getFileHandle
({
await
g_
session
.
getFileHandle
({
bundleName
:
restoreApps
[
0
],
uri
:
"
1.tar
"
});
console
.
info
(
'
getFileHandle success
'
);
}
```
## 应用恢复数据时安装应用
...
...
@@ -233,18 +251,26 @@
```
ts
import
fs
from
'
@ohos.file.fs
'
;
// 创建SessionRestore类的实例用于恢复数据
let
session
=
new
backup
.
SessionRestore
({
onFileReady
:
async
(
err
,
file
)
=>
{
let
g_session
;
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
)
{
console
.
info
(
'
onFileReady err:
'
+
err
);
}
try
{
let
bundlePath
;
if
(
file
.
uri
==
"
/data/storage/el2/restore/bundle.hap
"
)
{
// 此处开发者请根据实际场景安装包的存放位置进行调整
}
else
{
bundlePath
=
await
globalThis
.
context
.
filesDir
+
'
/
'
+
file
.
bundleName
+
'
/
'
+
file
.
uri
;
// 此处开发者请根据实际场景待恢复文件存放位置进行调整 bundlePath
}
if
(
!
fs
.
accessSync
(
bundlePath
))
{
console
.
info
(
'
onFileReady bundlePath err :
'
+
bundlePath
);
...
...
@@ -252,40 +278,37 @@
fs
.
copyFileSync
(
bundlePath
,
file
.
fd
);
fs
.
closeSync
(
file
.
fd
);
// 恢复数据传输完成后,会通知服务端文件准备就绪
await
session
.
publishFile
({
bundleName
:
file
.
bundleName
,
uri
:
file
.
uri
,
});
publishFile
(
file
);
console
.
info
(
'
onFileReady success
'
);
}
catch
(
e
)
{
console
.
error
(
'
onFileReady failed with err:
'
+
e
);
}
},
onBundleBegin
:
(
err
,
bundleName
)
=>
{
if
(
err
)
{
console
.
info
(
'
onBundleBegin err:
'
+
err
);
}
else
{
console
.
info
(
'
onBundleBegin bundleName:
'
+
bundleName
);
console
.
error
(
'
onBundleBegin failed with err:
'
+
err
);
}
console
.
info
(
'
onBundleBegin success
'
);
},
onBundleEnd
:
(
err
,
bundleName
)
=>
{
if
(
err
)
{
console
.
info
(
'
onBundleEnd err:
'
+
err
);
}
else
{
console
.
info
(
'
onBundleEnd bundleName:
'
+
bundleName
);
console
.
error
(
'
onBundleEnd failed with err:
'
+
err
);
}
console
.
info
(
'
onBundleEnd success
'
);
},
onAllBundlesEnd
:
(
err
)
=>
{
if
(
err
)
{
console
.
info
(
'
onAllBundlesEnd err:
'
+
err
);
}
else
{
console
.
info
(
'
onAllBundlesEnd
'
);
console
.
error
(
'
onAllBundlesEnd failed with err:
'
+
err
);
}
console
.
info
(
'
onAllBundlesEnd success
'
);
},
onBackupServiceDied
:
()
=>
{
console
.
info
(
'
onBackupServiceD
ied
'
);
},
console
.
info
(
'
service d
ied
'
);
}
});
return
sessionRestore
;
}
async
function
restore
()
{
g_session
=
createSessionRestore
();
const
backupApps
=
[
"
com.example.hiworld
"
,
]
...
...
@@ -296,24 +319,25 @@
"
\"
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
);
await
g_
session
.
appendBundles
(
file
.
fd
,
backupApps
);
console
.
info
(
'
appendBundles success
'
);
// 开发者需要请求安装应用的文件句柄
await
session
.
getFileHandle
({
await
g_
session
.
getFileHandle
({
bundleName
:
restoreApps
[
0
],
uri
:
"
/data/storage/el2/restore/bundle.hap
"
});
await
session
.
getFileHandle
({
await
g_
session
.
getFileHandle
({
bundleName
:
restoreApps
[
0
],
uri
:
"
manage.json
"
});
await
session
.
getFileHandle
({
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>
> **说明:**
>
> AsyncCallback回调中返回的File 所属file.backup.[File](#file)类型,返回的文件归备份服务所有,一旦文件关闭,备份服务将选择合适的
实际
去清理,但客户端必须关闭文件句柄。
> AsyncCallback回调中返回的File 所属file.backup.[File](#file)类型,返回的文件归备份服务所有,一旦文件关闭,备份服务将选择合适的
时机
去清理,但客户端必须关闭文件句柄。
**系统能力**
:SystemCapability.FileManagement.StorageService.Backup
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录