Created
November 30, 2015 09:32
-
-
Save ssut/30d43e77c9305434ba36 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
NAME=$1 | |
FROM=$2 | |
PIDFILE=".$1.pid" | |
DIR=$(pwd) | |
PERIOD=${3:-7} | |
PRIORITY=${4:-10} | |
FILENAME="$(date +%Y-%m-%d_%H%M).tar.gz" | |
BACKUPDIR="$DIR/backups/$NAME" | |
echo "Backup Name: $1" | |
echo "Backup From: $2" | |
echo "Backup To: $BACKUPDIR" | |
echo " ... and where the backup file will be created: $BACKUPDIR/$FILENAME" | |
# check existing process | |
if [ -f "$PIDFILE" ] | |
then | |
read PID < $PIDFILE | |
if ps -p $PID > /dev/null | |
then | |
echo "backup process is already running.." | |
exit 1 | |
fi | |
fi | |
# store PID | |
echo $$ > $PIDFILE | |
# create backup template | |
mkdir -p $BACKUPDIR | |
# delete old backups | |
find $BACKUPDIR/ -name "*.tar.gz" -mtime "+$PERIOD" -exec rm {} \; | |
# backup now | |
# z: compress archive using gzip, c: create an archive, f: archive filename | |
nice -n $PRIORITY tar -zcvf "$BACKUPDIR/$FILENAME" $FROM | |
# delete PID file | |
rm $PIDFILE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment