0

I have this problem with bash in python. I want to make script that will make folder based on system date - and this works just fine. The problem is when I want to rsync two directories one with my files to backup and other that is created with system date in name.

    import os
    f = os.system("bash -c 'DATE=folder-`date +%y%m%d;mkdir /home/lol/$DATE;'")
    g = os.system("bash -c 'rsync -av /folderToBackup/ /home/lol/$DATE/;'")

And when rsync copies files they all land in /home/lol/ not in /home/lol/$DATE

please if some one knows the solution, i tried different directories and rsynced files always land not in created folder with date but higher, for example in /home/lol/

2

In the second line, $DATE isn't defined anymore as you start a new shell and it has lost the variable definition of the first call.

Why don't you wrap this into a single shell script or feed the date from Python? Why are you using Python anyway? Frankly, your approach is nonsensical.

$!/bin/bash
DATE=folder-`date +%y%m%d;mkdir /home/lol/$DATE
rsync -av /folderToBackup/ /home/lol/$DATE/
0

in your example you start a bash and you set the DATE variable so the DATE variable is only available in this bash process. when you start a new bash (for your rsync) you doesn't have set a DATE variable. When you read this variable it is empty. that's why you copy to /home/lol. so for your script just set the DATE variable in the second (rsync) bash process.

i think a better solution for your goal is to use python's datetime and makedirs. then start one os.system bash process for rsync.

2
  • Defining the date twice opens an interesting race condition if you execute this near midnight. – Sven Jun 3 '14 at 17:59
  • that's right. not thinking about that while explaining his mistake – user1008764 Jun 3 '14 at 18:04
0

The issue is the way you handle the variable $DATE. This variable is visible for the first execution (when you create the directory with the desired date). When the script tries to execute the rsync, the content of this $DATE variable is unknown to the second execution of os.system(). So you need in some way to communicate this value from the first execution into the second execution.

You can explore the Python module 'subprocess', which is also better than the os module to achieve this kind of results. Here my test:

from subprocess import PIPE
from subprocess import Popen


def exec_and_show(command):
    process = Popen(
        args=command,
        stdout=PIPE,
        shell=True
        )
    return process.communicate()[0]


my_folder = exec_and_show("bash -c 'DATE=folder-`date +%y%m%d`;mkdir /home/lol/$DATE;echo $DATE'")

print "My folder is: {0}".format(my_folder)

And definitely I would not use a programming language for a two-lines execution like this. Some other answer here which resolves the problem using shell script is better. Not only because the solution is simpler (only TWO lines of code), but also all what is implied in the simplicity (future maintenance, for example).

Not the answer you're looking for? Browse other questions tagged or ask your own question.