Need a good kopano backup cron script example
-
Hi everyone.
Is there anyone about who would care to post a sanitized copy of their daily backup script for Kopano please?
Ive tried several times to build something but not being very good at shell scripting I am struggling.
I want to back up kopano as a whole nightly and retain for 14 days.
Then backup each user mailbox (foreach) for 7 days. Open to suggestions on the best practice.The intention is to target to a mounted NFS share from another server.
Thanks in advance
Regards
Ray -
/usr/sbin/kopano-backup -u <username> -O /srv/backup/kopano/ tar -zcvpf /srv/backup/mail-inbox-<username>.tar.gz /srv/backup/kopano/<username>/* rm -rf /srv/backup/kopano/<username>
Replace <username> with the inbox name and do this for all inboxes. Create a new Cronjob with the said script. Now backup the Tarball archives. Run the script according to your desired schedule, e. g. you could for create folders for daily, weekly, monthly etc.
-
Hi.
Here’s my nightly backup script. It is in production, but for Kopano Core 11.0.2.
Never tested it on core version bellow that.It’s adopted to your requests:
-
whole KC database backup, with retention days = 14 (you can set value between 2 - 99)
-
attachment backup directory
-
“foreach” user mailbox backup with retention days set to 7 (you can change the value between 2-9).
User mailboxes are backed up without attachments since there’s no need for duplication 'cause attachments are already backed up in separate folder. I also included option not to back up deleted items folder. (Disable if you wanna keep/backup them as well.)
Settings:
Change:
my $kopanoattsource = '/var/lib/kopano/attachments';
to your server’s file attachment directory. It’s set to Kopano default location.
Next, change your backup destination directory in here (my defaults):
my $backupdir = 'media/backup/kopano';
WARNING!
DO NOT add leading slash to variable $backupdir ! (like /media/… instead use just media/…) It MUST be without it, and yes it means absolute path from root.
(It’s about tar - it wants it like that.)In destination directory create subfolder “users”.
Until retention days/files are not reached, you’ll recieve errors, but these can be ignored, since until then there are no files yet to rotate them.
If you have any problems, just let me know…
#!/usr/bin/perl # Kopano Core 11.0.2 backup script my $retain_days = 7; my $dbretain_days = 14; my $dbcounter = $dbretain_days - 1; my $backupdir = 'media/backup/kopano'; my $kopanoattsource = '/var/lib/kopano/attachments'; my $i = 1; my $counter = $retain_days - 1; my $cpt = "/$backupdir*.$retain_days"; my ($usernum, @userlist) = listusers(); print "\nBacking up Kopano SQL database..."; my $kopanodbfile = "kopanosqldb.sqldump$dbretain_days"; my $j = length($kopanodbfile) - 2; my $cpt = substr $kopanodbfile, 0, $j; my $cpf0 = (substr $kopanodbfile, -2); my $cpf1 = (substr $kopanodbfile, -2) + $i; for (my $i=$dbcounter; $i--; $i >= 1) { $cpf0--; $cpf1--; if ($cpf0 < 10) { $cpf0 = "0" . $cpf0; } if ($cpf1 < 10) { $cpf1 = "0" . $cpf1; } commitcmd ("mv /$backupdir/$cpt$cpf0 /$backupdir/$cpt$cpf1"); } commitcmd ("mysqldump --single-transaction --routines kopano -r /$backupdir/kopanosqldb.sqldump01"); print "done\n Syncing attachements folder..."; commitcmd ("rsync -avzh $kopanoattsource /$backupdir"); print "done"; print "\nBacking up $usernum mailboxes for users: @userlist\n"; foreach $usrn (@userlist) { if ($usrn) { print "\nBacking up user '$usrn'..."; $userfile = "$usrn.tar.gz.$retain_days"; $j = length($userfile) - 1; $cpt = substr $userfile, 0, $j; $cpf0 = (substr $userfile, -1); $cpf1 = (substr $userfile, -1) + $i; commitcmd ("rm /$backupdir/users/$userfile"); for (my $i=$counter; $i--; $i >= 1) { $cpf0--; $cpf1--; commitcmd ("mv /$backupdir/users/$cpt$cpf0 /$backupdir/users/$cpt$cpf1"); } commitcmd ("kopano-backup -u $usrn --skip-attachments --skip-deleted -O /$backupdir/users/$usrn"); commitcmd ("tar -czf /$backupdir/users/$usrn.tar.gz.1 -C / $backupdir/users/$usrn"); commitcmd ("rm -rf /$backupdir/users/$usrn"); print "done\n" } } exit(0); sub commitcmd { my ($cmd) = @_; my $result; $result = `$cmd`; return $result; } sub listusers { my (@entry, @dlist); my $cmd = 'kopano-admin -l'; my $result = commitcmd ($cmd); chomp $result; $result = replace("\t", ',', $result); $result = replace(",,", ',', $result); $result = replace(' ', ' ', $result); my @lines = split /\n/, $result; my $numlines = $result =~ tr/\n//; my $numusers = $numlines - 4; while ($numlines > 4) { $numlines--; $lines[$numlines] =~ s/^,|\,$//g; @entry = split(/,/, $lines[$numlines]); $dlist[$numlines] = $entry[0]; } return ($numusers, @dlist); } sub replace { my ($search, $replace, $result) = @_; my $count = () = $result =~ /$search/g; while ($count) { $result =~ s/$search/$replace/ig; $count = () = $result =~ /$search/g; } return $result; }
-
-
@mapo Thank you so much.
This is amazing!Id spent a good few hours working out how Kopano backup worked, but not being fluent with bash scripts building it into a rotation backup was really challenging me.
I will apply this to my Kopano UCS system and let you know how I get along.
Ray
-
@mapo
Hi.Thank you for this great script! That saved me a lot of work :).
However, I have two questions:
- How does the restore work?
This call doesn’t work with message “please specify path to backup data”:
kopano-backup --index /var/backups/kopano_backup/users/[username] | grep [subject]
- The SQLdump has 0 bytes. I don’t think that’s possible. I use Kopano on a Univention-Host. Do User/Passwords for the database still have to be defined somewhere?
Or does the script need to be run as a specific user (not root).
Thank you!
Best wishes,
Philipp - How does the restore work?