]> git.p6c8.net - ldap_backup.git/blob - ldap_backup.rb
Added script file
[ldap_backup.git] / ldap_backup.rb
1 #!/usr/bin/env ruby
2
3 # Keep this amount of backups (excluding the previously done backup)
4 # -1 for indefinite
5 $KEEP_BACKUPS = 7
6
7 # LDAP databases to backup
8 # (default: 1 / default database)
9 $BACKUP_DBS = [0, 1]
10
11 # Path to backup directory
12 $BACKUP_DIR = '/root/ldap_backup'
13
14 # Check if slapd is running before backup
15 # (use this if you installed this script on a cluster and you don't want to
16 # run backups on the inactive nodes)
17 $CHECK_SLAPD = false
18
19 # Create the backup directory if it does not exist
20 $CREATE_BACKUP_DIR = false
21
22 require 'date'
23
24 # Compose the name of a backup file by joining a prefix, a supplied timestamp
25 # and the database number (optional). The result is just the file name, you
26 # have to prefix the directory on your own.
27 def make_file_name(timestamp, db=nil)
28 file_name = 'backup-'
29
30 file_name += 'db' + db.to_s + '-' unless db.nil?
31
32 file_name += timestamp + '.ldif'
33
34 return file_name
35 end
36
37 # Remove old backup files in the backup directory. You have to specify the
38 # directory, the number of backups you want to keep, a file you explicitly want
39 # to keep (i.e. the most recent file) and optionally a database number.
40 # Only backup files belonging to the specified database number will be removed
41 # (if not, the ones of the default database will be removed).
42 def rotate_backups(dir, keep_backups, keep_file, db=nil)
43 # Get all the files in the directory and remove everything from the list we
44 # are not interested in
45
46 files = Dir.entries(dir)
47
48 files.keep_if { |f| f.start_with?('backup-') and f.end_with?('.ldif') }
49
50 if db.nil?
51 files.delete_if { |f| f.include?('-db') }
52 else
53 files.keep_if { |f| f.include?('-db' + db.to_s + '-') }
54 end
55
56 files.delete(keep_file)
57
58 files.sort!
59
60 # Check if there are more files than we want to keep and remove the
61 # outdated ones if necessary
62
63 if files.size > keep_backups
64 files_delete = files.slice(0, files.size - keep_backups)
65
66 files_delete.each do |f|
67 puts "Deleting #{f}..."
68
69 begin
70 File::unlink(dir + '/' + f)
71 rescue SystemCallError => e
72 puts "Could not delete file #{f}"
73 puts e.message
74 end
75 end
76 end
77 end
78
79 # Check if slapd is running
80
81 if $CHECK_SLAPD
82 unless system('pidof slapd > /dev/null')
83 puts "slapd not running"
84 exit 1
85 end
86 end
87
88 # Check if backup directory exists
89
90 if (defined?($BACKUP_DIR)).nil? or $BACKUP_DIR.empty?
91 $BACKUP_DIR = '.'
92 end
93
94 unless Dir.exists?($BACKUP_DIR)
95 if $CREATE_BACKUP_DIR
96 puts "Creating directory #{$BACKUP_DIR}..."
97
98 begin
99 Dir.mkdir($BACKUP_DIR)
100 rescue SystemCallError => e
101 puts "Could not create directory #{$BACKUP_DIR}"
102 puts e.message
103 exit 1
104 end
105 else
106 puts "Directory does not exist"
107 exit 1
108 end
109 end
110
111 # Backup
112
113 puts "Backing up..."
114
115 command = 'slapcat'
116
117 file_date = DateTime.now.strftime('%Y%m%d-%H%M%S')
118
119 unless (defined?($BACKUP_DBS)).nil? or $BACKUP_DBS.empty?
120 $BACKUP_DBS.each do |db|
121 backup_file = make_file_name(file_date, db)
122 backup_file_path = $BACKUP_DIR + '/' + backup_file
123
124 backup_command = command + ' -n ' + db.to_s + ' -l ' + backup_file_path
125
126 puts "Backing up database #{db} to #{backup_file_path}"
127
128 if system(backup_command)
129 if $KEEP_BACKUPS >= 0
130 puts "Rotating..."
131
132 rotate_backups($BACKUP_DIR, $KEEP_BACKUPS, backup_file, db)
133 end
134 else
135 puts "Backing up database #{db} failed with exit code " + $?.exitstatus.to_s
136 end
137 end
138 else
139 backup_file = make_file_name(file_date)
140 backup_file_path = $BACKUP_DIR + '/' + backup_file
141
142 backup_command = command + ' -l ' + backup_file_path
143
144 puts "Backing up default database to #{backup_file_path}"
145
146 if system(backup_command)
147 if $KEEP_BACKUPS >= 0
148 puts "Rotating..."
149
150 rotate_backups($BACKUP_DIR, $KEEP_BACKUPS, backup_file)
151 end
152 else
153 puts "Backing up default database failed with exit code " + $?.exitstatus.to_s
154 end
155 end

patrick-canterino.de