问题:在宿主机通过docker exec 执行pgsql的容器内的备份脚本无法成功 (第一次提问,期待提携)
一、新建容器
[root@test script]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
31130e844f71 postgis/postgis:13-3.2 "docker-entrypoint.s…" 5 days ago Up 5 days 0.0.0.0:5432->5432/tcp, :::5432->5432/tcp pg
# 在本地写了一个pgsql的备份脚本.内容如下:
vi pgdump.sh
#!/bin/bash
pg_port=5432 # pg的端口
pg_user=postgres # pg用户
pg_dbname=test # pg数据库名字
[ -f test.tar ] && rm -f test.tar
pg_dump -U postgres -p 5432 -F t -b -v -c -x test -f ./test.tar > /dev/null 2>&1
二、将脚本拷贝到容器内/home目录下,以赋予文件777的权限
docker cp pgdump.sh pg:/home
[root@test script]# docker exec -it pg ls -l /home
total 4
-rwxrwxrwx 1 root root 266 Nov 16 10:23 pgdump.sh
三、执行脚本,没有结果返回
[root@test script]# docker exec -it pg /bin/bash /home/pgdump.sh
[root@test script]#
[root@test script]# docker exec -it pg ls -l /home
total 4
-rwxrwxrwx 1 root root 266 Nov 16 10:23 pgdump.sh
四、进入容器内执行,可成功执行
[root@test script]# docker exec -it pg bash
root@31130e844f71:/# cd /home/
root@31130e844f71:/home# bash pgdump.sh
root@31130e844f71:/home# ls -l
total 6532
-rwxrwxrwx 1 root root 266 Nov 16 10:23 pgdump.sh
-rw-r--r-- 1 root root 6683136 Nov 16 10:39 test.tar
总结:在容器内可以执行成功,在容器外无法执行
尝试的解决方法:
写了一个简单的脚本 test.sh 如下
#! /bin/bash
[ -f /home/test.txt ] && rm -f /home/test.txt
echo "hello world " >> /home/test.txt
同样按照上面的步骤操作,
[root@test script]# docker cp test.sh pg:/home
[root@test script]# docker exec -it pg ls -l /home
total 6536
-rwxrwxrwx 1 root root 266 Nov 16 10:23 pgdump.sh
-rw-r--r-- 1 root root 99 Nov 15 16:54 test.sh
-rw-r--r-- 1 root root 6683136 Nov 16 10:39 test.tar
[root@test script]# docker exec -it pg /bin/bash /home/test.sh
[root@test script]# docker exec -it pg ls -l /home
total 6540
-rwxrwxrwx 1 root root 266 Nov 16 10:23 pgdump.sh
-rw-r--r-- 1 root root 99 Nov 15 16:54 test.sh
-rw-r--r-- 1 root root 6683136 Nov 16 10:39 test.tar
-rw-r--r-- 1 root root 13 Nov 16 10:49 test.txt
发现是可以生成 test.txt 文件的,这样看来是感觉我的备份脚本有问题,但是我进去容器又可以成功执行。求帮助
脚本的最后一行>>/dev/null 2>&1 会导致任何输出都没有,可以去掉之后执行一下,看看有什么报错。
安装你的建议,删除了错误输出重定向,有正常输出,但是备份文件并没有重新生成。
[root@test script]# docker exec -it pg cat /home/pgdump.sh
#!/bin/bash
# pgsql的备份脚本
pg_port=5432 # pg的端口
pg_user=postgres # pg用户
pg_dbname=test # pg数据库名字
[ -f test.tar ] && rm -f test.tar
pg_dump -U postgres -p 5432 -F t -b -v -c -x test -f ./test.tar
[root@test script]# docker exec -it pg bash /home/pgdump.sh
pg_dump: last built-in OID is 16383
pg_dump: reading extensions
pg_dump: identifying extension members
pg_dump: reading schemas
pg_dump: reading user-defined tables
pg_dump: reading user-defined functions
pg_dump: reading user-defined types
pg_dump: reading procedural languages
pg_dump: reading user-defined aggregate functions
pg_dump: reading user-defined operators
pg_dump: reading user-defined access methods
pg_dump: reading user-defined operator classes
pg_dump: reading user-defined operator families
pg_dump: reading user-defined text search parsers
pg_dump: reading user-defined text search templates
pg_dump: reading user-defined text search dictionaries
pg_dump: reading user-defined text search configurations
pg_dump: reading user-defined foreign-data wrappers
pg_dump: reading user-defined foreign servers
pg_dump: reading default privileges
pg_dump: reading user-defined collations
pg_dump: reading user-defined conversions
pg_dump: reading type casts
pg_dump: reading transforms
pg_dump: reading table inheritance information
pg_dump: reading event triggers
pg_dump: finding extension tables
pg_dump: finding inheritance relationships
pg_dump: reading column info for interesting tables
pg_dump: finding the columns and types of table "public.area_code"
pg_dump: finding default expressions of table "public.area_code"
pg_dump: finding the columns and types of table "public.building"
pg_dump: finding default expressions of table "public.building"
pg_dump: finding the columns and types of table "public.heat"
pg_dump: finding default expressions of table "public.heat"
pg_dump: finding the columns and types of table "public.hour_flow"
pg_dump: finding default expressions of table "public.hour_flow"
pg_dump: finding the columns and types of table "public.passenger_flow"
pg_dump: finding default expressions of table "public.passenger_flow"
pg_dump: finding the columns and types of table "public.tag"
pg_dump: finding default expressions of table "public.tag"
pg_dump: finding the columns and types of table "public.test"
pg_dump: flagging inherited columns in subtables
pg_dump: reading indexes
pg_dump: reading indexes for table "public.area_code"
pg_dump: reading indexes for table "public.building"
pg_dump: reading indexes for table "public.heat"
pg_dump: reading indexes for table "public.hour_flow"
pg_dump: reading indexes for table "public.passenger_flow"
pg_dump: reading indexes for table "public.tag"
pg_dump: flagging indexes in partitioned tables
pg_dump: reading extended statistics
pg_dump: reading constraints
pg_dump: reading triggers
pg_dump: reading rewrite rules
pg_dump: reading policies
pg_dump: reading row-level security policies
pg_dump: reading publications
pg_dump: reading publication membership
pg_dump: reading subscriptions
pg_dump: reading large objects
pg_dump: reading dependency data
pg_dump: saving encoding = UTF8
pg_dump: saving standard_conforming_strings = on
pg_dump: saving search_path =
pg_dump: saving database definition
pg_dump: dumping contents of table "public.area_code"
pg_dump: dumping contents of table "public.building"
pg_dump: dumping contents of table "public.heat"
pg_dump: dumping contents of table "public.hour_flow"
pg_dump: dumping contents of table "public.passenger_flow"
pg_dump: dumping contents of table "public.tag"
pg_dump: dumping contents of table "public.test"
pg_dump: dropping DATABASE test
pg_dump: creating DATABASE "test"
pg_dump: connecting to new database "test"
pg_dump: creating SEQUENCE "public.area_code_id_seq"
pg_dump: creating TABLE "public.area_code"
pg_dump: creating COMMENT "public.TABLE area_code"
pg_dump: creating COMMENT "public.COLUMN area_code.id"
pg_dump: creating COMMENT "public.COLUMN area_code.code"
pg_dump: creating COMMENT "public.COLUMN area_code.name"
pg_dump: creating COMMENT "public.COLUMN area_code.deleted"
pg_dump: creating SEQUENCE "public.building_id_seq"
pg_dump: creating TABLE "public.building"
pg_dump: creating COMMENT "public.COLUMN building.id"
pg_dump: creating COMMENT "public.COLUMN building.town_code"
pg_dump: creating COMMENT "public.COLUMN building.code_name"
pg_dump: creating COMMENT "public.COLUMN building.type_name"
pg_dump: creating COMMENT "public.COLUMN building.tagkey_name"
pg_dump: creating COMMENT "public.COLUMN building.tag_name"
pg_dump: creating COMMENT "public.COLUMN building.tag_value"
pg_dump: creating COMMENT "public.COLUMN building.ratio"
pg_dump: creating COMMENT "public.COLUMN building.sort"
pg_dump: creating COMMENT "public.COLUMN building.create_time"
pg_dump: creating COMMENT "public.COLUMN building.tag_version"
pg_dump: creating SEQUENCE "public.heat_id_seq"
pg_dump: creating TABLE "public.heat"
pg_dump: creating COMMENT "public.COLUMN heat.id"
pg_dump: creating COMMENT "public.COLUMN heat."time""
pg_dump: creating COMMENT "public.COLUMN heat.heat_point"
pg_dump: creating COMMENT "public.COLUMN heat.create_time"
pg_dump: creating SEQUENCE "public.hour_flow_id_seq"
pg_dump: creating TABLE "public.hour_flow"
pg_dump: creating COMMENT "public.TABLE hour_flow"
pg_dump: creating COMMENT "public.COLUMN hour_flow.id"
pg_dump: creating COMMENT "public.COLUMN hour_flow.town_code"
pg_dump: creating COMMENT "public.COLUMN hour_flow.code_name"
pg_dump: creating COMMENT "public.COLUMN hour_flow.tag_name"
pg_dump: creating COMMENT "public.COLUMN hour_flow.hour"
pg_dump: creating COMMENT "public.COLUMN hour_flow.value"
pg_dump: creating COMMENT "public.COLUMN hour_flow.ratio"
pg_dump: creating COMMENT "public.COLUMN hour_flow.create_time"
pg_dump: creating COMMENT "public.COLUMN hour_flow.tag_version"
pg_dump: creating SEQUENCE "public.passenger_flow_id_seq"
pg_dump: creating TABLE "public.passenger_flow"
pg_dump: creating COMMENT "public.TABLE passenger_flow"
pg_dump: creating COMMENT "public.COLUMN passenger_flow.id"
pg_dump: creating COMMENT "public.COLUMN passenger_flow.town_code"
pg_dump: creating COMMENT "public.COLUMN passenger_flow.code_name"
pg_dump: creating COMMENT "public.COLUMN passenger_flow.type_name"
pg_dump: creating COMMENT "public.COLUMN passenger_flow.tagkey_name"
pg_dump: creating COMMENT "public.COLUMN passenger_flow.tag_name"
pg_dump: creating COMMENT "public.COLUMN passenger_flow.tag_value"
pg_dump: creating COMMENT "public.COLUMN passenger_flow.ratio"
pg_dump: creating COMMENT "public.COLUMN passenger_flow.sort"
pg_dump: creating COMMENT "public.COLUMN passenger_flow.create_time"
pg_dump: creating COMMENT "public.COLUMN passenger_flow.tag_version"
pg_dump: creating SEQUENCE "public.tag_id_seq"
pg_dump: creating TABLE "public.tag"
pg_dump: creating COMMENT "public.TABLE tag"
pg_dump: creating COMMENT "public.COLUMN tag.id"
pg_dump: creating COMMENT "public.COLUMN tag.tag_key"
pg_dump: creating COMMENT "public.COLUMN tag.tag_name"
pg_dump: creating COMMENT "public.COLUMN tag.deleted"
pg_dump: creating TABLE "public.test"
pg_dump: processing data for table "public.area_code"
pg_dump: processing data for table "public.building"
pg_dump: processing data for table "public.heat"
pg_dump: processing data for table "public.hour_flow"
pg_dump: processing data for table "public.passenger_flow"
pg_dump: processing data for table "public.tag"
pg_dump: processing data for table "public.test"
pg_dump: executing SEQUENCE SET area_code_id_seq
pg_dump: executing SEQUENCE SET building_id_seq
pg_dump: executing SEQUENCE SET heat_id_seq
pg_dump: executing SEQUENCE SET hour_flow_id_seq
pg_dump: executing SEQUENCE SET passenger_flow_id_seq
pg_dump: executing SEQUENCE SET tag_id_seq
pg_dump: creating CONSTRAINT "public.area_code area_code_pkey"
pg_dump: creating CONSTRAINT "public.building building_pkey"
pg_dump: creating CONSTRAINT "public.heat heat_pkey"
pg_dump: creating CONSTRAINT "public.hour_flow hour_flow_pkey"
pg_dump: creating CONSTRAINT "public.passenger_flow passenger_flow_pkey"
pg_dump: creating CONSTRAINT "public.tag tag_pkey"
[root@test script]#