-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogs_cleaner_main.sh
More file actions
executable file
·55 lines (33 loc) · 1.48 KB
/
Copy pathlogs_cleaner_main.sh
File metadata and controls
executable file
·55 lines (33 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/bash
# ------------------------------------------
# Initialising varibales, paths, etc.
# sources
SOURCE_PATH=<source_path>
SOURCE_FILE_NAME=<source_file_name>
DESTINATION_PATH=<destination_path>
DESTINATION_FILE_BASE_NAME="${SOURCE_FILE_NAME%.*}"
CURRENT_DATE=$(date +"%Y_%m_%d")
CURRENT_EPOCH=$(date +%s)
EXPIRATION_TIME_IN_DAYS=<number of days for deletion>
EXPIRATION_EPOCH=$(($EXPIRATION_TIME_IN_DAYS * 86400))
# ------------------------------------------
# ------------------------------------------
# Zipping the source file into destination and then truncating the source file
zip -j "${DESTINATION_PATH}/${DESTINATION_FILE_BASE_NAME}_${CURRENT_DATE}.zip" $SOURCE_PATH/$SOURCE_FILE_NAME
truncate -s 1K $SOURCE_PATH/$SOURCE_FILE_NAME
# ------------------------------------------
# -----------------------------------------
# Deleting files older than above mentioned days
# Getting all file names in destination path
ALL_ZIPPED_FILES=$(ls $DESTINATION_PATH)
# Checking age of all files and deleting which are older than Expiration time in days.
for CURRENT_FILE in ${ALL_ZIPPED_FILES[@]}; do
CURRENT_FILE_AGE_EPOCH=$(($CURRENT_EPOCH - $(stat -c %Y $DESTINATION_PATH/$CURRENT_FILE)))
if [ "$CURRENT_FILE_AGE_EPOCH" -gt "$EXPIRATION_EPOCH" ]; then
echo "Deleting: ${CURRENT_FILE}"
rm $DESTINATION_PATH/$CURRENT_FILE
else
echo "${CURRENT_FILE} does not match the delete criteria."
fi
done
# -----------------------------------------