From 7272ce4fad4e36aeba1178ccaa3e9933a9498bb0 Mon Sep 17 00:00:00 2001 From: Shaun Chyxion Date: Mon, 26 Feb 2018 21:33:29 +0800 Subject: [PATCH] add backup restore script --- mysql-backup/backup.sh | 45 ++++++++++++++++++++++ mysql-backup/restore.sh | 41 ++++++++++++++++++++ web/app/templates/database-backup/list.hbs | 2 +- 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100755 mysql-backup/backup.sh create mode 100755 mysql-backup/restore.sh diff --git a/mysql-backup/backup.sh b/mysql-backup/backup.sh new file mode 100755 index 0000000..c0139cd --- /dev/null +++ b/mysql-backup/backup.sh @@ -0,0 +1,45 @@ +#!/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 diff --git a/mysql-backup/restore.sh b/mysql-backup/restore.sh new file mode 100755 index 0000000..d6d4e06 --- /dev/null +++ b/mysql-backup/restore.sh @@ -0,0 +1,41 @@ +#!/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" +database=ambition_crm + +backup_dir="$SERVICE_HOME/backup/$database" +restore_file="$backup_dir/${1}.sql.gz" +# exit when file not found +if [ ! -f "$restore_file" ]; then + echo "no restore file [$restore_file] found" + exit 1 +fi + +< "$restore_file" gzip -dc | mysql ambition_crm_test + +# back to previous dir +popd > /dev/null diff --git a/web/app/templates/database-backup/list.hbs b/web/app/templates/database-backup/list.hbs index 15b2ce3..5d4b233 100644 --- a/web/app/templates/database-backup/list.hbs +++ b/web/app/templates/database-backup/list.hbs @@ -27,7 +27,7 @@