0

I use this script to make zipped backup of important folder but because after 5th backup files is moves to Recycle Bin and show to everyone I am looking for setpassword opinion to protect deleted zips or even better delete old zips but permanently (not move in Recycle Bin).

from datetime import datetime
from pathlib import Path
import zipfile


OBJECT_TO_BACKUP = '/home/etre/test/'  # The file or directory to backup
BACKUP_DIRECTORY = '/home/etre/test-backup/'  # The location to store the backups in
MAX_BACKUP_AMOUNT = 5  # The maximum amount of backups to have in BACKUP_DIRECTORY


object_to_backup_path = Path(OBJECT_TO_BACKUP)
backup_directory_path = Path(BACKUP_DIRECTORY)
assert object_to_backup_path.exists()  # Validate the object we are about to backup exists before we continue

# Validate the backup directory exists and create if required
backup_directory_path.mkdir(parents=True, exist_ok=True)

# Get the amount of past backup zips in the backup directory already
existing_backups = [
    x for x in backup_directory_path.iterdir()
    if x.is_file() and x.suffix == '.zip' and x.name.startswith('backup-')
]

# Enforce max backups and delete oldest if there will be too many after the new backup
oldest_to_newest_backup_by_name = list(sorted(existing_backups, key=lambda f: f.name))
while len(oldest_to_newest_backup_by_name) >= MAX_BACKUP_AMOUNT:  # >= because we will have another soon
    backup_to_delete = oldest_to_newest_backup_by_name.pop(0)
    backup_to_delete.unlink()

# Create zip file (for both file and folder options)
backup_file_name = f'backup-{datetime.now().strftime("%Y%m%d%H%M%S")}-{object_to_backup_path.name}.zip'
zip_file = zipfile.ZipFile(str(backup_directory_path / backup_file_name), mode='w')
if object_to_backup_path.is_file():
    # If the object to write is a file, write the file
    zip_file.write(
        object_to_backup_path.absolute(),
        arcname=object_to_backup_path.name,
        compress_type=zipfile.ZIP_DEFLATED
    )
elif object_to_backup_path.is_dir():
    # If the object to write is a directory, write all the files
    for file in object_to_backup_path.glob('**/*'):
        if file.is_file():
            zip_file.write(
                file.absolute(),
                arcname=str(file.relative_to(object_to_backup_path)),
                compress_type=zipfile.ZIP_DEFLATED
            )
# Close the created zip file
zip_file.close()

I tried this

`    zip_file.write(
        object_to_backup_path.absolute(),
        arcname=object_to_backup_path.name,
        compress_type=zipfile.ZIP_DEFLATED
        setpassword(b'1234')
`
lignjoslav
  • 21
  • 2
  • 1
    Sounds like you are looking for a way to securely wipe or [remove](https://stackoverflow.com/questions/17455300/python-securely-remove-file) a file. – jarmod Jan 23 '23 at 19:45
  • Do you have a link to the docs for `zipfile` module or method `setpassword` ? See also [similar question](https://stackoverflow.com/questions/17250/how-to-create-an-encrypted-zip-file) – hc_dev Jan 23 '23 at 19:47

1 Answers1

0

Official Python Zip File documentation is available here

the following code useful:

from zipfile import ZipFile
import zipfile

myzip = ZipFile('test.zip') 
myzip.setpassword(b"asasasasasas")
myzip.extract(member='Roughwork/pretify.html',pwd=b"asasasasasas") 

Syntax:

ZipFile.extract(member, file_path=None , pwd=None)

Parameters:

members: It specifies the name of files to be extracted.

file_path: location where archive file needs to be extracted, if file_path is None then contents of zip file will be extracted to the current working directory

pwd: the password used for encrypted files, By default pwd is None.

useful link..

A. Rokbi
  • 503
  • 2
  • 8