Skip to content

Instantly share code, notes, and snippets.

@khangvm53
Last active November 21, 2017 09:16
Show Gist options
  • Save khangvm53/b5e3f8dc3c7fca32344eb586f042eed9 to your computer and use it in GitHub Desktop.
Save khangvm53/b5e3f8dc3c7fca32344eb586f042eed9 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/bash
# version 1.0
# usage: ./backup.sh domain.com
#Config
BACKUPCONFIG="backup-config.txt"; #domain sourcepath dbhost dbname dbuser dbpass
BACKUPPATH="/home/infact-m6/backup";
BACKUPSERVER="infact-m3.sakura.ne.jp";
BACKUPSERVER_USERNAME="infact-m3";
BACKUPSERVER_PATH="/home/infact-m3/backup/";
backupcodeanddatabase () {
local FS FT;
FT=`date +$2/$1-%Y-%m-%d-%H:%M.tar.bz2`;
FS=`date +$2/$1-%Y-%m-%d-%H:%M.sql`;
( cd $TD || {
echo "bad $TD" && return 2
};
)
if [ ! -z "$_U" ] ; then
mysqldump -u$_U -p$_P -h$_H $_N > $FS;
fi
tar -cvjSf $FT $TD ;
SSH_SCRIPT="cd $BACKUPSERVER_PATH; mkdir $1; uname -a";
ssh -n "$BACKUPSERVER_USERNAME@$BACKUPSERVER" "$SSH_SCRIPT";
scp -r $2 "$BACKUPSERVER_USERNAME@$BACKUPSERVER:$BACKUPSERVER_PATH/$1";
rm -rf $2;
}
dobackup () {
MONTH=$(date +"%Y-%m");
BACKUPDOMAIN="$BACKUPPATH/$DOMAIN";
if [ ! -d $BACKUPDOMAIN ]; then
mkdir $BACKUPDOMAIN
fi
BACKUP="$BACKUPDOMAIN/$MONTH";
if [ ! -d $BACKUP ]; then
mkdir $BACKUP
fi
DAY=$(date +"%d");
BACKUP="$BACKUPDOMAIN/$MONTH/$DAY";
if [ ! -d $BACKUP ]; then
mkdir $BACKUP
fi
if [ -d "$HOSTPATH" ]; then
backupcodeanddatabase $DOMAIN $BACKUP
echo $HOSTPATH;
else
echo "directory not found: $HOSTPATH/$DOMAIN/"
fi
}
while IFS='' read -r line ; do
I=0;
echo "$line";
if [ "$line" != "" ]; then
for text in $(echo $line | tr " " "\n")
do
if [ "$I" == 0 ]; then
DOMAIN="$text";
fi
if [ "$I" == 1 ]; then
HOSTPATH="$text";
TD="$text";
fi
if [ "$I" == 2 ]; then
_H="$text";
fi
if [ "$I" == 3 ]; then
_N="$text";
fi
if [ "$I" == 4 ]; then
_U="$text";
fi
if [ "$I" == 5 ]; then
_P="$text";
fi
I=$((I+1));
done
fi
dobackup
done < "$BACKUPCONFIG"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment