Skip to content

Commit 34e6558

Browse files
back up daily: 0 12 * * *
1 parent 16b2764 commit 34e6558

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

scripts/backup_script.sh

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/bin/bash
2+
3+
# Function to set USB_MOUNT_PATH if default value is not defined
4+
set_mount_path() {
5+
if [ -d "$USB_MOUNT_PATH" ]; then
6+
echo "USB drive is already mounted at $USB_MOUNT_PATH"
7+
else
8+
# Navigate to /media/user and find the last mounted directory
9+
cd /media/$USER || exit
10+
11+
# Get the last directory that was mounted
12+
LAST_MOUNTED_DIR=$(ls -1dt */ | head -n 1)
13+
14+
if [ -z "$LAST_MOUNTED_DIR" ]; then
15+
# If no directory found, exit with an error
16+
echo "Error: No USB drive found in /media/$USER."
17+
exit 1
18+
fi
19+
20+
USB_MOUNT_PATH="/media/$USER/${LAST_MOUNTED_DIR}Backup/home"
21+
22+
if [ ! -d "$USB_MOUNT_PATH" ]; then
23+
# If no directory found, create one
24+
echo "Creating new directory: $USB_MOUNT_PATH"
25+
mkdir -p "$USB_MOUNT_PATH"
26+
else
27+
echo "Operation failed at $(date)! Exiting... " >> "$LOG_FILE"
28+
exit 1
29+
fi
30+
31+
echo "USB drive not found at the specified path. Using fallback path: $USB_MOUNT_PATH"
32+
fi
33+
}
34+
35+
# Default mount path
36+
USB_MOUNT_PATH="/media/$USER/6e80e928-063f-49b7-821b-f0462abaa997/Backup/home"
37+
38+
# Log file path
39+
LOG_FILE="/var/log/backup_script.log"
40+
touch $LOG_FILE && sudo chown $USER $LOG_FILE
41+
42+
# Set the mount path
43+
set_mount_path
44+
45+
# Run the backup only if USB drive is mounted
46+
if [ -d "$USB_MOUNT_PATH" ]; then
47+
# Perform backup
48+
sudo rsync -a --info=progress2 --human-readable --exclude="lost+found" --exclude=".cache" --exclude=".ansible" --exclude="Downloads" --exclude="VirtualBox VMs" /home/ "$USB_MOUNT_PATH"/
49+
50+
# Log status
51+
echo "Backup completed successfully at $(date)" >> "$LOG_FILE"
52+
echo "To restore the backup, use the following command:" >> "$LOG_FILE"
53+
echo "sudo rsync -a --info=progress2 --exclude=\"lost+found\" \"$USB_MOUNT_PATH\"/ /home/" >> "$LOG_FILE"
54+
55+
# Display status
56+
echo "Backup completed successfully."
57+
else
58+
# Log error
59+
echo "Error: USB drive not found. Backup not performed." >> "$LOG_FILE"
60+
61+
# Display error
62+
echo "Error: USB drive not found. Backup not performed. Check $LOG_FILE for details."
63+
fi

0 commit comments

Comments
 (0)