Skip to content

Instantly share code, notes, and snippets.

@blantonious
Created January 15, 2022 19:17
Show Gist options
  • Save blantonious/7af9a81670a538d1149d0de0d383a005 to your computer and use it in GitHub Desktop.
Save blantonious/7af9a81670a538d1149d0de0d383a005 to your computer and use it in GitHub Desktop.
Backup Websites
#!/bin/bash
#----------------------------------------
# OPTIONS
#----------------------------------------
DAYS_TO_KEEP=7 # 0 to keep forever
WWW_PATH='/var/www/public'
BACKUP_PATH='/backups/www'
#----------------------------------------
# Create the backup folder
if [ ! -d $BACKUP_PATH ]; then
mkdir -p $BACKUP_PATH
fi
# change into the web root directory
cd "$WWW_PATH"
if [ "$(pwd)" != "$WWW_PATH" ] ; then
echo "Failed to change directory to root of web path"
exit
fi
for website in * ; do
if [[ -d $website && ! -L "$website" ]]; then
echo "Found website folder: $website"
date=$(date -I)
tar -cvpzf $BACKUP_PATH/$date-$website.tar.gz $website
fi
done
# Delete old backups
if [ "$DAYS_TO_KEEP" -gt 0 ] ; then
echo "Deleting backups older than $DAYS_TO_KEEP days"
find $BACKUP_PATH/* -mtime +$DAYS_TO_KEEP -exec rm {} \;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment