找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
广告投放联系QQ68610888
查看: 634|回复: 2

openwrt的dl目录增量(差异)备份shell脚本

[复制链接]
发表于 2022-3-14 09:02 | 显示全部楼层 |阅读模式
本帖最后由 piazini 于 2022-3-27 21:32 编辑

openwrt的dl目录下载多费事我就不说了,但是每次更新后,都全部打包一遍,浪费时间也浪费空间。

写了sh脚本,方便增量备份。

dl.zip文件下载连接在文末。

2022/03/27:更新内容:
  更新后总体显示更简洁,只保留打包好的zip文件和列表文件。其他临时文件删除或隐藏。(提示:按Ctrl+h组合键可显示.点开头的隐藏文件)

  *.隐藏了一些记录文件。
  *.取消了temp文件。
  *.增加清理临时文件。
  *.修改了一些命令,支持备份文件夹



*首次使用前,把“openwrt路径”和“旧的dl.zip文件路径”改成你的
  1. #!/bin/bash
  2. #!/bin/bash
  3. # Date:2022/03/12
  4. # Date:2022/03/27
  5. #查看dl.zip和lede/dl/文件夹,包的差异
  6. #并自动复制打包

  7. datetime=`date +"%Y%m%d"`

  8. # openwrt的主目录,双引号里修改成你自己openwrt(lede)的dl路径
  9. openwrt_home=~/lede/dl/

  10. ## 当前备份shell脚本所在目录,不用改,自动获取sh文件自己所在目录
  11. sh_pwd=`pwd`
  12. #echo ${sh_pwd}

  13. ## 新建目录
  14. if [ ! -d ~/dl ];then
  15.     mkdir -p ${sh_pwd}/dl/
  16. fi
  17. ##新建.last_bak_pkg_lists.txt文件
  18. if [ ! -f .last_bak_pkg_lists.txt ];then
  19.     echo " " >> .last_bak_pkg_lists.txt
  20. fi

  21. ##新建dl_${datetime}.zip_lists.txt文件
  22. if [ ! -f dl_${datetime}.zip_lists.txt ];then
  23.     echo " " >> dl_${datetime}.zip_lists.txt
  24. fi

  25. ## 列出本级当前openwrt(lede)/dl/目录文件
  26. ls -1 ${openwrt_home} >.current_dl_lists_temp.txt

  27. ## 列出老的dl.zip包文件名
  28. unzip -v ~/dl.zip |awk -F'/' '{print $2}' | grep -vE "^$" > .old_dl_zip_lists.txt

  29. ## 列出dl.zip包里目录内容,对比后,差异放入dl_${datetime}.zip_lists.txt
  30. #unzip -v dl.zip |awk -F'/' '{print $2}' |grep -v "`cat 2dl.txt`" <-- 错误的过滤顺序
  31. cat .current_dl_lists_temp.txt| grep -v "`cat .old_dl_zip_lists.txt`"|grep -v "`cat .last_bak_pkg_lists.txt`"| grep -vE "^$" > dl_${datetime}.zip_lists.txt

  32. ## 备份列表空,则退出
  33. num_list=`wc -l dl_${datetime}.zip_lists.txt| awk '{print $1}'`
  34. if [ ${num_list} -eq 0 ];then
  35.     echo ""
  36.     ### 清理已经备份的临时dl文件夹(不是你openwrt(lede)目录的dl文件夹)
  37.     rm -rf dl/
  38.     ### 清理临时文件
  39.     rm -f .current_dl_lists_temp.txt .old_dl_zip_lists.txt dl_${datetime}.zip_lists.txt
  40.     echo "[ info ] Back list is null . wc -l dl_${datetime}.zip_lists.txt = ${num_list}"
  41.     echo ""
  42.     exit
  43. fi
  44. ## 备份新包
  45. cd ${openwrt_home}
  46. #pwd
  47. echo "[ info ] Begin copy 'dl_${datetime}.zip_lists.txt'"
  48. for file in `cat ${sh_pwd}/dl_${datetime}.zip_lists.txt`
  49. do
  50.     \cp -ar ${file} ${sh_pwd}/dl/
  51. done

  52. ##切换回sh文件所在目录
  53. cd ${sh_pwd}
  54. pwd

  55. ##打包
  56. zip -r dl_${datetime}.zip dl/

  57. ## 本次备份过的列表追加到上一次列表中
  58. ## 防止重复备份
  59. cat ${sh_pwd}/dl_${datetime}.zip_lists.txt >> ${sh_pwd}/.last_bak_pkg_lists.txt

  60. ## 清理已经备份的临时dl文件夹(不是你openwrt(lede)目录的dl文件夹)
  61. rm -rf dl/

  62. ## 清理临时文件
  63. rm -f .current_dl_lists_temp.txt .old_dl_zip_lists.txt

  64. echo ""
  65. echo "[ info ] Backup ZIP file name : dl_${datetime}.zip"
  66. echo "[ info ] Backup ZIP lists log file name : dl_${datetime}.zip_lists.txt"
  67. echo ""
  68. echo "[ info ] Backup 'dl' pkg done."
  69. echo ""
复制代码


截图:有背景色的就是,增量备份好的压缩包。



问:Linux下为什么不用tar包,用zip打包呢?
答:是为了通用性,win的机器上用打开tar需要额外装软件,而zip大部分系统都默认支持。


dl.zip下载连接:https://pan.baidu.com/s/1j9qZ2G7zyF7h5YF3nQJ4SA
dl文件链接下载来源:http://cnblogs.com/sbkill1r/p/14115667.html


发表于 2022-3-14 10:52 | 显示全部楼层
有个命令叫做 rsync

点评

要有自己写脚本的乐趣  详情 回复 发表于 2022-3-14 20:06
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-14 20:06 | 显示全部楼层
html 发表于 2022-3-14 10:52
有个命令叫做 rsync

要有自己写脚本的乐趣
回复 支持 反对

使用道具 举报

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

本版积分规则

有疑问请添加管理员QQ86788181|手机版|小黑屋|Archiver|恩山无线论坛(常州市恩山计算机开发有限公司版权所有) ( 苏ICP备05084872号 )

GMT+8, 2024-6-6 23:11

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

| 江苏省互联网有害信息举报中心 举报信箱:js12377 | @jischina.com.cn 举报电话:025-88802724 本站不良内容举报信箱:68610888@qq.com

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