Skip to content

Instantly share code, notes, and snippets.

@elleryq
Last active September 4, 2019 06:03
Show Gist options
  • Save elleryq/20c779f633f875d6dbabeb74c61fbdf9 to your computer and use it in GitHub Desktop.
Save elleryq/20c779f633f875d6dbabeb74c61fbdf9 to your computer and use it in GitHub Desktop.
自動刪除 NAS 上變更時間超過七天的檔案
#!/bin/bash
# 目的:自動刪除變更時間超過七天的檔案
# 環境:
# 1. NAS 有開放 FTP
# 2. host, username, password 放在 /etc/login.cfg ,格式遵循 ncftp 規定,內容大致如下
# host your_host
# user your_user
# pass your_pass
set -e
LOGIN_CFG=/etc/login.cfg
MOUNT_POINT=/tmp/nas
SUBDIRS=("gitlab" "redmine" "nexus3" "jenkins")
HOST=$(awk '/^host/{print $2;}' ${LOGIN_CFG})
USER=$(awk '/^user/{print $2;}' ${LOGIN_CFG})
PASS=$(awk '/^pass/{print $2;}' ${LOGIN_CFG})
# Detect mountpoint, if mounted, umount it
set +e
mountpoint ${MOUNT_POINT}
if [ $? -eq 0 ]; then
# Try umount
fusermount -u ${MOUNT_POINT}
fi
set -e
# Try to mount NAS
mkdir -p ${MOUNT_POINT}
curlftpfs -o user=${USER}:${PASS} ${HOST} ${MOUNT_POINT}
for SUBDIR in "${SUBDIRS[@]}"; do
find ${MOUNT_POINT}/BackAP/${SUBDIR} -type f -mtime +7 -exec rm -f {} \;
done
fusermount -u ${MOUNT_POINT}
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment