『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
楼主: muxiachuixue

[技术讨论] HA定时自动备份到百度网盘、阿里网盘(手把手教程)

  [复制链接]

1

主题

64

帖子

644

积分

高级会员

Rank: 4

积分
644
金钱
580
HASS币
0
发表于 2022-1-9 13:00:20 | 显示全部楼层
muxiachuixue 发表于 2022-1-3 13:11
不能用ha的自动化,ha中docker的环境不一样,很多命令没有的。如果要用ha的自动化,要改脚本里面的代码了 ...

我的不是docker环境安装的,使用华为手机Linux deploy安装Debian,搭建的homeassistant core ,不知道能否用Debian环境的自动化。我到现在都没有实现开机自启动。都是通过screen实现的。
感谢!
回复

使用道具 举报

15

主题

636

帖子

2205

积分

金牌会员

Rank: 6Rank: 6

积分
2205
金钱
1569
HASS币
0
发表于 2022-1-9 14:29:53 | 显示全部楼层
本帖最后由 telanx 于 2022-1-9 14:33 编辑

写的太详细了,特别适合小白,大赞李吹雪大神!

我是N1 Docker装的HA Core,node red,esphome,maridadb,Mqtt, 甚至还装了个nginx用来做ipv6的反向代理,所有的docker的映射文件夹都放在了外挂的hdd盘的一个svr_data目录里,请问大神,我直接设置脚本里ha的目录为这个硬盘的svr_data的路径,是否可以实现所有的需要备份的都一并打包备份呢?

嘿嘿,感谢大佬。
今目录看了眼,突然发现mariadb文件夹太大了。。。。汗
回复

使用道具 举报

5

主题

357

帖子

2057

积分

金牌会员

Rank: 6Rank: 6

积分
2057
金钱
1685
HASS币
60
 楼主| 发表于 2022-1-9 15:12:58 | 显示全部楼层
telanx 发表于 2022-1-9 14:29
写的太详细了,特别适合小白,大赞李吹雪大神!

我是N1 Docker装的HA Core,node red,esphome,maridadb ...

你可以试着运行一下,看看有没有报错
回复

使用道具 举报

0

主题

40

帖子

123

积分

注册会员

Rank: 2

积分
123
金钱
82
HASS币
0
发表于 2022-1-9 16:48:21 | 显示全部楼层
大佬,真给力
回复

使用道具 举报

15

主题

636

帖子

2205

积分

金牌会员

Rank: 6Rank: 6

积分
2205
金钱
1569
HASS币
0
发表于 2022-1-9 17:58:04 | 显示全部楼层
本帖最后由 telanx 于 2022-1-9 18:05 编辑
muxiachuixue 发表于 2022-1-9 15:12
你可以试着运行一下,看看有没有报错

我看了下您的脚本,如果HA Core的目录设置错误的话,后续文件起名的版本号什么的估计都挂。。。。所以,也就不准备尝试了。

刚试了下备份到阿里云,结果出问题了,麻烦大佬帮忙看下,N1安装的armbian bullseye, python版本3.9.2,按大佬的教程装aliyunpan-cli的时候,出现错误,后来查到需要安装python3.9-dev,从而安装成功aliyunpan-cli

运行which aliyunpan-cli,返回路径如下
/usr/local/bin/aliyunpan-cli
这个路径很正常,在默认的profile的path里,在ssh 界面,随时可以调用aliyunpan-cli程序,所以应该不需要改profile文件吧?

设置好脚本,ha的路径(真实有效的),aliyun_backup_remote_path="/SmartHome"

