-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbin bash Backup Script.sh
More file actions
46 lines (37 loc) · 1.04 KB
/
bin bash Backup Script.sh
File metadata and controls
46 lines (37 loc) · 1.04 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
#!/bin/bash
# Backup Script
# Creates a compressed backup with timestamp
# Check if source is provided
if [ $# -eq 0 ]; then
echo "Usage: $0 [destination_path]"
echo "Example: $0 ~/Documents ~/Backups"
exit 1
fi
# Set source and destination
SOURCE="$1"
DESTINATION="${2:-$HOME/Backups}"
# Check if source exists
if [ ! -e "$SOURCE" ]; then
echo "Error: Source '$SOURCE' does not exist"
exit 1
fi
# Create destination directory if it doesn't exist
mkdir -p "$DESTINATION"
# Create timestamp
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
# Get the base name of the source
BASENAME=$(basename "$SOURCE")
# Create backup filename
BACKUP_NAME="${BASENAME}_${TIMESTAMP}.tar.gz"
# Create the backup
echo "Creating backup of $SOURCE..."
tar -czf "$DESTINATION/$BACKUP_NAME" -C "$(dirname "$SOURCE")" "$(basename "$SOURCE")"
# Check if backup was successful
if [ $? -eq 0 ]; then
echo "Backup created successfully: $DESTINATION/$BACKUP_NAME"
# Display backup size
du -h "$DESTINATION/$BACKUP_NAME"
else
echo "Error: Backup failed"
exit 1
fi