rsync filters failing when executed with python subprocess.run()

时间:2018-02-01 18:08:20

标签: python-3.x subprocess rsync

I have a slightly complicated rsync command that needs to be executed at regular intervals by a python program.

enter code here

The command executes correctly from the command line, but when run with subprocess.run(), the include/exclude filters seem to be ignored.

The command is meant to sync certain data files between a pair of Raspberry Pi3's over ssh. The Pi's are running Python 3.5.2 and rsync 3.11 under Raspbian jessie.

Here's my test code executed from an ipython session.

In [3]: def testrsync():
   ...:     from subpro`enter code here`cess import run
   ...:     cmd = ("rsync -avzhe ssh -i --include='*.zip' --include='data.csv' "
   ...:            "--exclude='*' pi@192.168.1.122:/home/pi/pgcdata/ "
   ...:            "/tmp/pgcdata")
   ...:     cmdlist = cmd.split()
   ...:     print(cmd)
   ...:     ret  = run(cmdlist, shell=False)
   ...:     print(ret)
   ...:
   ...:

In [4]: testrsync()
rsync -avzhe ssh -i --include='*.zip' --include='data.csv' --exclude='*' pi@192.168.1.122:/home/pi/pgcdata/ /tmp/pgcdata
receiving incremental file list
created directory /tmp/pgcdata
cd+++++++++ ./
>f+++++++++ data-2018-01-24.csv.zip
>f+++++++++ data-2018-01-25.csv.zip
>f+++++++++ data-2018-01-26.csv.zip
>f+++++++++ data-2018-01-27.csv.zip
>f+++++++++ data-2018-01-28.csv.zip
>f+++++++++ data-2018-01-29.csv.zip
>f+++++++++ data-2018-01-30.csv.zip
>f+++++++++ data-2018-01-31.csv
>f+++++++++ data.csv
>f+++++++++ four-hour-plot.csv
>f+++++++++ one-day-plot.csv
>f+++++++++ seven-day-plot.csv
>f+++++++++ thirty-day-plot.csv

sent 312 bytes  received 731.54K bytes  162.63K bytes/sec
total size is 35.49M  speedup is 48.49
CompletedProcess(args=['rsync', '-avzhe', 'ssh', '-i', "--include='*.zip'", "--include='data.csv'", "--exclude='*'", 'pi@192.168.1.122:/home/pi/pgcdata/', '/tmp/pgcdata'], returncode=0)

The last four files above should NOT have been copied. If I empty the target directory, copy the printed rsync command and run it as a shell command, the filtering occurs and only the desired files are copied.

In [5]: !rm -rf /tmp/pgcdata/

In [6]: !rsync -avzhe ssh -i --include='*.zip' --include='data.csv' --exclude='*' pi@192.168.1.122:/home/pi/pgcdata
   ...: / /tmp/pgcdata
receiving incremental file list
created directory /tmp/pgcdata
cd+++++++++ ./
>f+++++++++ data-2018-01-24.csv.zip
>f+++++++++ data-2018-01-25.csv.zip
>f+++++++++ data-2018-01-26.csv.zip
>f+++++++++ data-2018-01-27.csv.zip
>f+++++++++ data-2018-01-28.csv.zip
>f+++++++++ data-2018-01-29.csv.zip
>f+++++++++ data-2018-01-30.csv.zip
>f+++++++++ data.csv

1 个答案:

答案 0 :(得分:0)

问题是过滤器模式周围的单引号。他们必须防止shell扩展模式,并且在rsync看到shell之前,shell会删除引号。

带有Subprocess.run的{​​p> shell=False并不会删除引号和rsync,保佑它的小心脏,不会抱怨。因此,模式最终没有效果,一切都被复制。

当我将命令字符串更改为:

时,问题得以解决
cmd = ("rsync -avzhe ssh -i --include=*.zip --include=data.csv "
       "--exclude=* pi@192.168.1.122:/home/pi/pgcdata/ "
       "/tmp/pgcdata")