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
100644 69 lines (44 sloc) 1.673 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
# backupsites.py
"""
Backup Sites
This script will backup all site collections in the specified web application.
 
Usage:
 
  ipy backupsites.py --url http://myserver --destination \\\\network\share
 
Arguments:
 
  --url - web application url
  --destination - location backups will be saved to
  [--overwrite] - if passed, backups will be overwriten if they exist
  [--help] - shows this help
 
"""
 
import sp
from sp import stsadm
import scriptutil
import sys
 
__all__ = ["backup_sites", "backup_site"]
 
# file extension for backup files
FILE_EXTENSION = ".bak"
 
 
def main(argv):
  args = scriptutil.getargs(argv, ["url=", "destination="], ["overwrite"], __doc__, True)
  backup_sites(args["url"], args["destination"], args.has_key("overwrite"))
 
 
def backup_sites(url, destination, overwrite=True):
  """Execute the script"""
  webapp = sp.get_webapplication(url)
  
  # make sure destination has a trailing slash
  if destination[-1] != "\\":
    destination = destination + "\\"
  
  def do_backup(site):
    backup_site(site, destination + _get_backup_filename(site.Url) + FILE_EXTENSION, overwrite)
  
  sp.enum_sites(webapp, do_backup)
 
 
def _get_backup_filename(url):
  url = url.replace("http://", "")
  url = url.replace("https://", "")
  url = url.replace("/", "_")
  url = url.replace(":", ".")
  return url
 
 
def backup_site(site, filename, overwrite=True):
  """Back up a site collection to the specified location"""
  site = sp.get_site(site)
  
  print "Backing up site", site.Url
  stsadm.run("backup", url=site.Url, filename=filename, overwrite=overwrite)
 
 
 
if __name__ == '__main__':
  main(sys.argv[1:])