Skip to content

Instantly share code, notes, and snippets.

@jeremysells
Last active September 2, 2018 19:36
Show Gist options
  • Save jeremysells/9cc6b6e77840f9bbabb1515edb1c0961 to your computer and use it in GitHub Desktop.
Save jeremysells/9cc6b6e77840f9bbabb1515edb1c0961 to your computer and use it in GitHub Desktop.
Dated Folder Backup
#!/usr/bin/env bash
set -e
# MIT licensed
# This script is AS-IS where is (no warrenty, liability etc - see the license for full)
# see: https://opensource.org/licenses/MIT and/or https://en.wikipedia.org/wiki/MIT_License
# Note: This script should just be used as an example. Use at your own risk!
# Configs - Settings
REMOTE_NAME="127.0.0.1"
BACKUP_ROOT_DIR=/some/path/${REMOTE_NAME}
REMOTE_BACKUP=home/user-name
#BACKUP_MAX_DAYS=10
# Configs - Fixed
DATE=`date '+%Y-%m-%d_%H-%M-%S'`
BACKUP_LAST_DIR=${BACKUP_ROOT_DIR}/latest
BACKUP_NEW_DIR=${BACKUP_ROOT_DIR}/${DATE}
# Make the new backup dir
echo "info: Making the dir: ${BACKUP_NEW_DIR}";
mkdir ${BACKUP_NEW_DIR}
# New backup is just a copy of the old one (then sync the differences)
echo "info: Copy of old lastest backup as new ${BACKUP_LAST_DIR}/filesystem ${BACKUP_NEW_DIR}/filesystem";
cp -rp ${BACKUP_LAST_DIR}/filesystem ${BACKUP_NEW_DIR}/filesystem
# Make sure the dir we are backing up into exists
echo "mkdir ${BACKUP_NEW_DIR}/filesystem/${REMOTE_BACKUP}"
mkdir -p ${BACKUP_NEW_DIR}/filesystem/${REMOTE_BACKUP}
# https://wiki.archlinux.org/index.php/full_system_backup_with_rsync
echo "info: rsync of root@${REMOTE_NAME}:/${REMOTE_BACKUP} to ${BACKUP_NEW_DIR}/filesystem/${REMOTE_BACKUP_FROM}";
rsync -azP --delete \
--exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found","lost+found"} \
root@${REMOTE_NAME}:/${REMOTE_BACKUP}/ \
${BACKUP_NEW_DIR}/filesystem/${REMOTE_BACKUP}/
# Set last backup
echo "info: setting the latest backup as the one we just did ${BACKUP_NEW_DIR} ${BACKUP_LAST_DIR}";
rm ${BACKUP_LAST_DIR}
ln -s ${BACKUP_NEW_DIR} ${BACKUP_LAST_DIR}
# Delete old backups
# Use at your own risk!
#echo "info: deleting old backups (Older then ${BACKUP_MAX_DAYS} days - from ${BACKUP_ROOT_DIR}"
#find ${BACKUP_ROOT_DIR} -maxdepth 1 -type d -ctime +${BACKUP_MAX_DAYS} -exec rm -rf {} \;
# Finished
echo "info: done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment