LinuxScripts/backup.sh

41 lines
1.3 KiB
Bash
Raw Permalink Normal View History

2024-04-12 13:20:39 +01:00
#!/bin/bash
# rsyncs a path to a remote server
# Sets Varibles that can be changed
SERVER_IP="" # IP of remote server
INPUT_PATH="" # Absolute Path to backup
EXCLUDE_PATTERN="" # Relitive path to INPUT_PATH to Exclude, can be left as ""
REMOTE_USER="" # User on remote server
2024-04-12 13:22:44 +01:00
REMOTE_PATH= # Absolute Path on the remote server to backup to. If path has a ' ' in it dont use "" or ''
2024-04-12 13:20:39 +01:00
# Sets Varibles for log files
DATE=$(date +%Y-%m-%d-%H-%M-%S)
LOG_FILE="/log/backup-home/${DATE}.log"
# Pings server IP to see if it is online
ping -c 1 "$SERVER_IP" > /dev/null 2>&1
# If online run backup
if [ $? -eq 0 ]; then
echo "$(date) Server $SERVER_IP is online" >> "$LOG_FILE"
# If there EXCLUDE_PATTERN is not null then set the rsync arguments with the pattern, else set without
if [ -n "$EXCLUDE_PATTERN" ]; then
RSYNC_ARGS="-avr --exclude=$EXCLUDE_PATTERN $INPUT_PATH"
else
RSYNC_ARGS="-arv $INPUT_PATH"
fi
# Run the rsync
rsync $RSYNC_ARGS $REMOTE_USER@$SERVER_IP:"$REMOTE_PATH" >> "$LOG_FILE" 2>&1
# Log whether or not it was sucessfull
if [ $? -eq 0 ]; then
echo "$(date) Backup successful" >> "$LOG_FILE"
else
echo "$(date) Backup failed" >> "$LOG_FILE"
fi
# If not online report in log
else
echo "$(date) Server $SERVER_IP is offline" >> "$LOG_FILE"
fi