-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbackup-base.sh
More file actions
executable file
·66 lines (49 loc) · 2.4 KB
/
backup-base.sh
File metadata and controls
executable file
·66 lines (49 loc) · 2.4 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
56
57
58
59
60
61
62
63
64
65
66
#!/bin/sh
# Constants
HOST=$(hostname)
DISK_PATH=$1
BACKUP_DIR="$DISK_PATH/backup-$HOST"
# Function to calculate free and used space
calculate_space() {
echo "Calculating space for $DISK_PATH..."
FREE_SPACE=$(df "$DISK_PATH" | awk 'NR==2 {print $4}')
USED_SPACE_HOME=$(df /home | awk 'NR==2 {print $3}')
WILL_FIT=$((FREE_SPACE - USED_SPACE_HOME))
}
# Exclude the oldest backup until the drive can fit the home directory
make_space() {
calculate_space "$DISK_PATH"
while [ "$WILL_FIT" -lt 0 ]; do
echo "Drive has $FREE_SPACE KB free, home uses $USED_SPACE_HOME KB, needs $WILL_FIT KB more."
# Find the oldest backup file
FILE_TO_EXCLUDE=$(ls -1 "$BACKUP_DIR" | head -n 1)
# Check if a file was found before attempting to delete
if [ -z "$FILE_TO_EXCLUDE" ]; then
echo "No backups found to delete."
exit 1
fi
echo "Deleting $FILE_TO_EXCLUDE\n"
rm -fr "$BACKUP_DIR"/"$FILE_TO_EXCLUDE"
# Recalculate space after deletion
calculate_space "$DISK_PATH"
done
}
# Backup home directory
backup() {
DATE=$(date +%Y-%m-%d)
# Excluded directories
EXCLUDES="--exclude=lost+found --exclude=.cache --exclude=.config/autostart/ --exclude=.config/VirtualBox/ --exclude=.local/share/Trash --exclude=videos/ --exclude=.local/share/Steam --exclude=.local/share/qutebrowser --exclude=.local/share/cargo --exclude=.local/share/ULWGL --exclude=.local/share/snyk-ls --exclude=.config/BraveSoftware --exclude=.config/Slack --exclude=.config/heroic --exclude=.config/EmuDeck --exclude=.config/wall-d/install --exclude=no-backup/ --exclude=.local/share/nvim/mason/packages --exclude=.local/share/nvim/lazy --exclude=.local/share/Anki2 --exclude=.local/share/spotify-launcher --exclude=.config/discord --exclude=.config/dwm/ultra-wide-dwm/dwm --exclude=.config/dwm/ultra-wide-dwm/*.o --exclude=.steam/ --exclude=.config/dwmblocks/laptop-dwmblocks/build/ --exclude=.config/dmenu/*.o --exclude=.config/dmenu/dmenu --exclude=.config/dmenu/stest --exclude=.config/dwm/laptop-dwm/*.o --exclude=.config/dwm/laptop-dwm/dwm"
# Logging the backup operation
echo "Backing up home directory to $BACKUP_DIR/$DATE-$HOST"
# Build rsync command
RSYNC_CMD="rsync -a --info=progress2 $EXCLUDES $HOME/ $BACKUP_DIR/$DATE-$HOST"
# Perform the backup
$RSYNC_CMD
}
# Check if the script received a parameter
if [ -z "$1" ]; then
echo "Usage: $0 <disk_path>"
exit 1
fi
# make_space "$1"
backup "$1"