Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Lo All, Firstly, most will probably see this as a duplicate post, but it has a unique element: tar... Ive posted this code a few times now, and many have assisted. However, Ive modified the code to add the date to the filename. Unfortunately, the output just does not change... And the code is:

import time
import datetime
import os
import json
import tarfile
source_config='/myapp/brendan.json'
backup_dir = '/myapp/target/'
DATE_FORMAT = '%Y%m%d'
now = time.strftime(DATE_FORMAT) 
timestr_now = time.strftime('%Y%m%d')

def main():
    print timestr_now
    with open(source_config) as f:
                data = json.load(f)
                for entry in data["source_include"]:
                    base_filename = os.path.basename(entry)
                    #source_dirs = [ name for name in os.listdir(entry) if os.path.isdir(os.path.join(entry, name)) ]
                    full_dir = os.path.join(entry)
                    testname = (timestr_now, backup_dir, base_filename)
                    date_name = str(timestr_now + "_" + base_filename)
                    tar = tarfile.open(os.path.join(backup_dir, date_name + '.tar.gzip'), 'w:gz')
                    tar.add(full_dir)
                    tar.close()

and the JSON:

{
    "source_type": "folder",
    "tar_type": "gzip",
    "tar_max_age": "10",
    "source_include": ["/myapp/conf/", 
                        "/myapp/db/", 
                        "/myapp/randomdata/"],
    "target_path": "/myapp/target/",
    "del_age": "10"
}

Please note that the additional options in the JSON WILL be used at a later date.

So, when I run this code, it returns the date: 20131206 (in the console), which is expected as a result of the print timestr line. However, the resultant tars are named conf.tar.gzip and db.tar.gzip... instead of the expected 20131206conf.tar.gzip... Any idea what the hell Im doing wrong here?

share|improve this question
1  
okay, I seem to hav solved the issue. I added a line, date_name = str(timestr_now + "_" + base_filename) and then changed tar = tarfile.open(os.path.join(backup_dir, date_name = 'tar.gzip'), 'w:gz') and its sorted!! Ive posted the edited code for those who need it to work in future!! –  Brendan 11 mins ago
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.