#!/bin/bash ### BEGIN INIT INFO # Provides: Liuzhiting # Required-Start: $local_fs $remote_fs $network $syslog # Required-Stop: $local_fs $remote_fs $network $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: starts the mysql migrate # Description: A database migration script for a back-end server system. ### END INIT INFO function mysqlDump(){ echo 你输入的文件名称是:$3.txt echo $1 $2 $3 $4 $3.txt $(date +%Y%m%d) filePath="/mnt/sql/$(date +%Y%m%d)/" if [ ! -x "$filePath" ];then mkdir -p "$filePath" fi if [ "$2" == "all" ];then echo 你选择的是全部备份,正在备份迁移请等待... mysqldump -u root -pliuzhiting $1 > $filePath$3.txt filesize=$(stat -c "%s" $filePath$3.txt) if [ $filesize -gt 0 ];then fullPath=$filePath$3.txt echo "$fullPath $filesize 1024" | awk '{printf("备份文件全路径是:%s,文件大小:%0.5fkb\n", $1, $2/$3)}' echo 备份迁移完成... return 0 else echo 未知原因,无法备份迁移... return -1 fi elif [ "$2" == "table" ];then echo 你选择的备份的表格是:$4 mysqldump -u root -pliuzhiting $1 $4 > $filePath$3.txt filesize=$(stat -c "%s" $filePath$3.txt) if [ $filesize -gt 0 ];then # echo 备份文件全路径:$filePath$3.txt,文件大小:$[$filesize/1024]kb fullPath=$filePath$3.txt echo "$filesize 1024 $fullPath" | awk '{printf("备份文件全路径是:%s,文件大小:%0.5fkb\n", $3, $1/$2)}' echo 备份迁移完成... return 0 else echo 未知原因,无法备份迁移... return -1 fi else echo $2 echo $3 fi return -1 } function manageDumpData () { echo $1 $2 $3 $4 filePath="/mnt/sql/$(date +%Y%m%d)/" if [ ! -x "$filePath" ];then echo 正在创建$filePath文件夹 mkdir -p "$filePath" echo 创建完成... fi echo $filePath$3.json if [ ! -x "$1" ];then echo 项目文件路径不存在,"$1"... return -1 fi cd "$1" if [ "$2" == "all" ];then echo 你选择的是备份项目数据库全部表格 if [ ! -f "$1manage.py" ];then echo 项目路径下不存在"$1manage.py" return -1 fi python3 manage.py dumpdata > $filePath$3.json filesize=$(stat -c "%s" $filePath$3.json) if [ $filesize -gt 0 ];then fullPath=$filePath$3.json echo "$fullPath $filesize 1024" | awk '{printf("备份文件全路径是:%1,文件大小:%0.5fkb\n", $1, $2/$3}' echo 备份迁移完成... return 0 else echo 未知原因,无法备份迁移... return -1 fi elif [ "$2" == "app" ];then echo 你选择备份Appname名称是:$4 python3 manage.py dumpdata $4 > $filePath$3.json filesize=$(stat -c "%s" $filePath$3.json) if [ $filePath -gt 0 ];then fullPath=$filePath$3.json echo "$fullPath $filesize 1024" | awk '{printf("备份文件全路径是:%1,文件大小:%0.5fkb\n", $1, $2/$3}' echo 备份迁移完成... return 0 else echo 未知原因,无法备份迁移... return -1 fi else echo "YYYYY" return 0 fi return -1 } read -t 30 -p "请输入迁移方式(mysql自带方式[dump]或者Python方式[migrate],如果你选择的是mysql方式请再输入要备份的数据库名称[请按空格隔开,30秒完成输入]):" migrate dbname echo "$migrate" $dbname if [ ! $migrate ];then migrate="dump" dbname='Ansjer' fi if [ "$migrate" == "dump" ];then echo 你选择的是MySQL备份方式,并且你要备份的数据库是:$dbname read -t 30 -p "请先输入文件名称,然后选择备份所有数据[all] 或者备份其中一个表格[table],如果选择的备份其中一个表格请再输入表格名称[每个输入用表格隔开]" filename database tablename echo ddde, $database $filename $tablename filename=${filename:-dump} #当变量a为null或为空字符串时则var=b var=${a:-b} database=${database:-all} mysqlDump $dbname $database $filename $tablename echo $? elif [ "$migrate" == "migrate" ];then echo 你选择的是Python方式备份迁移数据库... projPath="/mnt/Sharing/Ansjer/" read -t 30 -p "(请先输入要备份的文件名称,然后是选择要备份appname或者备份全部,如果全部清输入[all],或者输入[app]再输入appnname):" filename database appname filename=${filename:-dump} database=${database:-all} manageDumpData $projPath $database $filename $appname echo $? else echo ay fi