-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathremove_old_archives.py
125 lines (103 loc) · 4.47 KB
/
remove_old_archives.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python3
# encoding: utf-8
"""
Um pequeno utilitário complementar ao utilitário NPK-Backup, que serve para
automatizar a remoção de ficheiros de arquivo antigos na pasta especificada
(a utilizar numa máquina onde estejam alojada a pasta de arquivo sincronizada
com a Dropbox). Em cada execução, este programa apaga todas as pastas (e
respetivos conteúdos) cuja data seja mais recente do que o número de dias
indicado no ficheiro de configurações.
NOTA: Este script apaga de forma imediata e irreversível ficheiros e pastas,
sem qualquer aviso prévio ao utilizador, pelo que existe risco de perda de
dados. Recomenda-se por isso as devidas precauções e testes exaustivos antes de
decidir utilizá-lo num contexto real.
© 2017 Victor Domingos (http://victordomingos.com)
Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)
"""
import os
import datetime
import shutil
import logging
import logging.handlers
from send2trash import send2trash
from app_settings_remove_old import BACKUPS_FOLDER, BACKUP_TIMEOUT
from app_settings_remove_old import USE_TRASH, LOGS_PATH
logger = logging.getLogger()
def setup_logging():
log_path = os.path.expanduser(LOGS_PATH)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(levelname)s: %(message)s')
# Write log messages to a file
file = logging.FileHandler(log_path)
file.setLevel(logging.DEBUG)
file.setFormatter(formatter)
logger.addHandler(file)
# Show log messages also on screen
screen = logging.StreamHandler()
screen.setLevel(logging.DEBUG)
screen.setFormatter(formatter)
logger.addHandler(screen)
def delete_dir_by_date(main_path, year, month=None, day=None):
full_path = '{}/{}'.format(main_path, year)
if month is not None:
full_path = '{}/{}'.format(full_path, month)
if day is not None:
full_path = '{}/{}'.format(full_path, day)
if USE_TRASH:
msg = 'Sending directory to Trash: ' + full_path
logger.debug(msg)
send2trash(full_path)
else:
msg = 'Deleting directory: ' + full_path
logger.debug(msg)
shutil.rmtree(full_path, ignore_errors=True, onerror=None)
def main():
pasta_principal = os.path.expanduser(BACKUPS_FOLDER)
timeout = datetime.datetime.now() - datetime.timedelta(days=BACKUP_TIMEOUT)
ano, mes, dia = timeout.year, timeout.month, timeout.day
setup_logging()
for pasta_ano in os.listdir(pasta_principal):
if pasta_ano.startswith('.'):
continue
try:
if int(pasta_ano) < ano:
delete_dir_by_date(pasta_principal, pasta_ano)
continue
elif int(pasta_ano) > ano:
continue # passa para a pasta de ano seguinte
except Exception as e:
msg = 'An exception occurred while iterating through the folders.\n' + str(e)
logger.debug(msg)
logger.debug("Continuing with next folder...")
continue
# se pasta_ano == ano, então continua a processar o seu conteúdo...
pasta_ano_atual = pasta_principal + '/' + pasta_ano
for pasta_mes in os.listdir(pasta_ano_atual):
if pasta_mes.startswith('.'):
continue
try:
if int(pasta_mes) < mes:
delete_dir_by_date(pasta_principal, pasta_ano, pasta_mes)
continue
elif int(pasta_mes) > mes:
continue # passa para a pasta de mes seguinte
except Exception as e:
msg = 'An exception occurred while iterating through the folders.\n' + str(e)
logger.debug(msg)
logger.debug("Continuing with next folder...")
continue
# se pasta_mes == mes, então continua a processar o seu conteúdo...
pasta_mes_atual = pasta_principal + '/' + pasta_ano + '/' + pasta_mes
for pasta_dia in os.listdir(pasta_mes_atual):
if pasta_dia.startswith('.'):
continue
try:
if int(pasta_dia) < dia:
delete_dir_by_date(pasta_principal, pasta_ano, pasta_mes, pasta_dia)
except Exception as e:
msg = 'An exception occurred while iterating through the folders.\n' + str(e)
logger.debug(msg)
logger.debug("Continuing with next folder...")
continue
if __name__ == "__main__":
main()