shellscript tar問題 - Linux

Table of Contents

不好意思我又來了...
這幾天遇到一些問題想破頭只想出一點東西 決定還是來跟各位求助..
因為剛學script 是超級菜雞 寫得很雜亂請大家見諒

#!/bin/bash

last_month=$(date -d "$(date +%Y%m)01 last month" +%Y%m)
Source=/home/ray/photo
dest=/home/ray/photo2
sourcefilesize=$(ls -l /home/ray/photo/$last_month* | awk '{ SUM += $5} END {
print SUM}')
#destfilesize=$(ls -l /home/ray/photo3/$last_month* | awk '{ SUM += $5} END {
print SUM}')
today=$(date +"%Y-%m-%d")
zipname=$TODAY.tar.gz
zipfilename=${last_month:2}
verifydest=/home/ray/photo3



cd /home/ray/photo

file=$(ls -d $last_month*)

tar -zcvPf /home/ray/photo2/$zipfilename.tar.gz $file

tar -zxvf /home/ray/photo2/$zipfilename.tar.gz -C /home/ray/photo3

destfilesize=$(ls -l /NAS/Raytest3/$last_month* | awk '{ SUM += $5} END {
print SUM}')

if [ $sourcefilesize == $destfilesize ];
then
rm -rf $Source/$file
else
exit
fi

if [ $sourcefilesize == $destfilesize ];
then
rm -rf $verifydest/$file
else
exit
fi



流程是這樣的

首先每個月1號將/home/ray/photo 上個月份的所有資料打包放到/home/ray/photo2 並命
名為2107.tar.gz(8月1號執行)

再將2107.tar.gz解包將資料放到/home/ray/photo3

然後比對/home/ray/photo 跟 /home/ray/photo3兩邊檔案大小(驗證是否正常) 如果OK就
把他們的資料刪掉



目前我測試時有準備兩個自建的資料夾20210701 & 20210702 放到/home/ray/photo

將20210701 & 20210702 打包命名2107.tar.gz 放到/home/ray/photo2

再將/home/ray/photo2的2107.tar.gz解包放到/home/ray/photo3

比對/home/ray/photo跟/home/ray/photo3 的20210701 & 20210702檔案大小是否一樣

確定一樣並刪除/home/ray/photo的 20210701 & 20210702

但是/home/ray/photo3的20210701 & 20210702 只有刪掉2021072

刪除/home/ray/photo3的時候只刪掉20210702而20210702還在沒有刪掉

不知道問題在哪裡...



--

All Comments

Victoria avatarVictoria2021-08-10
備份的話,不考慮rsync?
Robert avatarRobert2021-08-14
比對檔案的話,diff或md5也許比較好?
Callum avatarCallum2021-08-19
tar可以直接pipe跨檔案系統/目錄,不需要暫存檔
Regina avatarRegina2021-08-23
感謝O大建議 我很需要XD
Bethany avatarBethany2021-08-28
rm -rf $verifydest/$file 會變成 rm -rf 3/file1
file2 第二個檔案沒有 3/ 路徑. 你可以 cd 先 cd 再
rm
Hedy avatarHedy2021-09-01
建議先裝個 shellcheck 把語法修一修
看到 rm -rf 後面的變數沒 quoting 實在是冒冷汗
Kyle avatarKyle2021-09-06
感謝兩位 !!
Olivia avatarOlivia2021-09-10
你的 $today 是大寫還是小寫?
Rosalind avatarRosalind2021-09-15
today這個變數其實沒用到XD