[go: up one dir, main page]

Menu

[599fc9]: / sqlutil / sqlbackup.py  Maximize  Restore  History

Download this file

77 lines (65 with data), 2.8 kB

 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
import os, subprocess, gzip, optparse
class mysqldump (subprocess.Popen):
def __init__( self, db, outfile=None, zip=True, user=None, pswd=None ):
cmd = ['mysqldump']
if user != None: cmd.append( '-u '+user )
if pswd != None: cmd.append( '-p '+pswd )
cmd += [ db ]
if zip: of = gzip.GzipFile( outfile, 'wb' )
else: of = open( outfile, 'w' )
super( mysqldump, self ).__init__( cmd, shell=True, stdout=of )
class mysql (subprocess.Popen):
def __init__( self, db, infile, zip=True, user=None, pswd=None ):
cmd = ['mysql']
if user != None: cmd.append( '-u '+user )
if pswd != None: cmd.append( '-p '+pswd )
cmd += [ db ]
if zip: iff = gzip.GzipFile( infile )
else: iff = open( infile, 'r' )
super( mysql, self ).__init__( cmd, shell=True, stdin=iff )
class mount (subprocess.Popen):
def __init__( self, path, mnt=True ):
cmd = list()
cmd.append( ('umount','mount')[mnt] )
cmd.append( path )
print cmd
super( mount, self ).__init__( cmd, shell=True )
class sqlbackup_prg (optparse.OptionParser):
def __init__( self ):
mp = os.path.join( '', os.path.join( 'mnt', 'flash' ) )
sp = os.path.join( 'software', '25' )
optparse.OptionParser.__init__( self )
self.add_option( '-l', '--load', action='store_true', dest='load',
help='ricarica un database' )
self.add_option( '-z', '--zip', action='store_true', dest='zip',
default=True,
help='utilizza la compressione gzip' )
self.add_option( '-s', '--save', action='store_true', dest='save',
help='salva un database' )
self.add_option( '-m', '--mount', action='store', dest='mpath',
default=mp,
help='directory da montare prima di leggere/scrivere i file' )
self.add_option( '-d', '--directory', action='store', dest='path',
default=os.path.join( mp, sp ),
help='directory dove leggere/scrivere i file' )
self.add_option( '-u', '--user', action='store', dest='user',
help='utente con cui accedere al database' )
self.add_option( '-p', '--password', action='store', dest='pswd',
help='password con cui accedere al database' )
def run( self ):
options, args = self.parse_args()
if not( options.save or options.load ): return 0
if options.mpath != None: e = mount( options.mpath, True )
print e
if e != 0: return e
if options.save:
for dbn in args:
f = os.path.join( options.path, '.'.join( [dbn,'gz'] ) )
mysqldump( dbn, f, options.zip, options.user, options.pswd )
elif options.load:
for dbn in args:
f = os.path.join( options.path, '.'.join( [dbn,'gz'] ) )
mysql( dbn, f, options.zip, options.user, options.pswd )
if options.mpath != None: mount( options.mpath, False )
if __name__ == '__main__':
sqlbackup_prg().run()