Skip to content

Instantly share code, notes, and snippets.

@khangvm53
Created September 19, 2017 03:18
Show Gist options
  • Save khangvm53/1866047eca4ea9e387f096619858101f to your computer and use it in GitHub Desktop.
Save khangvm53/1866047eca4ea9e387f096619858101f to your computer and use it in GitHub Desktop.
#!/bin/bash
# usage: ./backup.sh domain.com
#Config
BACKUPPATH="/home/backup"; # store backups here
HOSTPATH="/home"; # path where hosted domains are
# set database settings.
DBUSER="root"
DBPASS="root"
DBHOST="localhost"
DBNAME="lol"
backupcodeanddatabase () {
local _N _U _P _H FS FT TD WP;
FT=`date +$3/$2-%Y-%m-%d.tar.gz`;
FS=`date +$3/$2-%Y-%m-%d.sql`;
TD=$1/$2;
WP=$TD/wp-config.php;
( cd $TD || {
echo "bad $TD" && return 2
};
if [ -f $WP ]; then
eval $(sed -n "s/^d[^D]*DB_\([NUPH]\)[ASO].*',[^']*'\([^']*\)'.*/_\1='\2'/p" $WP);
mysqldump -u$_U -p$_P -h$_H $_N > $FS;
else
_N=$DBNAME
_U=$DBUSER
_P=$DBPASS
_H=$DBHOST
mysqldump -u$_U -p$_P -h$_H $_N > $FS;
fi
tar -czPf $FT $TD )
}
dobackup () {
NOW=$(date +"%Y-%m-%d-%H:%M");
BACKUPDOMAIN="$BACKUPPATH/$DOMAIN";
if [ ! -d $BACKUPDOMAIN ]; then
mkdir $BACKUPDOMAIN
fi
BACKUP="$BACKUPDOMAIN/$NOW";
if [ ! -d $BACKUP ]; then
mkdir $BACKUP
fi
if [ -d "$HOSTPATH/$DOMAIN/" ]; then
backupcodeanddatabase $HOSTPATH $DOMAIN $BACKUP
else
echo "directory not found: $HOSTPATH/$DOMAIN/"
fi
}
if [ -z "$1" ]; then
I=0;
for DIR in $(find $HOSTPATH -maxdepth 1 -type d)
do
DOMAIN=$(basename $DIR)
if [ $I -ne 0 ]; then
#echo $DOMAIN
dobackup
fi
I=$((I+1))
done
exit 0
else
DOMAIN="$1";
dobackup
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment