簡體   English   中英

如何在Windows中使用Cygwin運行.sh

[英]How to run .sh using Cygwin in windows

我有一個名為backup.sh的文件,我也已經安裝並設置了路徑變量。 但是當我雙擊backup.sh文件時,它說了一些我不知道的東西。 場景返回了使用grails中的腳本進行mongo db數據庫備份。

    #!/bin/bash
#
# Michael Mottola
# <mikemottola@gmail.com>
# December 18, 2011
# 
# Creates backup files (bson) of all MongoDb databases on a given server.
# Default behaviour dumps the mongo database and tars the output into a file
# named after the current date. ex: 2011-12-19.tar.gz
#

### Set server settings
HOST="localhost"
PORT="27017" # default mongoDb port is 27017
USERNAME=""
PASSWORD=""

# Set where database backups will be stored
# keyword DATE gets replaced by the current date, you can use it in either path below
BACKUP_PATH="Desktop/path/to/backup/directory" # do not include trailing slash
FILE_NAME="DATE" #defaults to [currentdate].tar.gz ex: 2011-12-19.tar.gz


##################################################################################
# Should not have to edit below this line unless you require special functionality
# or wish to make improvements to the script
##################################################################################

# Auto detect unix bin paths, enter these manually if script fails to auto detect
MONGO_DUMP_BIN_PATH="$(which mongodump)"
TAR_BIN_PATH="$(which tar)"

# Get todays date to use in filename of backup output
TODAYS_DATE=`date "+%Y-%m-%d"`

# replace DATE with todays date in the backup path
BACKUP_PATH="${BACKUP_PATH//DATE/$TODAYS_DATE}"

# Create BACKUP_PATH directory if it does not exist
[ ! -d $BACKUP_PATH ] && mkdir -p $BACKUP_PATH || :

# Ensure directory exists before dumping to it
if [ -d "$BACKUP_PATH" ]; then

    cd $BACKUP_PATH

    # initialize temp backup directory
    TMP_BACKUP_DIR="mongodb-$TODAYS_DATE"

    echo; echo "=> Backing up Mongo Server: $HOST:$PORT"; echo -n '   ';

    # run dump on mongoDB
    if [ "$USERNAME" != "" -a "$PASSWORD" != "" ]; then 
        $MONGO_DUMP_BIN_PATH --host $HOST:$PORT -u $USERNAME -p $PASSWORD --out $TMP_BACKUP_DIR >> /dev/null
    else 
        $MONGO_DUMP_BIN_PATH --host $HOST:$PORT --out $TMP_BACKUP_DIR >> /dev/null
    fi

    # check to see if mongoDb was dumped correctly
    if [ -d "$TMP_BACKUP_DIR" ]; then

        # if file name is set to nothing then make it todays date
        if [ "$FILE_NAME" == "" ]; then
            FILE_NAME="$TODAYS_DATE"
        fi

        # replace DATE with todays date in the filename
        FILE_NAME="${FILE_NAME//DATE/$TODAYS_DATE}"

        # turn dumped files into a single tar file
        $TAR_BIN_PATH --remove-files -czf $FILE_NAME.tar.gz $TMP_BACKUP_DIR >> /dev/null

        # verify that the file was created
        if [ -f "$FILE_NAME.tar.gz" ]; then
            echo "=> Success: `du -sh $FILE_NAME.tar.gz`"; echo;

            # forcely remove if files still exist and tar was made successfully
            # this is done because the --remove-files flag on tar does not always work
            if [ -d "$BACKUP_PATH/$TMP_BACKUP_DIR" ]; then
                rm -rf "$BACKUP_PATH/$TMP_BACKUP_DIR"
            fi
        else
             echo "!!!=> Failed to create backup file: $BACKUP_PATH/$FILE_NAME.tar.gz"; echo;
        fi
    else 
        echo; echo "!!!=> Failed to backup mongoDB"; echo;  
    fi
else

    echo "!!!=> Failed to create backup path: $BACKUP_PATH"

fi

找不到它的--host命令。 更新以獲取以后的幫助。

您可以將其運行為:

bash backup.sh

要么

chmod +x backup.sh
./backup.sh

bash FilePath / backup.sh

例如,如果您在E:/ SohamShetty / Files中有backup.sh文件,那么您應該這樣做。

bash E:/SohamShetty/Files/backup.sh

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM