触发脚本运行许可后/之前无业游民

时间:2019-03-27 10:25:22

标签: vagrant vagrantfile

我对Vagrant很陌生,但是基本了解了。 不过,将Vagrant更新到版本2.2.4后仍然存在问题。 我确实运行了基本的shell脚本来安装sql db和必要的配置,然后在一切准备就绪后创建数据库并填充数据后,我需要运行另一个脚本。同样,当环境被破坏时,将调用另一个触发器来转储数据库以供将来上传。

Vagrantfile相关部分:

Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu/trusty64"
  config.ssh.insert_key = true

  config.vm.define "db" do |db|
    db.vm.hostname = "local-db-dev"
    db.vm.network "private_network", ip: "192.168.100.20"
    db.vm.provider "virtualbox" do |vb|
      vb.customize ["modifyvm", :id, "--cpus", "1" ]
      vb.customize ["modifyvm", :id, "--memory", "512"]
      vb.customize ["modifyvm", :id, "--name", "DB dev"]
    end
    db.vm.synced_folder "./dbBackup", "/srv/dbBackup", owner: "vagrant", group: "vagrant", type: "nfs"
    db.vm.provision :shell, path: "bootstrap-db.sh"

    db.trigger.after :up do |trigger|
      trigger.info = "DB up procedure ..."
      trigger.run = {path: "./dbBackup/bootstrap-db_up.sh"}
    end

    db.trigger.before :destroy, :halt, :suspend do |trigger|
      trigger.info = "DB down procedure ..."
      trigger.run = {path: "./dbBackup/bootstrap-db_down.sh"}
    end
  end
end

bootstrap-db_down.sh:

#!/usr/bin/env bash
export MYSQL_PWD="pass"

USER="user"
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
BACKUP_DIR="/srv/dbBackup/$TIMESTAMP"
mkdir -p "$BACKUP_DIR"

databases=`mysql -u $USER -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`

for db in $databases; do
    if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "sys" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]] ; then
        echo "Dumping database: $db"
        mysqldump -u $USER --databases $db > "$BACKUP_DIR/backupdb.$db.sql"
    fi
done

一切正常,直到调用触发器并执行脚本为止。例如,销毁触发器之前有第一个命令“ mkdir”。执行响应是拒绝权限。将其更改为“ sudo mkdir”,但响应是sudo未知。

在这种情况下,我显然没有足够的经验来理解权限。


编辑:有些进步

感谢Matt Schuchard的评论,他检查了实际在目录方面发生了什么,并发现我无法以这种方式访问​​目录。触发脚本中的更改通过允许我创建文件夹来改善了这种情况,但是现在我收到以下异常消息:“ bash:mysql:未找到命令”。认为我不完全了解脚本的触发方式和触发位置。

编辑的bootstrap-db_down.sh:

#!/usr/bin/env bash
export MYSQL_PWD="rooter"
USER="root"
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
BACKUP_DIR="./dbBackup/$TIMESTAMP"
mkdir "$BACKUP_DIR"

databases=`mysql -u $USER -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`

for db in $databases; do
    if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "sys" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]] ; then
        echo "Dumping database: $db"
        mysqldump -u $USER --databases $db > "$BACKUP_DIR/backupdb.$db.sql"
    fi
done

编辑:最终解决方案

终于明白了。问题又出在椅子和键盘之间。

我只是不了解trigger.run和trigger.run_remote之间的区别,所以我尝试在本地运行脚本,但希望会影响远程运行。我将添加相关的代码示例,以防有人偶然发现类似问题。

Vagrantfile相关部分:

Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu/trusty64"
  config.ssh.insert_key = true

  config.vm.define "db" do |db|
    db.vm.hostname = "local-db-dev"
    db.vm.network "private_network", ip: "192.168.100.20"
    db.vm.provider "virtualbox" do |vb|
      vb.customize ["modifyvm", :id, "--cpus", "1" ]
      vb.customize ["modifyvm", :id, "--memory", "512"]
      vb.customize ["modifyvm", :id, "--name", "DB dev"]
    end
    db.vm.synced_folder "./dbBackup", "/srv/dbBackup", owner: "vagrant", group: "vagrant", type: "nfs"
    db.vm.provision :shell, path: "bootstrap-db.sh"

    db.trigger.after :up do |trigger|
      trigger.info = "DB up procedure ..."
      trigger.run_remote = {inline: "/srv/dbBackup/bootstrap-db_up.sh"}
    end

    db.trigger.before :destroy, :halt, :suspend do |trigger|
      trigger.info = "DB down procedure ..."
      trigger.run_remote = {inline: "/srv/dbBackup/bootstrap-db_down.sh"}
    end
  end
end

bootstrap-db_down.sh:

#!/usr/bin/env bash
export MYSQL_PWD="pass"

USER="user"
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
BACKUP_DIR="/srv/dbBackup/$TIMESTAMP"
mkdir -p "$BACKUP_DIR"

databases=`mysql -u $USER -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`

for db in $databases; do
    if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "sys" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]] ; then
        echo "Dumping database: $db"
        mysqldump -u $USER --databases $db > "$BACKUP_DIR/backupdb.$db.sql"
    fi
done

0 个答案:

没有答案