Skip to content

Instantly share code, notes, and snippets.

@eusonlito
Last active June 26, 2023 04:25
Show Gist options
  • Save eusonlito/409533b73a04f5937d5591fc861026cd to your computer and use it in GitHub Desktop.
Save eusonlito/409533b73a04f5937d5591fc861026cd to your computer and use it in GitHub Desktop.
Easy backup script into a local computer
#!/bin/bash
# -------------------------------------
# Script to do incremental rsync backups
# into a local computer
#
# This script is freely distributed under the GPL
# -------------------------------------
# -------------------------------------
# Configuration
#
# You only should touch this section
# -------------------------------------
echo -e "\n\nSTART: $(date "+%Y-%m-%d %H:%M:%S")"
DATE="$(date "+%Y-%m-%d")"
# Local directory to backup
STORE="/"
# Root directory to for backup stuff. Must exists
BASE="/backup"
# Organization directories
BASEBACKUP="$BASE/server"
BASELAST="$BASEBACKUP/last"
BASEHISTORY="$BASEBACKUP/history"
CURRENTHISTORY="$BASEHISTORY/$DATE"
# Days to store the incremental changes
INCREMENTALTIME=30
# -------------------------------------
# Script execution
# -------------------------------------
if [ ! -d "$BASEBACKUP" ]; then
install -d "$BASEBACKUP"
fi
if [ ! -d "$BASEBACKUP" ]; then
echo -e "\n\nBASE BACKUP FOLDER $BASEBACKUP COULD NOT BE CREATED"
fi
if [ ! -d "$CURRENTHISTORY" ]; then
install -d "$CURRENTHISTORY"
fi
rsync \
-av \
--force \
--delete \
--backup \
--ignore-errors \
--exclude /proc/ \
--exclude /dev/ \
--exclude /run/ \
--exclude /tmp/ \
--exclude /sys/ \
--exclude /media/ \
--exclude /mnt/ \
--exclude "$BASE/" \
--backup-dir="$CURRENTHISTORY/" \
"$STORE" "$BASELAST"
for OLD in $(ls -t "$BASEHISTORY" | tail -n +$INCREMENTALTIME); do
echo -e "\n\nDeleted history backup $BASEHISTORY/$OLD"
rm -rf "$BASEHISTORY/$OLD"
done
echo -e "\n\nEND: $(date "+%Y-%m-%d %H:%M:%S")"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment