Skip to content

Instantly share code, notes, and snippets.

@Pithikos
Created March 15, 2016 17:03
Show Gist options
  • Save Pithikos/24dbeffe700b6ecade2e to your computer and use it in GitHub Desktop.
Save Pithikos/24dbeffe700b6ecade2e to your computer and use it in GitHub Desktop.
Rsync backup of home directory
#!/bin/bash
# (expiration is in days)
BACKUP_SRC=/home/manos
BACKUP_DEST=/media/manos/Backup
BACKUP_PREFIX=backup-
BACKUP_EXPIRY=365
# Remove old backups
find ${BACKUP_DIR} -atime +${BACKUP_EXPIRY} -name "${BACKUP_PREFIX}*" -exec rm {} \;
# Create a new backup folder
TIMESTAMP=`date +%Y%m%d`
BACKUP_DIR="${BACKUP_DEST}/${BACKUP_PREFIX}${TIMESTAMP}"
mkdir "$BACKUP_DIR"
# If this is the very first backup (we use rsync instead of vanilla cp
# to be able and exclude some paths)
if [[ `ls -F | egrep "$BACKUP_PREFIX.*/" | wc -l` == '0' ]]; then
rsync -av "$BACKUP_SRC" "$BACKUP_DIR" \
--exclude='.gvfs' \
--exclude='.dbus' \
--exclude='.cache' \
--exclude='.viminfo'
else
# If we already have a backup
LAST_BACKUP=`ls -F | egrep "$BACKUP_PREFIX.*/" | sort | tail -n 1`
# Make a copy of an older backup
cp -al "$LAST_BACKUP" "$BACKUP_DIR"
# Sync any changes from the directory we actually want to backup
rsync -av --delete "$BACKUP_SRC" "$BACKUP_DIR"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment