Support Forums

Full Version: Flexible Backup Script
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So yeah, if anyone knows me; you know I screw stuff up.... a lot. Considering this, I felt it necessary to make some sort of script to backup all my important configuration files to help counter act my stupidity (Its working quite well).

It will basically take a dictionary of glob patterns to search, and what sub directory the files should be put under (GlobPatterns)

for example, Anope services has many .db files; so by doing

"/anope/*.db" : "Anope_Configs"

the script will search for all files that match the pattern *.db in the folder /anope/ and copy them to a sub directory, in this case Anope_Configs (Plus the date), in your specified Main Backup folder (All folders will be created if they do not already exist).

Say /anope/ holds 4 files

a.db
b.db
readme.txt
Anope.txt

the script will grab both a.db and b.db, it will create the folder "/home/Backups" then create the folder "Anope_Configs:11:24:2009:16" in "/home/Backups" and copy the files over into "Anope_Configs:11:24:2009:16". It will then repeat for any other key/value pairs in the dictionary

I know I butchered the explanation but it is really worth checking out

Code:
#!/usr/bin/env python

import datetime, glob, os, shutil

GlobPatterns = { "/anope/*.db" : "Anope_Configs", "/Unreal/unrealircd.conf" : "Unreal_Config" }
BackupDirectory = "/home/Backups"

for Glob, Directory in GlobPatterns.iteritems():
    FileList = []
    print "[MB]Searching...\"%s\"" % ( Glob )
    for Matched in glob.glob( Glob ):
        FileList.append( Matched )
    print "[MB]Found %i Files" % ( len(FileList) )
    if not os.path.exists( BackupDirectory ):
        print "[MB]Main Backup directory \"%s\" does not exist....creating" % ( BackupDirectory )
        os.mkdir( BackupDirectory )
    FullDir = BackupDirectory + os.sep + Directory + ":" + datetime.datetime.now().strftime("%m:%d:%Y:%H")
    if not os.path.exists( FullDir ):
        print "[MB]Backup Sub Directory \"%s\" does not exist....creating"
        os.mkdir( FullDir )
    for File in FileList:
        print "[MB]Copying \"%s\" to \"%s\"...." % ( File, FullDir )
        shutil.copy( File, FullDir )
print "[MB]Job Completed"
Wow really nice hehe, I wish I could come up with useful stuff like this, rather then xchat scripts lol i gots no imagination
I mean you could just get Anope to use MySQL for more awesomeness but whatever, nice.
Man you've inspired me!
That a really a good snippet, Nice share and Thanks!

[color=#FF0000 (Click to View)
Thanks guys, and

Code:
print "[MB]Backup Sub Directory \"%s\" does not exist....creating"

should be

Code:
print "[MB]Backup Sub Directory \"%s\" does not exist....creating" % ( FullDir )

not really a big problem though