-
Notifications
You must be signed in to change notification settings - Fork 9
/
backup_config.sh
executable file
·37 lines (34 loc) · 1.22 KB
/
backup_config.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
#!/usr/bin/env bash
# Set variables
SOURCE_DIR="/.config"
TEMP_BACKUP_DIR="/tmp"
REMOTE_BACKUP_DIR="/mnt/backup-server/.config"
LOG_FILE="/var/log/rsync-config-backup.log"
TIMESTAMP=$(date +'%Y-%m-%d_%H%M%S')
ARCHIVE_NAME="config_backup_$TIMESTAMP.tar.gz"
EXCLUDE_PATTERNS=(
'--exclude=*.jpg'
'--exclude=*.jpeg'
'--exclude=*.png'
'--exclude=*.gif'
'--exclude=*.mp3'
'--exclude=*.mp4'
'--exclude=*.avi'
'--exclude=*.mkv'
'--exclude=*.flac'
'--exclude=plexmediaserver/*'
)
# Create the archive in /tmp and check if the archive was created successfully
if tar -czvf "$TEMP_BACKUP_DIR/$ARCHIVE_NAME" "${EXCLUDE_PATTERNS[@]}" -C "$SOURCE_DIR" . >"$LOG_FILE" 2>&1; then
echo "Archive created successfully: $TEMP_BACKUP_DIR/$ARCHIVE_NAME" >>"$LOG_FILE"
# Sync the archive to the remote backup directory
if rsync -av "$TEMP_BACKUP_DIR/$ARCHIVE_NAME" "$REMOTE_BACKUP_DIR" >>"$LOG_FILE" 2>&1; then
echo "Backup synced successfully: $REMOTE_BACKUP_DIR/$ARCHIVE_NAME" >>"$LOG_FILE"
# Optionally, remove the local archive after successful sync
rm "$TEMP_BACKUP_DIR/$ARCHIVE_NAME"
else
echo "Failed to sync the backup to the remote server" >>"$LOG_FILE"
fi
else
echo "Failed to create the archive" >>"$LOG_FILE"
fi