赞
踩
很久没写专业领域之外的笔记了,这种东西还是能够外网发表的,想想还是发出来
也适用其他nas,以及主流Linux机器
本文涉及到
开启远程调试功能后拼接密码
绿联云DX4600基于Openwrt,不存在/usr/local目录
curl -L "https://github.com/docker/compose/releases/download/v2.2.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/bin/docker-compose
or
curl -L https://get.daocloud.io/docker/compose/releases/download/v2.4.1/docker-compose-`uname -s`-`uname -m` > /usr/bin/docker-compose
chmod +x /usr/bin/docker-compose
docker-compose exec database psql teslamate teslamate
docker volume rm $(docker volume ls -qf dangling=true)
root@UGREEN-A8C5:~# docker-compose version
docker-compose version 1.16.1, build 6d1ac21
docker-py version: 2.5.1
CPython version: 2.7.13
OpenSSL version: OpenSSL 1.0.1t 3 May 2016
官方地址:https://docs.teslamate.org/docs/installation/docker
我将文件放在了存储目录中,后续的所有操作大致都是在此目录下进行(在DX4600根目录也可以)
找到该文件位置
root@UGREEN-A8C5:~# find / -name teslamate
/mnt/dm-1/.ugreen_nas/132060/teslamate
root@UGREEN-A8C5:~# cd /mnt/dm-1/.ugreen_nas/132060/teslamate
root@UGREEN-A8C5:/mnt/dm-1/.ugreen_nas/132060/teslamate# ls
docker-compose.yml
docker-compose文件内容如下:
version: "3" services: teslamate: image: teslamate/teslamate:1.27.2 #由于不知道什么原因,我下载laster版本是1.25,因此手动制定了当前最新版本 restart: always environment: - ENCRYPTION_KEY= #需要修改为自己的key,删除等号后的空格 - DATABASE_USER=teslamate - DATABASE_PASS=Aa123456 #可自定义,三方保持一致 - DATABASE_NAME=teslamate - DATABASE_HOST=database - MQTT_HOST=mosquitto ports: - 4000:4000 volumes: - ./import:/opt/app/import cap_drop: - all database: image: postgres:14 #当前最新版本 restart: always environment: - POSTGRES_USER=teslamate - POSTGRES_PASSWORD=Aa123456 #可自定义,三方保持一致 - POSTGRES_DB=teslamate volumes: - teslamate-db:/var/lib/postgresql/data grafana: image: ccr.ccs.tencentyun.com/lei-wei/grafana-tesla-cn:v1.0 #汉化的grafana restart: always environment: - DATABASE_USER=teslamate - DATABASE_PASS=Aa123456 #可自定义,三方保持一致 - DATABASE_NAME=teslamate - DATABASE_HOST=database ports: - 3000:3000 volumes: - teslamate-grafana-data:/var/lib/grafana mosquitto: image: eclipse-mosquitto:2 restart: always command: mosquitto -c /mosquitto-no-auth.conf # ports: # - 1883:1883 volumes: - mosquitto-conf:/mosquitto/config - mosquitto-data:/mosquitto/data volumes: teslamate-db: teslamate-grafana-data: mosquitto-conf: mosquitto-data:
其中有一些需要解释的点
docker-compose up -d
如果是首次安装的话到此结束,后续开车,等待接收数据即可
自带的一些面板显示了挺全面的参数,但是依旧可以自定义一些,如电池健康度等。本质就是添加json文件,使用sql语句去teslamate查询相关参数,因此可以直接导入dashboard。但是由于汉化Grafana的权限原因,我懒得调了,直接使用脚本自带的方式进行添加吧。
地址:https://github.com/CarlosCuezva/dashboards-Grafana-Teslamate#how-to-create-a-grafana-api-key
下载文件放到当前目录下
unzip dashboards-Grafana-Teslamate-1.21.1.zip
cd dashboards-Grafana-Teslamate-1.21.1
chmod +x dashboards.sh
./dashboards.sh
第一步有4个输入点
What do you want to do?
1) Generate config file
2) Download lastest release
3) Install/Update dashboards
4) Quit
Please, enter your choice: 1
Enter the Grafana URL (e.g. http://localhost:3000): #输入你的访问地址,末尾不要带/
Enter the relative or absolute path of the dashboards directory (e.g. ./dashboards): ./dashboards
Enter the Grafana TOKEN: #输入Grafana token
File "config.sh" generated correctly.
Press any key to continue
粘贴进命令行即可
输入2
What do you want to do?
1) Generate config file
2) Download lastest release
3) Install/Update dashboards
4) Quit
Please, enter your choice: 2
Downloaded version "v1.21.1" successfully
Press any key to continue
输入3
What do you want to do? 1) Generate config file 2) Download lastest release 3) Install/Update dashboards 4) Quit Please, enter your choice: 3 URL: http://192.168.31.254:3000 DASHBOARDS_DIRECTORY: ./dashboards DESTINATION_DIRECTORY: Teslamate - Custom RESTORED battery-health.json RESTORED charges.json RESTORED charging-costs-stats.json RESTORED charging-curves.json RESTORED charging-tops.json RESTORED current-charge-view.json RESTORED drive-tops.json RESTORED drives.json RESTORED locations.json RESTORED overview.json RESTORED states.json RESTORED tire-pressure.json Installation/update process completed Press any key to continue
这样就会有一个新的扩展面板,有些是新加的,有些是优化的
官方地址:https://docs.teslamate.org/docs/maintenance/backup_restore
因为我之前是搭载vps上的,但是vps总会过期,入手了NAS就能搭在本地了,并且考虑数据的安全问题,备份与恢复也是离不开的话题
在docker-compose.yml目录下执行以下命令,出现文件即备份成功
docker-compose exec -T database pg_dump -U teslamate teslamate > teslamate.bck
在docker-compose.yml目录下执行以下命令,停止teslamate容器
docker-compose stop teslamate
在docker-compose.yml目录下执行以下命令,删除现有数据并重新初始化,注意最后的点
docker-compose exec -T database psql -U teslamate << .
drop schema public cascade;
create schema public;
create extension cube;
create extension earthdistance;
CREATE OR REPLACE FUNCTION public.ll_to_earth(float8, float8)
RETURNS public.earth
LANGUAGE SQL
IMMUTABLE STRICT
PARALLEL SAFE
AS 'SELECT public.cube(public.cube(public.cube(public.earth()*cos(radians(\$1))*cos(radians(\$2))),public.earth()*cos(radians(\$1))*sin(radians(\$2))),public.earth()*sin(radians(\$1)))::public.earth';
.
在docker-compose.yml目录下执行以下命令,恢复数据
docker-compose exec -T database psql -U teslamate -d teslamate < teslamate.bck
在docker-compose.yml目录下执行以下命令,开启teslamate容器
docker-compose start teslamate
视频演示可以查看https://www.youtube.com/watch?v=taOtmhADvk4
视频作者还提供了相关备份、恢复、更新的脚本,感兴趣的同学可以下载研究一下,地址:https://github.com/carloscuezva/teslamate-maintenance
这也是不可或缺的一部分,万一哪天容器或nas挂了呢?
既然上面视频作者已经写了脚本了,直接使用即可
给文件添加执行权限
chmod +x backupTeslaMate.sh
当前版本的文件与作者视频演示的多了一些内容,即删除超过7天的备份文件,但是执行下来会报错,问题出在find命令上,虽然不影响备份,但是我觉得有报错比较丑,于是我删除了该内容
#!/bin/bash # # Description: Script to backup the TeslaMate database # # Author: Carlos Cuezva # Created: 23/04/2021 # Last update: 07/04/2023 PATH_COMPOSE='/mnt/dm-1/.ugreen_nas/132060/teslamate' #改成自己存放docker-compose.yml文件的位置 PATH_BACKUP='/mnt/dm-1/.ugreen_nas/132060/teslamate/backup' #改成自己期望存放备份文件的位置 REMOVE_OLDER=7 echo -e '\n---- Starting the backup ----\n' cd $PATH_COMPOSE FILENAME=`date +"%Y%m%d_%H%M"`_teslamate.bck docker-compose exec database pg_dump -U teslamate teslamate > $PATH_BACKUP/$FILENAME cd $PATH_BACKUP gzip -9 -f $FILENAME FILESIZE=`du -h "$FILENAME.gz" | cut -f1` echo -e "\nThe size of the backup is $FILESIZE\n" #echo -e "\nRemoving backups older than $REMOVE_OLDER days\n" #find $PATH_BACKUP -name '*_teslamate.bck.gz' -type f -mtime +$REMOVE_OLDER -delete echo -e '\n---- Backup finished ----\n'
执行效果大概如下
添加定时任务
crontab -e
输入存放该文件的路径,注意使用绝对路径,每天凌晨0点进行一次备份,计划可以自行修改
0 0 * * * /xxx/backupTeslaMate.sh
#我的是
0 0 * * * /mnt/dm-1/.ugreen_nas/132060/teslamate/teslamate-maintenance-main/backupTeslaMate.sh
直接添加定时任务
0 0 * * * /usr/bin/docker-compose -f /xxx/docker-compose.yml exec -T database pg_dump -U teslamate teslamate > teslamate_wcx.bck
未知原因,我进行恢复时没有效果,表现为执行命令时无回显
后来看到https://www.xiaote.com/r/6437be9d9bee997d2d481e16
尝试进入容器再执行命令,果然tmd可以
输入以下命令进入数据库容器
docker-compose exec database psql teslamate teslamate
在内部执行以下命令
drop schema public cascade;
create schema public;
create extension cube;
create extension earthdistance;
CREATE OR REPLACE FUNCTION public.ll_to_earth(float8, float8)
RETURNS public.earth
LANGUAGE SQL
IMMUTABLE STRICT
PARALLEL SAFE
AS 'SELECT public.cube(public.cube(public.cube(public.earth()*cos(radians($1))*cos(radians($2))),public.earth()*cos(radians($1))*sin(radians($2))),public.earth()*sin(radians($1)))::public.earth';
exit退出容器
然后输入以下命令,将备份文件拷贝到容器根目录
docker cp teslamate.bck f704e1057ece:/
输入以下命令进入容器
docker exec -it teslamate_database_1 bash
执行以下命令恢复
psql -U teslamate -d teslamate < teslamate.bck
exit退出,启动teslamate即可
nas,teslamate等还有很多有趣的玩法,但是累了,不折腾了。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。