写在前面
- 前面借助 exiv2 和 exiftool 做照片视频做了归档,是时候把需要的信息存起来了。
- 考虑到数据的安全性、读写的便利性,决定用 SQLite(Synology 内置)。
- 注:
Synology
还内置了 Redis、PostGreSQL。
建库
- 文件系统,只有文件全路径是唯一,其它信息均读出来方便日后查:
CREATE TABLE if not exists FileInfo (
FullPath NVARCHAR(100) PRIMARY KEY NOT NULL,
FileMd5 NCHAR(33) NOT NULL,
FileType CHARACTER(9),
FileSize INT NOT NULL,
CreateTime DATETIME,
UpdateTime DATETIME
);
CREATE INDEX if not exists FullPath ON FileInfo (FullPath);
CREATE INDEX if not exists CreateTime ON FileInfo (CreateTime);
CREATE INDEX if not exists FileType ON FileInfo (FileType);
CREATE INDEX if not exists FileMd5 ON FileInfo (FileMd5);
- 增删改查总得调试几把:
sqlite3 ~admin/www/Tmission/FileInfo.db 'select count(FullPath) from FileInfo where FullPath like "%王阳明故居%";'
- 输出 Excel 也挺方便:
sqlite3 -header -csv ~admin/www/Tmission/FileInfo.db "select FullPath,datetime(CreateTime,'unixepoch','localtime') from FileInfo where FullPath like '%国剧少年行%' limit 3"
# FullPath,"datetime(CreateTime,'unixepoch','localtime')"
# "/volume1/homes/higkoo/Drive/Backup/国剧少年行/20190109-134140.JPG","2019-01-09 13:41:40"
# "/volume1/homes/higkoo/Drive/Backup/国剧少年行/20190109-125415.JPG","2019-01-09 12:54:15"
# "/volume1/homes/higkoo/Drive/Backup/国剧少年行/20190109-112352.JPG","2019-01-09 11:23:52"
归档
- 全盘扫描,并存入必要的信息:
#!/bin/bash
sDir="${1:-/volume1/homes/higkoo/Drive/Backup/}" # 扫描目录
iDB="${2:-/volume1/homes/admin/www/Tmission/FileInfo.db}" # SQLite3数据库
readonly debug=true # 调试开关
readonly eLog="${iDB%%/*}Error.log" # 数据库目录同级存错误日志
find "${sDir}" ! -path "*@eaDir*" -type f | while read sPath; do
unset fMd5 cTime fSize fExt uTime
fExt="${sPath##*.}"
fMd5=`md5sum "${sPath}"` && fMd5="${fMd5%% *}"
cTime=`exiftool -d %s -DateTimeOriginal -S -s "${sPath}"`
fSize=`stat --printf=%s "${sPath}"`
uTime=`date +%s`
$debug && declare -p sPath fExt fSize fMd5 cTime uTime
if [[ ! -z $fMd5 && ! -z "$sPath" && ! -z $fSize ]]; then
eMd5=`sqlite3 "${iDB}" "select FileMd5 from FileInfo where FullPath='${sPath:-NULL}'" 2>>"${eLog}"`
if [[ -z "$eMd5" ]]; then
sqlite3 "${iDB}" "INSERT INTO FileInfo (FileMd5,FullPath,FileSize,FileType,CreateTime,UpdateTime) VALUES ('$fMd5','$sPath','$fSize','$fExt','$cTime','$uTime');" 2>>"${eLog}"
[[ $? -eq 0 ]] && echo -ne '+' || echo -ne '#'
else
if [[ "$eMd5" == "$fMd5" ]]; then
echo -ne '_'
else
sqlite3 "${iDB}" "UPDATE FileInfo SET (FileMd5,FileSize,FileType,CreateTime,UpdateTime) VALUES ('$eMd5','$fSize','$fExt','$cTime','$uTime') where FullPath='${sPath:-NULL}';" 2>>"${eLog}"
[[ $? -eq 0 ]] && echo -ne '-' || echo -ne '*'
fi
fi
else
echo "$(date +%F\ %T),${sPath}" >>"${eLog}"
echo -ne '.'
fi
$debug && exit 0
done
- 端杯水,让程序在后台跑一会:
nohup Task.sh > debug.log 2>&1 &
来源:oschina
链接:https://my.oschina.net/higkoo/blog/3188526