how to tar in python
I want to backup my website using a python script. This is the script:
#!/usr/bin/python
import os
import time
import datetime
from subprocess import call
DATETIME = time.strftime('%Y%d%m-%H%M%S')
BACKUP_PATH = '/Backups/backups/Test/'
WEB_PATH = '/var/www/html'
TAR_CMD = "-zcf " + BACKUP_PATH + DATETIME + "-html.tar.gz " + WEB_PATH
print TAR_CMD
call (["tar",TAR_CMD])The output of TAR_CMD is
-zcf /Backups/backups/Test/20152111-123016-html.tar.gz /var/www/html
which is correct - calling tar plus this output tars my website - ok.
When running the python script the output is:
-zcf /Backups/backups/Test/20152111-123150-html.tar.gz /var/www/html
tar: Cowardly refusing to create an empty archive
Try 'tar --help' or 'tar --usage' for more information.
What am I doing wrong?
21 Answer
You need to pass the command components as elements of a list, so you can add tar to the TAR_CMD variable and then use split(' ') to create a list of command components separated on spaces:
TAR_CMD='tar .....'
subprocess.call(TAR_CMD.split(' '))Or directly:
subprocess.call('tar ....'.split(' ')) 1