public
Description: A Python library for working with the SharePoint object model
Homepage:
Clone URL: git://github.com/glenc/sp.py.git
sp.py / src / backupsites.py
aa88df13 » glenc 2008-05-18 Modified the way stsadm han... 1 # backupsites.py
2 """
3 Backup Sites
4 This script will backup all site collections in the specified web application.
5
6 Usage:
7
8 ipy backupsites.py --url http://myserver --destination \\\\network\share
9
10 Arguments:
11
12 --url - web application url
13 --destination - location backups will be saved to
14 [--overwrite] - if passed, backups will be overwriten if they exist
15 [--help] - shows this help
ba434776 » glenc 2008-05-17 Added script to backup site... 16
aa88df13 » glenc 2008-05-18 Modified the way stsadm han... 17 """
18
ba434776 » glenc 2008-05-17 Added script to backup site... 19 import sp
20 from sp import stsadm
c238dfcf » glenc 2008-05-18 Added new scriptutil module... 21 import scriptutil
22 import sys
ba434776 » glenc 2008-05-17 Added script to backup site... 23
5e914f46 » glenc 2008-05-18 Cleanup on scripts 24 __all__ = ["backup_sites", "backup_site"]
25
aa88df13 » glenc 2008-05-18 Modified the way stsadm han... 26 # file extension for backup files
ba434776 » glenc 2008-05-17 Added script to backup site... 27 FILE_EXTENSION = ".bak"
28
aa88df13 » glenc 2008-05-18 Modified the way stsadm han... 29
30 def main(argv):
c238dfcf » glenc 2008-05-18 Added new scriptutil module... 31 args = scriptutil.getargs(argv, ["url=", "destination="], ["overwrite"], __doc__, True)
32 backup_sites(args["url"], args["destination"], args.has_key("overwrite"))
aa88df13 » glenc 2008-05-18 Modified the way stsadm han... 33
34
35 def backup_sites(url, destination, overwrite=True):
ba434776 » glenc 2008-05-17 Added script to backup site... 36 """Execute the script"""
37 webapp = sp.get_webapplication(url)
38
39 # make sure destination has a trailing slash
40 if destination[-1] != "\\":
41 destination = destination + "\\"
42
43 def do_backup(site):
aa88df13 » glenc 2008-05-18 Modified the way stsadm han... 44 backup_site(site, destination + _get_backup_filename(site.Url) + FILE_EXTENSION, overwrite)
ba434776 » glenc 2008-05-17 Added script to backup site... 45
46 sp.enum_sites(webapp, do_backup)
47
48
49 def _get_backup_filename(url):
50 url = url.replace("http://", "")
51 url = url.replace("https://", "")
52 url = url.replace("/", "_")
53 url = url.replace(":", ".")
54 return url
55
56
aa88df13 » glenc 2008-05-18 Modified the way stsadm han... 57 def backup_site(site, filename, overwrite=True):
ba434776 » glenc 2008-05-17 Added script to backup site... 58 """Back up a site collection to the specified location"""
59 site = sp.get_site(site)
60
c238dfcf » glenc 2008-05-18 Added new scriptutil module... 61 print "Backing up site", site.Url
aa88df13 » glenc 2008-05-18 Modified the way stsadm han... 62 stsadm.run("backup", url=site.Url, filename=filename, overwrite=overwrite)
ba434776 » glenc 2008-05-17 Added script to backup site... 63
64
65
66 if __name__ == '__main__':
aa88df13 » glenc 2008-05-18 Modified the way stsadm han... 67 main(sys.argv[1:])
ba434776 » glenc 2008-05-17 Added script to backup site... 68