Instantly share code, notes, and snippets.

@myourys myourys/vps-backup.sh
Last active Aug 29, 2015

Embed
What would you like to do?
#!/bin/bash
#=============================================================================
# FileName: vps-backup.sh
# Desc: vps backup script
# Author: Hector
# Email: myourys@gmail.com
# HomePage: http://www.yiwuye.com
# Version: 0.0.1
# LastChange: 2014-07-17 14:50:13
# History:
#=============================================================================
#====================
# date-.tar.gz
# date-dir
# - mysql/*.sql
# - folders/*
# - files/*
#====================
# config
BAK_DIR=/home/backup
MYSQL_USER=username #mysql user name
MYSQL_PASS=password #mysql password
#some settings
Today=$(date +"%Y%m%d") #today
Today_DIR=$BAK_DIR/$Today
function bak_mysql()
{
MYSQL_DIR=$Today_DIR/mysql
mkdir $MYSQL_DIR
for db in `/usr/local/mysql/bin/mysql -u$MYSQL_USER -p$MYSQL_PASS -B -N -e 'SHOW DATABASES' | xargs`; do
if [ ${db} != "mysql" ] && [ ${db} != "information_schema" ]; then
(/usr/local/mysql/bin/mysqldump -u$MYSQL_USER -p$MYSQL_PASS ${db} > $MYSQL_DIR/${db}.sql)
fi
done
}
function bak_dir()
{
folder_dir=$Today_DIR/folders
if [ ! -d $folder_dir ]; then
mkdir -p $folder_dir
fi
cp -f -r $1 $folder_dir/$2
}
function bak_file()
{
files_dir=$Today_DIR/files
if [ ! -d $files_dir ]; then
mkdir -p $files_dir
fi
cp -f -r $1 $files_dir/$2
}
#begin backup
pathCur=$(pwd)
mkdir -p $Today_DIR
bak_mysql
bak_dir "/home/wwwroot/hector" "web-hector" # web - hector
bak_dir "/usr/local/nginx/conf/vhost" "nginx-cofig" # NGINX config
bak_dir "/usr/local/php/etc" "php-etc" # php-etc
cd $BAK_DIR
tar zcf $Today.tar.gz $Today
rm -rf $Today_DIR
#remove 3 days ago backup
rm $BAK_DIR/$(date -d -3day +"%Y%m%d").tar.gz
python /home/bypy.py upload $BAK_DIR/$Today.tar.gz vps-backup/
cd $pathCur
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment