patternpythonMinor
Small backup script in Python based on 7-Zip
Viewed 0 times
scriptzipsmallpythonbasedbackup
Problem
Is this reliable?
While there are many backup programs out there, it is still hard to find free software that allows unattended differential backup on Windows. Completely rolling something from scratch is usually not the way to go and after some searching I found that 7-Zip can be utilized to achieve full and differential backups that are highly compressed.
My goal was to create a small script that can run unattended to create full backups every 30 days and in the intervals in between create differential backups to safe space. While the script runs nicely in a few tests, I am far from being a "pythonista" and would appreciate any feedback on code quality and reliability.
The program requires 7zip.exe in the path or current directory and requires two command line parameters,
```
import os
import sys
import logging
import argparse
import subprocess
from datetime import datetime, date, timedelta
ARCHIVE_FILENAME_PATTERN = "py_backup__.7z"
FULL_ARCHIVE_PREFIX = "py_backup_full_"
DIFF_ARCHIVE_PREFIX = "py_backup_diff_"
MAX_DAYS_BETWEEN_FULL = 30
MAX_DAYS_FOR_DIFF = 30
logger = logging.getLogger(__name__)
def parse_date_in_filename(filename):
import re
date_in_name_pattern = '_[0-9]{4}-[0-9]{2}-[0-9]{2}'
match = re.search(date_in_name_pattern, filename)
if (match):
date_object = datetime.strptime(match.group(0), '_%Y-%m-%d')
return (True, date_object)
else:
# log that we could not match this filename
return (False, None)
def get_valid_dir(parser, arg):
if not os.path.isdir(arg):
parser.error("The directory %s could not be found!" % arg)
else:
return arg
def archive_ok(file_path):
"""Tests whether the archive can be sucessfully decompressed with 7zip."""
if (not os.path.isfile(file_path)):
file_path = file_path + '.7z'
test_command_string = u'7z.exe
While there are many backup programs out there, it is still hard to find free software that allows unattended differential backup on Windows. Completely rolling something from scratch is usually not the way to go and after some searching I found that 7-Zip can be utilized to achieve full and differential backups that are highly compressed.
My goal was to create a small script that can run unattended to create full backups every 30 days and in the intervals in between create differential backups to safe space. While the script runs nicely in a few tests, I am far from being a "pythonista" and would appreciate any feedback on code quality and reliability.
The program requires 7zip.exe in the path or current directory and requires two command line parameters,
--dest-dir destination_directory, and the directories to be backed up given by --input-dirs input_directory/ies.```
import os
import sys
import logging
import argparse
import subprocess
from datetime import datetime, date, timedelta
ARCHIVE_FILENAME_PATTERN = "py_backup__.7z"
FULL_ARCHIVE_PREFIX = "py_backup_full_"
DIFF_ARCHIVE_PREFIX = "py_backup_diff_"
MAX_DAYS_BETWEEN_FULL = 30
MAX_DAYS_FOR_DIFF = 30
logger = logging.getLogger(__name__)
def parse_date_in_filename(filename):
import re
date_in_name_pattern = '_[0-9]{4}-[0-9]{2}-[0-9]{2}'
match = re.search(date_in_name_pattern, filename)
if (match):
date_object = datetime.strptime(match.group(0), '_%Y-%m-%d')
return (True, date_object)
else:
# log that we could not match this filename
return (False, None)
def get_valid_dir(parser, arg):
if not os.path.isdir(arg):
parser.error("The directory %s could not be found!" % arg)
else:
return arg
def archive_ok(file_path):
"""Tests whether the archive can be sucessfully decompressed with 7zip."""
if (not os.path.isfile(file_path)):
file_path = file_path + '.7z'
test_command_string = u'7z.exe
Solution
Code Quality
Fix http://pep8online.com/s/YnHEYUA5
I also suggest you to read these documents:
Code reliability.
Code has no tests. How can it be reliable?
Use pytest.
What if '7z.exe' is not in the SYS PATH?
- PEP8/PyLint:
Fix http://pep8online.com/s/YnHEYUA5
I also suggest you to read these documents:
- Python PEP8
- Python docstrings
- Other improvements
- You don't need to check that "a list is None": you can just use
if not sys.argv. BTW, you never use argv.
- You don't need parentheses around operators in "if" statements. This is Python, not Java :)
- Remove imports from functions.
- All methods should be documented. Avoid comments that could be explained in the docstrings.
- Don't use "+" to concatenate strings. Use .format().
Code reliability.
Code has no tests. How can it be reliable?
Use pytest.
What if '7z.exe' is not in the SYS PATH?
Context
StackExchange Code Review Q#35147, answer score: 2
Revisions (0)
No revisions yet.