-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMFB2AD.sh
61 lines (50 loc) · 1.91 KB
/
MFB2AD.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
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash
#Written by alicecantsleep and chatGPT
#Backs up files specified to target directories and removes
#all but newest 3 files. This can be adjusted on line 48.
#WARNING - WILL DELETED ALL FILES IN DESTINATION DIRECTORIES
#HIGHLY recommended to backup to a new directories
#DON'T FORGET THE CONFIG FILE
# Load the configuration
source 'MFB2AD_config.ini'
# Loop through each file in the file_list
for file_data in "${file_list[@]}"; do
# Parse file and backup directory from file_data
file=$(echo "$file_data" | awk -F':' '{print $1}')
backup_dir=$(echo "$file_data" | awk -F':' '{print $2}')
# Create the backup directory if it doesn't exist
mkdir -p "$backup_dir"
# Check if the file exists
if [ -f "$file" ]; then
# Create the backup file name with today's date
backup_file="${backup_dir}/$(date +%Y%m%d)_$(basename "$file")"
# Copy the file to the backup location
cp "$file" "$backup_file"
# Check if the backup was successful
if [ $? -eq 0 ]; then
echo "Backup created for $file: $backup_file"
else
echo "Backup failed for file: $file"
fi
else
echo "File does not exist: $file"
fi
done
# Cleanup old backups, keeping only the newest 3 for each file
for file_data in "${file_list[@]}"; do
# Parse file and backup directory from file_data
file=$(echo "$file_data" | awk -F':' '{print $1}')
backup_dir=$(echo "$file_data" | awk -F':' '{print $2}')
backup_files=("$backup_dir"/*)
num_backups=${#backup_files[@]}
num_backups_to_keep=3
if [ "$num_backups" -gt "$num_backups_to_keep" ]; then
files_to_delete=("${backup_files[@]:num_backups_to_keep}")
# Delete older backup files
for file in "${files_to_delete[@]}"; do
rm "$file"
echo "Deleted backup for $file: $file"
done
fi
done
echo "Backup and cleanup completed."