-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSFB.sh
54 lines (43 loc) · 1.37 KB
/
SFB.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
#!/bin/bash
#Written by alicecantsleep and chatGPT
#Command: sh /sfb.sh <file> <backup_dir>
#Backs up file specified to directory specified and removes
#all but newest 3 files. This can be adjusted on line 41.
#WARNING - WILL DELETED ALL FILES IN DESTINATION DIRECTORY
#HIGHLY recommended to backup to a new directory
file=$1
backup_dir=$2
# Create the backup directory if it doesn't exist
mkdir -p "$backup_dir"
if [ -z "$file" ] || [ -z "$backup_dir" ]; then
echo "Usage: SFB.sh <file> <backup_dir>"
exit 1
fi
if [ ! -f "$file" ]; then
echo "File does not exist: $file"
exit 1
fi
# 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"
exit 1
fi
# Cleanup old backups, keeping only the newest 3
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
echo "Backup and cleanup completed."