]>
git.p6c8.net - ldap_backup.git/blob - ldap_backup.rb
3 # Keep this amount of backups (excluding the previously done backup)
7 # LDAP databases to backup
8 # (default: 1 / default database)
11 # Path to backup directory
12 $BACKUP_DIR = '/root/ldap_backup'
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)
19 # Create the backup directory if it does not exist
20 $CREATE_BACKUP_DIR = false
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)
30 file_name
+= 'db' + db
.to_s
+ '-' unless db
.nil?
32 file_name
+= timestamp
+ '.ldif'
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
46 files
= Dir
.entries(dir
)
48 files
.keep_if
{ |f
| f
.start_with
?('backup-') and f
.end_with
?('.ldif') }
51 files
.delete_if
{ |f
| f
.include?('-db') }
53 files
.keep_if
{ |f
| f
.include?('-db' + db
.to_s
+ '-') }
56 files
.delete(keep_file
)
60 # Check if there are more files than we want to keep and remove the
61 # outdated ones if necessary
63 if files
.size
> keep_backups
64 files_delete
= files
.slice(0, files
.size
- keep_backups
)
66 files_delete
.each
do |f
|
67 puts
"Deleting #{f}..."
70 File
::unlink(dir
+ '/' + f
)
71 rescue SystemCallError
=> e
72 puts
"Could not delete file #{f}"
79 # Check if slapd is running
82 unless system('pidof slapd > /dev/null')
83 puts
"slapd not running"
88 # Check if backup directory exists
90 if (defined?($BACKUP_DIR)).nil? or $BACKUP_DIR.empty
?
94 unless Dir
.exists
?($BACKUP_DIR)
96 puts
"Creating directory #{$BACKUP_DIR}..."
99 Dir
.mkdir($BACKUP_DIR)
100 rescue SystemCallError
=> e
101 puts
"Could not create directory #{$BACKUP_DIR}"
106 puts
"Directory does not exist"
117 file_date
= DateTime
.now
.strftime('%Y%m%d-%H%M%S')
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
124 backup_command
= command
+ ' -n ' + db
.to_s
+ ' -l ' + backup_file_path
126 puts
"Backing up database #{db} to #{backup_file_path}"
128 if system(backup_command
)
129 if $KEEP_BACKUPS >= 0
132 rotate_backups($BACKUP_DIR, $KEEP_BACKUPS, backup_file
, db
)
135 puts
"Backing up database #{db} failed with exit code " + $
?.exitstatus
.to_s
139 backup_file
= make_file_name(file_date
)
140 backup_file_path
= $BACKUP_DIR + '/' + backup_file
142 backup_command
= command
+ ' -l ' + backup_file_path
144 puts
"Backing up default database to #{backup_file_path}"
146 if system(backup_command
)
147 if $KEEP_BACKUPS >= 0
150 rotate_backups($BACKUP_DIR, $KEEP_BACKUPS, backup_file
)
153 puts
"Backing up default database failed with exit code " + $
?.exitstatus
.to_s
patrick-canterino.de