-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup_database.py
95 lines (77 loc) · 3.22 KB
/
backup_database.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
#!/usr/bin/env python
"""
This script creates a timestamped database backup,
and cleans backups older than a set number of dates
"""
## based on code at http://codereview.stackexchange.com/questions/78643/create-sqlite-backups
import sqlite3
import shutil
import time
import os
import sys
DESCRIPTION = """
Create a timestamped SQLite database backup, and
clean backups older than a defined number of days
"""
# How old a file needs to be in order
# to be considered for being removed
NO_OF_DAYS = 7
FILE_TO_BACKUP = os.path.join(os.path.dirname(os.path.abspath(__file__)),'bikeandwalk.sqlite')
BACKUP_DIRECTORY = os.path.join(os.path.dirname(os.path.abspath(__file__)),'db_backups')
MINIMUM_BACKUPS = 3
def sqlite3_backup(dbfile=FILE_TO_BACKUP, backupdir=BACKUP_DIRECTORY):
"""Create timestamped database copy"""
if not os.path.isdir(backupdir):
print('Creating Backup Directory')
os.makedirs(backupdir)
if(os.path.isfile(dbfile)):
backup_file = os.path.join(backupdir, os.path.basename(dbfile) +
time.strftime("-%Y%m%d-%H%M%S"))
connection = sqlite3.connect(dbfile)
cursor = connection.cursor()
# Lock database before making a backup
cursor.execute('begin immediate')
# Make new backup file
shutil.copyfile(dbfile, backup_file)
print ("\nCreating {0}").format(os.path.basename(backup_file))
# Unlock database
connection.rollback()
else:
print(os.path.basename(dbfile) + ' Does not exist')
sys.exit(0)
def clean_data(dbfile=FILE_TO_BACKUP, backupdir=BACKUP_DIRECTORY):
"""Delete files older than NO_OF_DAYS days"""
print ("\n------------------------------")
print ("Cleaning up old backups")
# Always leave at lest the minimum number of backups
filelist = os.listdir(backupdir)
#Put them in order, newest First
filelist.sort(reverse=True)
# Only include files where the file name starts with FILE_TO_BACKUP name
fileCount = len(filelist)
for listElement in range(fileCount-1, -1,-1):
if os.path.basename(filelist[listElement])[:len(os.path.basename(dbfile))] != os.path.basename(dbfile)[:len(os.path.basename(dbfile))]:
del filelist[listElement]
#the filelist should only contain backup files
fileCount = len(filelist)
if fileCount > MINIMUM_BACKUPS:
for listElement in range(MINIMUM_BACKUPS, fileCount):
backup_file = os.path.join(backupdir, filelist[listElement])
if os.path.isfile(backup_file):
if os.stat(backup_file).st_mtime < (time.time() - (NO_OF_DAYS * 86400)):
os.remove(backup_file)
print "Deleting {0}".format(os.path.basename(backup_file))
else:
print('No Backup files to delete')
if __name__ == "__main__":
fileName = FILE_TO_BACKUP
dirName = BACKUP_DIRECTORY
if len(sys.argv) > 1:
if sys.argv[1]:
fileName = sys.argv[1]
if len(sys.argv) > 2:
if sys.argv[2]:
dirName = sys.argv[2]
sqlite3_backup(fileName, dirName)
clean_data(fileName, dirName)
print ("\nBackup update has been successful.")