备份服务器数据至 git

之前有过一篇《将 VPS 备份到 Dropbox 的脚本》,现在研究了一下备份到 git 的脚本,去他喵的 Dropbox

#!/usr/bin/env bash

MYSQL_USR=""
MYSQL_PWD=""

WEB_DIR=/data/wwwroot
NGINX_CONF_DIR=/usr/local/tengine/conf
SCRIPT_DIR=/root/bin
LOCAL_BAK_DIR=/data/backup/to_git
COMMIT="Automatic Backup @ $(date)"

eval `ssh-agent -s`
ssh-add ~/.ssh/github

rm -rf $LOCAL_BAK_DIR
mkdir -p $LOCAL_BAK_DIR

git clone git@github.com:kokororin/backup.git $LOCAL_BAK_DIR

cd $LOCAL_BAK_DIR
git rm -rf .
git clean -fxd

cp -r $WEB_DIR $LOCAL_BAK_DIR
cp -r $NGINX_CONF_DIR $LOCAL_BAK_DIR/nginx_conf
cp -r $SCRIPT_DIR $LOCAL_BAK_DIR/scripts
crontab -l > $LOCAL_BAK_DIR/scripts/crontab

mkdir $LOCAL_BAK_DIR/db

for db in `/usr/local/mysql/bin/mysql -u$MYSQL_USR -p$MYSQL_PWD -B -N -e 'SHOW DATABASES' | xargs`; do
    (/usr/local/mysql/bin/mysqldump -u$MYSQL_USR -p$MYSQL_PWD ${db} >  $LOCAL_BAK_DIR/db/${db}.sql
    sed -i '$d' $LOCAL_BAK_DIR/db/${db}.sql)
done


cd $LOCAL_BAK_DIR

git add -A
git commit -m "$COMMIT"
git push origin master

rm -rf $LOCAL_BAK_DIR