-
Notifications
You must be signed in to change notification settings - Fork 1
/
dailycron_backupdb
executable file
·158 lines (133 loc) · 5.98 KB
/
dailycron_backupdb
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python
# Program to make a backup of the mysql database
# two backups are made:
# 1. in the directory where its invoked
# 2. in the sd card if present
import sys,os,subprocess
import errno
import shutil
# removes files older than 24 hours
import os
import datetime
from ConfigParser import ConfigParser
def readConfig(installationPath):
config = ConfigParser()
#r = config.read("sample.ini")
print("installationPath is:" + installationPath)
#installationPath="/var/www/html/d3/"
configAbsPath = installationPath + "/" + "credentials.ini"
r = config.read(configAbsPath)
dictionary = {}
for section in config.sections():
dictionary[section] = {}
for option in config.options(section):
dictionary[section][option] = config.get(section, option)
#pprint(dictionary)
#print("sendto is something like:"+ dictionary["Reports"]['sendto'])
#sys.exit(1)
return dictionary
def remove_older_than_n_hours(search_dir, elapsed_hours=24):
#dir_to_search = os.path.curdir
dir_to_search = search_dir
for dirpath, dirnames, filenames in os.walk(dir_to_search):
for file in filenames:
curpath = os.path.join(dirpath, file)
file_modified = datetime.datetime.fromtimestamp(os.path.getmtime(curpath))
if datetime.datetime.now() - file_modified > datetime.timedelta(hours=elapsed_hours):
print "should remove " + curpath
os.remove(curpath)
else:
print "should not remove " + curpath
def file_get_contents(filename):
with open(filename, 'r') as f:
mnt_path = f.readline().rstrip('\n')
return mnt_path
def find_sdcard_path():
# usually, sd cards are found in paths like
# /media/rohit/9016-4EF8
# We wil be dumping to a path like:
# /media/rohit/9016-4EF8/.dump/
tmp_out = "/tmp/out"
cmd = "sudo mount|grep media| grep '-'|awk {'print $3'} > " + tmp_out
output = subprocess.check_output(cmd, shell=True)
sdcard_path = file_get_contents(tmp_out)
return sdcard_path
def silentremove(filename):
try:
os.remove(filename)
except OSError as e: # this would be "except OSError, e:" before Python 2.6
if e.errno != errno.ENOENT: # errno.ENOENT = no such file or directory
raise # re-raise exception if a different error occured
def silentmakedir(filename):
try:
os.makedirs(filename)
except OSError as e: # this would be "except OSError, e:" before Python 2.6
if e.errno != errno.ENOENT: # errno.ENOENT = no such file or directory
raise # re-raise exception if a different error occured
def create_dump_dir_if_needed(dumpDirPath):
res = os.path.isdir(dumpDirPath)
if res == False:
# sometimes it exists but is a file
silentremove(dumpDirPath)
silentmakedir(dumpDirPath)
def dump_mysql_and_prune_old_files(localDumpDir, isSdCardPresent, sdDumpdirFullPath, agedHours):
# to decrypt use:
# ccdecrypt -k keyfile .dump/mysqldump_04-08-20152221.sql.gz.cpt
# gunzip .dump/mysqldump_04-08-20152207.sql.gz
# to backup and restore use:
# backup: # mysqldump -u root -p[root_password] [database_name] > dumpfilename.sql
# restore:# mysql -u root -p[root_password] [database_name] < dumpfilename.sql
#output = subprocess.check_output("cat syscall_list.txt | grep f89e7000 | awk '{print $2}'", shell=True)
MYSQLDUMP=subprocess.check_output('which mysqldump', shell=True).rstrip('\n')
GZIP=subprocess.check_output('which gzip', shell=True).rstrip('\n')
CCRYPT=subprocess.check_output('which ccrypt', shell=True).rstrip('\n')
# get installation path
installationPath = ""
isLink = os.path.islink(sys.argv[0])
if isLink == True:
targetPath = os.readlink(sys.argv[0])
installationPath = os.path.dirname(targetPath)
else:
#print("self :"+ sys.argv[0] + " is not a link")
targetPath = (sys.argv[0])
installationPath = os.path.dirname(targetPath)
#DBNAME="test"
#DBNAME="visify"
#DBUSER="backup"
#DBPASSWD="XXXX"
#KEYFILE="/root/keyfile"
config = readConfig(installationPath)
DBNAME = config['Main']['db']
DBUSER = config['Backup']['backup_username']
DBPASSWD = config['Backup']['backup_password']
KEYFILE = config['Backup']['backup_keyfile']
TIMESTAMP=subprocess.check_output('date \"+%m-%d-%Y-%H%M\"', shell=True).rstrip('\n')
MYSQLDUMP_OPTIONS=" --single-transaction "
FULL_DUMP_PATH=localDumpDir + "/mysqldump_" +TIMESTAMP+".sql.gz.cpt"
cmd=MYSQLDUMP + MYSQLDUMP_OPTIONS + " -u " + DBUSER + " -p" + DBPASSWD + " " + DBNAME + "| " + GZIP + " | " + CCRYPT +" -k " + KEYFILE + " > "+ FULL_DUMP_PATH
print "cmd is[" +cmd+ "]"
#sys.exit(0)
output = False
output = subprocess.check_output(cmd, shell=True)
if isSdCardPresent == True:
# copy to the sd card if it exists
shutil.copy2(FULL_DUMP_PATH, sdDumpdirFullPath)
if output != False:
remove_older_than_n_hours(localDumpDir, agedHours)
if isSdCardPresent == True:
remove_older_than_n_hours(sdDumpdirFullPath, agedHours)
# TODO: Remember to change this to at least 96 hours
agedHours = 96
isSdCardPresent = False
dumpDirRelativePath = ".dump"
#localDumpDirPathAbs = "/root/" + dumpDirRelativePath
localDumpDirPathAbs = "./" + dumpDirRelativePath
base_path = find_sdcard_path()
sdDumpdirFullPath = base_path + "/" + dumpDirRelativePath
create_dump_dir_if_needed(dumpDirRelativePath)
if len(base_path) > 0:
isSdCardPresent = True
print("Checking for "+ sdDumpdirFullPath)
create_dump_dir_if_needed(sdDumpdirFullPath)
dump_mysql_and_prune_old_files(localDumpDirPathAbs, isSdCardPresent, sdDumpdirFullPath, agedHours)
#sys.exit(0)