手动运行脚本,发现本地备份是没有任何问题的。但是在上传aliyunpan的时候,出现了错误
Traceback (most recent call last):
  File "/usr/local/bin/aliyunpan-cli", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/main.py", line 224, in main
    cli()
  File "/usr/local/lib/python3.9/dist-packages/click/core.py", line 1128, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.9/dist-packages/click/core.py", line 1053, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.9/dist-packages/click/core.py", line 1659, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python3.9/dist-packages/click/core.py", line 1395, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.9/dist-packages/click/core.py", line 754, in invoke
    return __callback(*args, **kwargs)
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/main.py", line 102, in mkdir
    commander.mkdir(path)
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/cli/cli.py", line 265, in mkdir
    file_list.extend(self.mkdir(path.parent))
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/cli/cli.py", line 265, in mkdir
    file_list.extend(self.mkdir(path.parent))
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/cli/cli.py", line 265, in mkdir
    file_list.extend(self.mkdir(path.parent))
  [Previous line repeated 977 more times]
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/cli/cli.py", line 260, in mkdir
    file_id = self._path_list.get_path_fid(path, update=False)
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/api/models.py", line 154, in get_path_fid
    if str(path) in ('', '/', '\\', '.', 'root'):
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/api/models.py", line 216, in __str__
    path = [i for i in str(PurePosixPath(Path(p).as_posix())).split('/') if i != '']
  File "/usr/lib/python3.9/pathlib.py", line 1071, in __new__
    self = cls._from_parts(args, init=False)
  File "/usr/lib/python3.9/pathlib.py", line 696, in _from_parts
    drv, root, parts = self._parse_args(args)
  File "/usr/lib/python3.9/pathlib.py", line 689, in _parse_args
    return cls._flavour.parse_parts(parts)
  File "/usr/lib/python3.9/pathlib.py", line 68, in parse_parts
    it = reversed(parts)
RecursionError: maximum recursion depth exceeded while calling a Python object
Traceback (most recent call last):
  File "/usr/local/bin/aliyunpan-cli", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/main.py", line 224, in main
    cli()
  File "/usr/local/lib/python3.9/dist-packages/click/core.py", line 1128, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.9/dist-packages/click/core.py", line 1053, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.9/dist-packages/click/core.py", line 1659, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python3.9/dist-packages/click/core.py", line 1395, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.9/dist-packages/click/core.py", line 754, in invoke
    return __callback(*args, **kwargs)
  File "/usr/local/lib/python3.9/dist-packages/click/decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/main.py", line 193, in sync
    commander.sync(local_path, remote_path, sync_time, time_out, chunk_size, retry, delete)
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/cli/cli.py", line 668, in sync
    self.upload(path, upload_path, timeout=time_out, chunk_size=chunk_size, retry=retry)
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/cli/cli.py", line 375, in upload
    upload_file_list = self.upload_dir(path, upload_path)
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/cli/cli.py", line 416, in upload_dir
    self.mkdir(upload_path)
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/cli/cli.py", line 265, in mkdir
    file_list.extend(self.mkdir(path.parent))
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/cli/cli.py", line 265, in mkdir
    file_list.extend(self.mkdir(path.parent))
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/cli/cli.py", line 265, in mkdir
    file_list.extend(self.mkdir(path.parent))
  [Previous line repeated 973 more times]
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/cli/cli.py", line 260, in mkdir
    file_id = self._path_list.get_path_fid(path, update=False)
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/api/models.py", line 154, in get_path_fid
    if str(path) in ('', '/', '\\', '.', 'root'):
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/api/models.py", line 216, in __str__
    path = [i for i in str(PurePosixPath(Path(p).as_posix())).split('/') if i != '']
  File "/usr/lib/python3.9/pathlib.py", line 1071, in __new__
    self = cls._from_parts(args, init=False)
  File "/usr/lib/python3.9/pathlib.py", line 696, in _from_parts
    drv, root, parts = self._parse_args(args)
  File "/usr/lib/python3.9/pathlib.py", line 689, in _parse_args
    return cls._flavour.parse_parts(parts)
  File "/usr/lib/python3.9/pathlib.py", line 68, in parse_parts
    it = reversed(parts)
