forked from J-Bentley/mc-backup.sh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mc-manage-backups.sh
executable file
·49 lines (36 loc) · 1.67 KB
/
mc-manage-backups.sh
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
#!/bin/bash
pathFile=$(readlink -f "$BASH_SOURCE")
dirname=${pathFile%/*}
required_files=(constants.sh lib.sh)
for v in ${required_files[*]}; { [[ -r "$dirname"/${v} ]] || { echo "File ${v} not found. Exiting..."; $exit; }; }
source "$dirname"/constants.sh
source "$dirname"/lib.sh
checkBackupBucket "$1"
maxBackupCount=14
backupFilesCount=$(ls "$backupDir" | wc -w)
backupFiles=($(ls "$backupDir"))
cloudBackupFilesCount=$($oci_path os object list -bn "$backupBucket" | jq '.data | length')
mapfile -t cloudBackupFiles < <($oci_path os object list -bn "$backupBucket" | jq --raw-output '.data | .[] | .name' | sort)
log "[$(currentDay)] Amount of backups are at $backupFilesCount files\n"
if [ "$backupFilesCount" -gt "$maxBackupCount" ]; then
diff=$(("$backupFilesCount"-"$maxBackupCount"-1))
diffLog=$(("$diff"+1))
log "[$(currentDay)] Amount of backups have surpassed $maxBackupCount. Deleting $diffLog old files.\n"
for i in $(seq 0 $diff); do
file="${backupFiles[i]}"
log "[$(currentDay)] Local | Deleting $file.\n"
sudo -u "$minecraftUser" rm "$backupDir/$file"
done
fi
log "[$(currentDay)] Amount of Cloud Backups are at $cloudBackupFilesCount files\n"
if [ "$cloudBackupFilesCount" -gt "$maxBackupCount" ]; then
diff=$(("$cloudBackupFilesCount"-"$maxBackupCount"-1))
diffLog=$(("$diff"+1))
log "[$(currentDay)] Amount of Cloud backups have surpassed $maxBackupCount. Deleting $diffLog old files.\n"
for i in $(seq 0 $diff); do
file="${cloudBackupFiles[i]}"
log "[$(currentDay)] Cloud | Deleting $file.\n"
$oci_path os object delete --force -bn "$backupBucket" --object-name "$file"
done
fi
$exit 0