2

I am trying to write a python 3 script that will compress the contents of a directory /home/pi/results and copy it to another folder /home/pi/backups.
I need to be able to run this script multiple times and each time, the resulting archive is named something based on the previous archives. So first run would create backup001.tgz, second run would create backup002.tgz, etc. Each backup will be a complete backup containing anything within that directory.

I've figured out how to compress the folder to a .tgz, I just can't figure out how to append a number to it based on previous backups.

tar=tarfile.open(backupdir+'backup.tgz', "w:gz")
            tar.add(resultspath, arcname=os.path.basename(resultspath))
            tar.close()
Ulf Gjerdingen
  • 1,414
  • 3
  • 16
  • 20

4 Answers4

1
import os
latest_file = sorted(os.listdir('/home/pi/backups'))[-1]

This should give you the latest file name. The next one could be figured out from this name.

ham
  • 716
  • 5
  • 12
1

Use glob to get a list of backups that already exist
Use max() with custom key to find the last one
Use string formatting to create the next filename

import glob
filelist = glob.glob('Backup???.tgz')
last = max(filelist, key = lambda x: x[6:9])
filename = 'Backup{0:03d}.tgz'.format(int(last[6:9])+1)
print filename

(Outputs Backup003.tgz for a directory which contains Backup001.tgz & Backup002.tgz)

Community
  • 1
  • 1
SiHa
  • 7,830
  • 13
  • 34
  • 43
  • While this is technically exactly what I was asking for, the answer @turkus provided with the timecoded filenames worked better for me. I clicked the up vote icon for your answer, but apparently I gotta get a reputation of 15 before they show up. – Lightmaster Aug 23 '16 at 22:03
0

Maybe use timestamp? Then you will know at the first sight which specific backup you're looking for.

import datetime   

date = datetime.datetime.utcnow().isoformat()
filename = 'backup-{}.tgz'.format(date)
filepath = os.path.join(backupdir, filename)

tar=tarfile.open(filepath, "w:gz")
        tar.add(resultspath, arcname=os.path.basename(resultspath))
        tar.close()
turkus
  • 4,637
  • 2
  • 24
  • 28
  • While not strictly what I was asking, its a much better idea to use a timestamp instead. I changed it from utcnow() to now() to use my timezone. Only problem is that the time part contains colons, which tar apparently has issues with when in the file name. Just gotta figure out how to change the colons to underscores, which I'll do when I get off work tonight. – Lightmaster Aug 23 '16 at 20:05
  • ``date = datetime.datetime.utcnow().strftime("%b_%d_%Y_%H_%M_%S")`` – turkus Aug 23 '16 at 20:20
  • @Lightmaster check what kind of other variables you can put into strftime method. Anyway if it's better answer, you can approve this answer. – turkus Aug 23 '16 at 20:21
  • I changed the datetime codes to fit how I wanted them, but this was exactly what I needed. Thanks. – Lightmaster Aug 23 '16 at 21:57
  • @Lightmaster I'm really glad I could help you. Thanks. – turkus Aug 24 '16 at 05:01
0

You could try a filtering/counting approach:

# we set the path, and the word key that we are looking for
path = '/home/user/backups'
word_key = 'backup-'

# we count the occurrences and generate a new filename from it
backups_count = len(filter(lambda x : word_key in x, os.listdir(path)))
filename = 'backup-{}.tgz'.format(backups_count)
Luis González
  • 3,199
  • 26
  • 43