RecursionError: maximum recursion depth exceeded while calling a Python object
Traceback (most recent call last):
  File "/usr/local/bin/aliyunpan-cli", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/main.py", line 224, in main
    cli()
  File "/usr/local/lib/python3.9/dist-packages/click/core.py", line 1128, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.9/dist-packages/click/core.py", line 1053, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.9/dist-packages/click/core.py", line 1659, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python3.9/dist-packages/click/core.py", line 1395, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.9/dist-packages/click/core.py", line 754, in invoke
    return __callback(*args, **kwargs)
  File "/usr/local/lib/python3.9/dist-packages/click/decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/main.py", line 193, in sync
    commander.sync(local_path, remote_path, sync_time, time_out, chunk_size, retry, delete)
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/cli/cli.py", line 668, in sync
    self.upload(path, upload_path, timeout=time_out, chunk_size=chunk_size, retry=retry)
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/cli/cli.py", line 375, in upload
    upload_file_list = self.upload_dir(path, upload_path)
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/cli/cli.py", line 416, in upload_dir
    self.mkdir(upload_path)
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/cli/cli.py", line 265, in mkdir
    file_list.extend(self.mkdir(path.parent))
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/cli/cli.py", line 265, in mkdir
    file_list.extend(self.mkdir(path.parent))
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/cli/cli.py", line 265, in mkdir
    file_list.extend(self.mkdir(path.parent))
  [Previous line repeated 973 more times]
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/cli/cli.py", line 260, in mkdir
    file_id = self._path_list.get_path_fid(path, update=False)
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/api/models.py", line 154, in get_path_fid
    if str(path) in ('', '/', '\\', '.', 'root'):
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/api/models.py", line 216, in __str__
    path = [i for i in str(PurePosixPath(Path(p).as_posix())).split('/') if i != '']
  File "/usr/lib/python3.9/pathlib.py", line 1071, in __new__
    self = cls._from_parts(args, init=False)
  File "/usr/lib/python3.9/pathlib.py", line 696, in _from_parts
    drv, root, parts = self._parse_args(args)
  File "/usr/lib/python3.9/pathlib.py", line 689, in _parse_args
    return cls._flavour.parse_parts(parts)
  File "/usr/lib/python3.9/pathlib.py", line 68, in parse_parts
    it = reversed(parts)
RecursionError: maximum recursion depth exceeded while calling a Python object
Traceback (most recent call last):
  File "/usr/local/bin/aliyunpan-cli", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/main.py", line 224, in main
    cli()
  File "/usr/local/lib/python3.9/dist-packages/click/core.py", line 1128, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.9/dist-packages/click/core.py", line 1053, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.9/dist-packages/click/core.py", line 1659, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python3.9/dist-packages/click/core.py", line 1395, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.9/dist-packages/click/core.py", line 754, in invoke
    return __callback(*args, **kwargs)
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/main.py", line 133, in tree
    commander.tree(path)
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/cli/cli.py", line 141, in tree
    return self._path_list.tree(path, stdout)
  File "/usr/local/lib/python3.9/dist-packages/aliyunpan/api/models.py", line 129, in tree
    raise FileNotFoundError(path)
FileNotFoundError: /SmartHome


但是我在ssh的界面,手动运行 aliyunpan-cli sync --no-delete /本地的备份路径 /SmartHome 是可以成功上传到阿里云盘的 (我用aliyunpan-cli mkdir /SmartHome 也成功建立了阿里云盘的文件夹)

请教大佬,上面提示的错误是什么原因呢?感谢
回复

使用道具 举报

5

主题

357

帖子

2057

积分

金牌会员

Rank: 6Rank: 6

积分
2057
金钱
1685
HASS币
60
 楼主| 发表于 2022-1-9 20:34:15 | 显示全部楼层
只要运行脚本文件的时候没有报错 aliyunpan-cli 程序不存在,就不需要设置profile文件。因为sh文件运行的时候环境变量和宿主机不同,所以我才在帖子里说了设置一下环境变量,防止报错。
看你贴出来的log,是FileNotFoundError: /SmartHome,你把auto_backup.log(在本地backup文件夹)文件里面的日志也贴出来看一下。
另外,你尝试一下不要在阿里云盘的云端建立备份文件夹,脚本会自动建立,也不要修改脚本里面的云端文件夹,会自动生成,看看是否还报错
回复

使用道具 举报

5

主题

357

帖子

2057

积分

金牌会员

Rank: 6Rank: 6

积分
2057
金钱
1685
HASS币
60
 楼主| 发表于 2022-1-9 20:35:42 | 显示全部楼层
telanx 发表于 2022-1-9 17:58
我看了下您的脚本,如果HA Core的目录设置错误的话,后续文件起名的版本号什么的估计都挂。。。。所以,也 ...

只要运行脚本文件的时候没有报错 aliyunpan-cli 程序不存在,就不需要设置profile文件。因为sh文件运行的时候环境变量和宿主机不同,所以我才在帖子里说了设置一下环境变量,防止报错。
看你贴出来的log,是FileNotFoundError: /SmartHome,你把auto_backup.log(在本地backup文件夹)文件里面的日志也贴出来看一下。
另外,你尝试一下不要在阿里云盘的云端建立备份文件夹,脚本会自动建立,也不要修改脚本里面的云端文件夹,会自动生成,看看是否还报错
回复

