我正在使用dropbox_uploader.sh运行备份我的数据库。 backupd通过基于日期的命名约定保存在dropbox中:
for (var j = 0; j < (nCols / nRows); j++) {
}
。例如DATE=$(date +"%d-%m-%Y_%H%M")
BKP_FILE="pal_BK_$DATE.sql"
。
有没有办法从dropbox中删除超过一个月的备份?
答案 0 :(得分:2)
如果不修改日期格式,文件之间的比较会变得有点奇怪,但这样会好起来的。
我们的想法是以秒为单位计算当前日期(以您的格式)和限制删除日期(之前1个月)。然后,对于列出的文件的每个条目,我们检索日期,我们将其转换为秒,并且我们与所述限制进行int-比较。如果文件足够旧,我们将其删除。
#!/bin/bash
#####################
# Convert a given date to s (leaves result in DATE_IN_S)
#####################
dateToS() {
local date=$1
local year=$(echo $date | tr "-" "\t" | tr "_" "\t" | awk {' print $3 '})
local month=$(echo $date | tr "-" "\t" | tr "_" "\t"| awk {' print $2 '})
local day=$(echo $date | tr "-" "\t" | tr "_" "\t" | awk {' print $1 '})
local hour=$(echo $date | tr "-" "\t" | tr "_" "\t" | awk {' print $4 '} | cut -c -2)
local minute=$(echo $date | tr "-" "\t" | tr "_" "\t" | awk {' print $4 '} | cut -c 3-)
local seconds="00"
# Compute the date time in s
DATE_IN_S=$(date -d "${year}-${month}-${day} ${hour}:${minute}:${seconds}" +%s)
}
#####################
# MAIN CODE
#####################
DEST_DIR= # Dropbox backups base folder
DATE=$(date +"%Y-%m-%d_%H%M")
BKP_FILE="pal_BK_${DATE}.sql"
# Compute the limit date to erase files
LIMIT_DATE=$(date +%s)
LIMIT_DATE=$((LIMIT_DATE-2592000)) # 1 month in seconds
# Retrieve the list of files
files=$(./dropbox_uploader.sh list $DEST_DIR | awk {' print $3 '} | tail -n +2)
# Process each file
for file in $files; do
fileDate=$(echo $file | tr "_" "\t" | tr "." "\t" | awk {' print $3"_"$4 '})
# Retrieve file date in seconds
dateToS $fileDate
# Erase the file if it exceeds the limit date
#echo "[DEBUG] Comparing ${DATE_IN_S} - ${LIMIT_DATE}"
if [ ${DATE_IN_S} -lt ${LIMIT_DATE} ]; then
echo "[INFO] Erasing file $file"
./dropbox_uploader.sh delete ${DEST_DIR}${file}
fi
done
请注意: