2018-02-26 21:33:29 +08:00

46 lines
1.0 KiB
Bash
Executable File

#!/bin/bash
# get real path of softlink
get_real_path() {
local f="$1"
while [ -h "$f" ]; do
ls=`ls -ld "$f"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
f="$link"
else
f=`dirname "$f"`/"$link"
fi
done
eval "$2"="'$f'"
}
get_real_path "$0" prg_path
echo "Script Path [$prg_path]"
# Service Home
pushd $(dirname "$prg_path") > /dev/null
SERVICE_HOME=$(dirname $(pwd))
popd > /dev/null
# enter service home
pushd "$SERVICE_HOME"
time=$(date '+%Y%m%d%H%M%S')
database='ambition_crm'
db_user='root'
db_password='696@2^~)oZ@^#*Q'
backup_dir="$SERVICE_HOME/backup/$database"
# create backup dir if not exists
[ ! -e "$backup_dir" ] && mkdir -p "$backup_dir"
mysqldump -u"$db_user" -p"$db_password" "$database" | gzip > "$backup_dir/${time}.sql.gz"
# remove 30 days ago backups
find "$backup_dir" -name "*.sql.gz" -type f -mtime +30 -exec rm -rf {} \; > /dev/null 2>&1
# back to previous dir
popd > /dev/null