使用道具 举报

15

主题

636

帖子

2205

积分

金牌会员

Rank: 6Rank: 6

积分
2205
金钱
1569
HASS币
0
发表于 2022-1-9 23:06:08 | 显示全部楼层
muxiachuixue 发表于 2022-1-9 20:35
只要运行脚本文件的时候没有报错 aliyunpan-cli 程序不存在,就不需要设置profile文件。因为sh文件运行的 ...

重新下载了您的脚本,然后就更改了HA core的目录,其余都用的默认,阿里云盘上也都清空了,没有任何目录了。
手动运行了一下,还是跟之前的报错一样。
看了下log,绝大多数跟我之前帖子贴的是一样的。
log见附件。

auto_backup.log.zip

6.88 KB, 下载次数: 1

回复

使用道具 举报

5

主题

357

帖子

2057

积分

金牌会员

Rank: 6Rank: 6

积分
2057
金钱
1685
HASS币
60
 楼主| 发表于 2022-1-9 23:43:32 | 显示全部楼层
本帖最后由 muxiachuixue 于 2022-1-10 00:01 编辑
telanx 发表于 2022-1-9 23:06
重新下载了您的脚本,然后就更改了HA core的目录,其余都用的默认,阿里云盘上也都清空了,没有任何目录 ...

运行一下这两个命令,看看是否会报错:
aliyunpan-cli mkdir /homeassistant_backup/backup
aliyunpan-cli -f auto_backup.log sync --no-delete /mnt/sda1/Svr_Data/backup /homeassistant_backup 2>&1 | sudo tee -a /mnt/sda1/Svr_Data/backup/auto_backup.log


如果不报错的话,把上面两个命令复制到一个新的a.sh 文件里面,然后执行  sh a.sh 看看能否成功运行




回复

使用道具 举报

15

主题

636

帖子

2205

积分

金牌会员

Rank: 6Rank: 6

积分
2205
金钱
1569
HASS币
0
发表于 2022-1-10 00:13:09 | 显示全部楼层
muxiachuixue 发表于 2022-1-9 23:43
运行一下这两个命令,看看是否会报错:

如果不报错的话,把上面两个命令复制到一个新的a.sh 文件里面,然 ...

单独在ssh里运行任何aliyunpan-cli命令都不会报错,第一个命令是创建文件夹的命令,我之前运行过,没问题。

我刚才,直接简单粗暴的把阿里那块的if ---fi的判断语句全部注释掉了,然后脚本就能成功运行了。
  #if [ -n "$(aliyunpan-cli tree 2>&1 | grep -o 'root')" ] && [ ! -n "$(aliyunpan-cli tree 2>&1 | grep -o 'NotFoundError\|InvalidRefreshToken\|InvalidConfiguration')" ]; then
    #if [ ! -n "$aliyun_backup_remote_path" ]; then
      #aliyun_backup_remote_path=/homeassistant_backup
    #elif [ ! -n "$(echo $aliyun_backup_remote_path | grep -o '^/')" ]; then
      #aliyun_backup_remote_path="/$aliyun_backup_remote_path"
      #if [ -n "$(echo $aliyun_backup_remote_path | grep -oP '(?<=^/).*(?=/$)')" ]; then
        #aliyun_backup_remote_path="$(echo $aliyun_backup_remote_path | grep -oP '^/.*(?=/$)')"
      #fi
    #elif [ -n "$(echo $aliyun_backup_remote_path | grep -o '^/')" ]; then
      #if [ -n "$(echo $aliyun_backup_remote_path | grep -oP '(?<=^/).*(?=/$)')" ]; then
        #aliyun_backup_remote_path="$(echo $aliyun_backup_remote_path | grep -oP '^/.*(?=/$)')"
      #fi
    #fi
    #aliyunpan-cli mkdir $aliyun_backup_remote_path/backup
    #aliyunpan-cli -f auto_backup.log sync --no-delete $backup_path $aliyun_backup_remote_path 2>&1 #| sudo tee -a $backup_path/auto_backup.log



另外,我还在备份ha的备份语句后面跟着加了个备份nodered的命令,然后也一并备份成功了。。。哈哈哈

大佬看看那段判断的语句吧,可能问题出现在这块。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-12-22 16:50 , Processed in 0.056861 second(s), 32 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表