Skip to content

Instantly share code, notes, and snippets.

@ayozehd
Created June 18, 2018 13:25
Show Gist options
  • Save ayozehd/e4e807ae2cfb729aa137485f57309d6c to your computer and use it in GitHub Desktop.
Save ayozehd/e4e807ae2cfb729aa137485f57309d6c to your computer and use it in GitHub Desktop.
Backup compressing your files from a directory. Useful to run from Cron.d
#!/bin/bash
DATE="$(date '+%d.%m.%Y')"
ALLPROJECTSDIR='/var/www/html' || $3; # Type you projects folder. It could be /home/user/public_html
PROJECT=$1 # Project's Name is needed as script argument!
PROJECTDIR="$ALLPROJECTSDIR/$PROJECT"
BACKUPDIR="$HOME/backup" || $2
PROJECTBACKUPDIR="$BACKUPDIR/$PROJECT"
CAPACITYLIMIT=95 # Setup your capacity limit
CURRENTCAPACITY=$(df -h $BACKUPDIR | tail -1 | awk '{print $5}' | cut -d '%' -f 1)
LOGFILE="$HOME/log/backup-files-$PROJECT.log"
[ ! -e $LOGFILE ] && touch $LOGFILE # Create the logfile if doesn't exist
echo "$DATE\n" >> $LOGFILE
if [ $CURRENTCAPACITY -lt $CAPACITYLIMIT ]
then
if [ -d "$PROJECTDIR" ]; then
[ ! -d "$BACKUPDIR" ] && mkdir $BACKUPDIR # Creates backup folder if doesn't exist
[ ! -d "$PROJECTBACKUPDIR" ] && mkdir $PROJECTBACKUPDIR # Creates a project folder inside backups dir
tar -zcf $PROJECTBACKUPDIR/$PROJECT-$DATE.tar.gz $PROJECTDIR
echo "Backup created in: $BACKUPDIR" >> $LOGFILE
fi
else
echo "There isn't enough free space in backup directory!!" >> $LOGFILE
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment