美文网首页
PG自动备份-关于ansible远程执行shell的环境变量问题

PG自动备份-关于ansible远程执行shell的环境变量问题

作者: 神力无敌_61b6 | 来源:发表于2018-11-21 19:55 被阅读0次

问题:pg_dump:command not found

最近在做数据库的自动备份功能,主体思路是设定好备份周期到celery中,定时调用ansible api调用playbook剧本,但是在ansible调用shell模块执行sh脚本的时候,遇到了pg_dump command not found。
相同的脚本在执行端服务器缺能正常运行。

原因:

command not found命令未找到,首先想到的就是环境变量的问题,网上查找了一番,果不其然正是环境变量的问题,根本原因是ansible登录方式导致的(login shell 和non-login shell)。

login shell 和 non-login shell

  • 我们在远程登录的时候,执行的是login shell,会加载载/etc/profile,~/.bash_profile;
  • 但ansible这类ssh远程执行是non-login shell,不会加载/etc/profile,/.bash_profile,而是加载/etc/bashrc和/.bashrc;

简单解释一下login shell 和 non-login shell

login shell:取得bash时需要完整的登录流程。就是说通过输入账号和密码登录系统,此时取得的shell称为login shell

non-login shell:取得sbash接口的方法不需要重复登录的举动。如以X Window登录登录linux后,再以X的图形界面启动终端机,此时那个终端机并没有需要输入账号和密 码,那个bash环境就是non-login shell。在原本的bash环境下再次执行bash命令,同样也没有输入账号密码就进入新的bash环境(前一个bash的子进程),新的bash也是non-login shell。

查看profile与bashrc的不同

查看profile,发现导入了PG的环境变量PGHOME,所以以login shell登录的时候pg_dump命令可以正常使用

[root@out-pg pg_dump]# cat /root/.bash_profile 
export PGHOME=/usr/local/postgresql
export PGDATA=/usr/local/postgresql/data
export PGLIB=/usr/local/postgresql/lib
export PATH=$PGHOME/bin:$PATH

而.bashrc却没有这些环境变量,pg_dump就会显示command not found

[root@out-pg pg_dump]# cat  /root/.bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

解决办法:

知道上面的原因再去解决就很容易了,我们可以添加.bash_profile相同的环境变量到.bashrc,保证ansible再以nologin shell执行脚本的时候具有PG相关的环境变量,此方法可以适用类似问题,如:java环境变量问题,大家看代码就一目了然了.

pg_dump.yml备份playbook:

task name:postgres variable configuration /root/.bashrc file

在剧本中,我们打印出.bashrc并注册到bashrc_result中,查找bashrc_result包含不包含PG_HOME,如果不包含,使用echo写入到.bashrc,再次执行问题得到解决

- name: pg_dump backup
  hosts: '{{ pg_host }}'
  tasks:
  - name: create backup main directory
    file:
      path: '{{ pg_home }}/pg_dump'
      owner: postgres
      group: postgres
      state: directory
      mode: 0755
  - name: Copy dump shell script
    copy:
      src: ssh/pg_dump.sh
      dest: '{{ pg_home }}/pg_dump/pg_dump.sh'
      owner: postgres
      group: postgres
      mode: 0755
      backup: yes
  - name: create backup subdirectories by date
    file:
      path: '{{ pg_home }}/pg_dump/{{ date }}'
      owner: postgres
      group: postgres
      state: directory
      mode: 0755
  - name: check postgres variable
    shell: cat /etc/profile
    register: bashrc_result
  - name: postgres variable configuration /root/.bashrc file
    shell: /bin/echo {{ item }} >> /root/.bashrc; source /root/.bashrc
    when: bashrc_result.stdout.find('PGHOME') == -1
    with_items:
      - export PGHOME={{ pg_home }}
      - export PGDATA={{ pg_home }}/data
      - export PGLIB={{ pg_home }}/lib
      - export PATH=$PGHOME/bin:$PATH
  - name: Execute shell begin backup
    shell:   sh pg_dump.sh  {{ pg_home }}  {{pg_user}} {{pg_instance}}   '{{table_list}}' {{ date }}  {{backup_server}} {{backup_dest}} {{ backup_ssh_port }}
    args:
      executable: /bin/bash
      chdir: '{{ pg_home }}/pg_dump/'

添加后/etc/bashrc:

[root@out-pg pg_dump]# cat /root/.bashrc
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
export PGHOME=/usr/local/postgresql
export PGDATA=/usr/local/postgresql/data
export PGLIB=/usr/local/postgresql/lib
export PATH=$PGHOME/bin:$PATH

附:

pg_dump.sh备份脚本

#!/bin/bash
pg_home=$1
pg_user=$2
pg_instance=$3
table_list=$4
date=$5
server=$6
dest=$7
port=$8

pg_dump -F c  ${table_list} -U  ${pg_user}  ${pg_instance} > ${pg_home}/pg_dump/${date}/${date}.dmp

相关文章

网友评论

      本文标题:PG自动备份-关于ansible远程执行shell的环境变量问题

      本文链接:https://www.haomeiwen.com/subject/sohhqqtx.html