-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup_script.sh
More file actions
executable file
·38 lines (30 loc) · 968 Bytes
/
backup_script.sh
File metadata and controls
executable file
·38 lines (30 loc) · 968 Bytes
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
#!/bin/bash
# Define variables
BACKUP_SRC=$1
BACKUP_DEST=$2
LOG_FILE="/var/log/backup_script.log"
TIMESTAMP=$(date +"%Y-%m-%d_(%H:%M:%S)")
BACKUP_FILE="${BACKUP_DEST}/backup_${TIMESTAMP}.tar.gz"
# Function to log messages
log_message() {
echo "$1" | tee -a $LOG_FILE
}
# Check if source directory exists
if [ ! -d "$BACKUP_SRC" ]; then
log_message "ERROR: Source directory $BACKUP_SRC does not exist."
exit 1
fi
# Check if destination directory exists, create if not
if [ ! -d "$BACKUP_DEST" ]; then
mkdir -p $BACKUP_DEST
fi
# Perform the backup with compression and encryption
tar -czf - $BACKUP_SRC | openssl enc -aes-256-cbc -e -out $BACKUP_FILE -pass pass:yourpassword 2>> $LOG_FILE
if [ $? -eq 0 ]; then
BACKUP_SIZE=$(du -sh $BACKUP_FILE | cut -f1)
log_message "Backup successful: $BACKUP_FILE (Size: $BACKUP_SIZE)"
else
log_message "ERROR: Backup failed for $BACKUP_SRC."
echo "Backup failed: $BACKUP_SRC."
exit 1
fi