Skip to content

Instantly share code, notes, and snippets.

@brandonsimpson
Last active April 24, 2018 03:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save brandonsimpson/b42fef6a6580f2d7e82b to your computer and use it in GitHub Desktop.
Save brandonsimpson/b42fef6a6580f2d7e82b to your computer and use it in GitHub Desktop.
Backup linux server via rsync to remote backup NAS drive
#!/bin/sh
# Backup linux server via rsync to remote backup NAS drive
#
# Run this from a root cronjob at whatever intervals you need
#
# example:
# # backup server at 4am
# 0 4 * * * sh /root/rsync_server_backup.sh > /dev/null;
#
#
# SERVER MUST HAVE ROOT SSH KEYS SHARED ON BACKUP SERVER FOR RSYNC TO WORK
#
# Created by Brandon Simpson
# Last updated: May 29, 2013
if (( EUID != 0 )); then
echo "You must be root to do this." 1>&2
exit 1
fi
START=$(date +%s)
TIMESTAMP=$(date '+%F-%H%M%S')
REMOTE_SERVER=“<remote server ip>
REMOTE_BACKUP_FOLDER="/volume1/Backup/linux_backups/$(hostname)"
REMOTE_USER="root"
# -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)
# -A, --acls preserve ACLs (implies -p)
# -X, --xattrs preserve extended attributes
# -H, --hard-links preserve hard links
# -z, --compress compress file data during the transfer
# -v, --verbose increase verbosity
# --delete-after receiver deletes after transfer, not before
# --delete-excluded also delete excluded files from dest dirs
# --dry-run perform a trial run with no changes made
# execute rsync command and exclude paths
rsync --archive --hard-links --human-readable --inplace --numeric-ids --delete --delete-excluded --verbose /* $REMOTE_USER@$REMOTE_SERVER:$REMOTE_BACKUP_FOLDER --exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found,/home/*/.gvfs,/var/spool/postfix/*,/var/log/*,/var/lib/php/session/*,/var/lib/pacman/sync/*,/var/cache/*,/var/run/portreserve/*}
FINISH=$(date +%s)
echo "total time: $(( ($FINISH-$START) / 60 )) minutes, $(( ($FINISH-$START) % 60 )) seconds"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment