help centre
For more info visit status.atmail.com

How can we help?


Search our knowledge base for answers to
common questions and latest updates.



My activities New request

Follow

Backup and Recovery

Stewart -

PROBLEM
I need to know how to backup and restore my Atmail installation.

ENVIRONMENT

  • On-Premise Server + WebMail Installations: Version 7.0 > Current Version
  • Webmail Only Installations: Version 7.0 > Current Version

CAUSE

  • Best practice requirements back up and restore

RESOLUTION

Backups are an important part of maintaining your Atmail system. This guide lists the many possible ways you can backup your Atmail server - offsite, or otherwise.

 

Onsite Backups

Backup to a local directory (Full backups)

This script can be used to backup your Atmail installation, and store it to a local drive or directory.

This script  can be automated using cron, and executes as either the root user, or as the 'atmail' user:

#!/bin/bash
# Atmail directory
ATMAIL_DIR="/usr/local/atmail/"
 
# Backup destination
BACKUP_DIR="/mnt/data/backups/"
 
# Date tag for backup files
DATETAG=`date "+%Y%m%d"`
cd $BACKUP_DIR
 
# Dump each database to its own file
mysqldump --opt -u mysql-user --password='mysql-password' atmail | gzip > atmail-data-$DATETAG.sql.gz
 
# Copy the atmail startup scripts to their own database (optional)
nice -n 10 tar -cfz atmail-script-$DATETAG.tar.gz /etc/init.d/atmailserver $ATMAIL_DIR/webmail/ $ATMAIL_DIR/server_source
 
# Dump atmail to its own file
nice -n 10 tar -cfz atmail-users-$DATETAG.tar $ATMAIL_DIR/users/

Backup to local directory (Incremental)

This script can be used to perform incremental backups of your Atmail system, using rsync. This is local-local only, and has to be within disks of the same filesystem format. This has the benefit of using significantly less space on large-scale deployments. Installation of rsync is required.

#!/bin/bash
# Atmail Directory
ATMAIL_DIR="/usr/local/atmail/users/"
 
# Backup Destination
BACKUP_DIR="/mnt/data/backups/"
 
# Date tag for backup files
DATETAG=`date "+%Y%m%d"`
 
# Rsync Options
OPTIONS="-e ssh -vrltgoi --delay-updates --delete"
 
# Minimum changes before backups
MINCHANGES=20
cd $BACKUP_DIR
 
# List files changed/added
rsync $OPTS $ATMAIL_DIR $BACKUP_DIR/latest >> $BACKUP_DIR/rsync.log
 
# Check if enough has changed
COUNT=$( wc -l $BACKUP_DIR/rsync.log|cut -d" " -f1 )
if [ $COUNT -gt $MINCHANGES ] ; then
if [ ! -e $BACKUP_DIR/$tstamp ] ; then
 
# Make a hardlinked copy named as the date
cp -al $BACKUP_DIR/latest $BACKUP_DIR/$DATETAG
 
# Move backup directory with hardlinks to directory with date tags
mv $BACKUP_DIR/rsync.log $BACKUP_DIR/$DATETAG
fi
fi

Offsite Backups

Backup to external server (FTP)

The following script creates backup files and moves/transfers them using FTP, without need for intermediate files. This makes sure that you can use as much of your allotted disk space as necessary. Without the need for intermediate files, you will not need to make sure that your local system has enough space for a full tar file. This method comes with the benefit of being able to make use of as much disk space as you can.

#!/bin/bash
# Atmail Directory
ATMAIL_DIR="/usr/local/atmail/users/"
 
# Backup Directory (not used)
BACKUP_DIR="/mnt/data/atmail/"
 
# Date tags for uploads
DATETAG=`date "+%Y%m%d"`
cd $BACKUP_DIR
 
# Dump each database to its own file
echo "Dumping MySQL database... \n"
mysqldump --opt -u root atmail | gzip > $ATMAIL_DIR/atmail-data.sql
 
echo "Uploading..."
 
nice -n 10 /usr/bin/ftp -inv [FTP server IP]<<ENDFTP
user atmailbackup password
binary
put |"nice -n 10 tar czf - $ATMAIL_DIR" backup-$DATETAG.tar.gz
bye
ENDFTP
 
echo "Removing cached files..."
rm -rf $ATMAIL_DIR/atmail-data.sql

Backup to external storage (SSH)

This method creates a tar file directly on the destination storage server, using the SSH protocol. This method provides an additional layer of security when transferring data over the network, and is recommended for external servers encompassing the public internet. As per above, this method does not require intermediate files. This makes sure that you can use as much of your allotted disk space as necessary. Without the need for intermediate files, you will not need to make sure that your local system has enough space for a full tar file. This method comes with the benefit of being able to make use of as much disk space as you can.

#!/bin/bash
# Atmail Directory
ATMAIL_DIR="/usr/local/atmail/users/"
 
# Backup Directory (not used)
BACKUP_DIR="/mnt/data/atmail/"
 
# Date tags for uploads
DATETAG=`date "+%Y%m%d"`
 
cd $BACKUP_DIR
 
# Dump each database to its own file
echo "Dumping MySQL database... \n"
mysqldump --opt -u root atmail | gzip > $ATMAIL_DIR/atmail-data-$DATETAG.sql
 
# Dump Atmail Maildir through message stream
echo "Creating tar file... \n"
tar cfz - $ATMAIL_DIR | ssh root@[SSH server IP] "cat > backup-$DATETAG.tar.gz"
 
echo "Removing cache files... \n"
rm -f atmail-data-$DATETAG.sql

 

Recovery: MySQL

Servers crash - and when they do, it's always nice to know that you can always restore from backups. This portion of the document details how to recover your data and restore your installation to optimal state.

This document details various ways to recover your MySQL data. For the sake of simplification, the following flagged words refer to different server settings:

[dbname] - database name
[dbuser]
- database user

Recover MySQL database from dump file

If your backup utility created a MySQL dump file, all you need to do is reimport it. First, create a backup of your current database:

% mysqldump -u [dbuser] -p [dbname] > mysqldump-old-db.sql

If this dump fails because of database corruption, you might want to create a backup of your MySQL data directory instead:

% tar cvfz /path/to/mysql-data.tgz /var/lib/mysql

After the backups are done, drop the database under the MySQL terminal:

% mysql -u [dbuser] -p [dbname]
> drop database [dbname];

For example, a drop command for a database called 'sample_data' would look like this:

% mysql -u [dbuser] -p [dbname]
> drop database [dbname];

Create the database again:

> create database [dbname];
> quit;

You can then import your old data with this command:

% mysql -u [dbuser] -p [dbname] < mysqldump.sql

Where 'mysqldump.sql' is your backed-up MySQL dump file.

Recover MySQL data from binlogs.

If your server has binlogs enabled, you can export the full history of MySQL commands for your server. This can then be imported into MySQL, so you can have a query-by-query replica of the MySQL server state you wish to restore.

For the purposes of this document, we are going to assume that your MySQL bin log directory is '/var/log/mysql/'. The directory then contains these files:

mysql-bin.000001
mysql-bin.000002
mysql-bin.000003
mysql-bin.000004
mysql-bin.000005

First, identify the timeframe you wish to restore. By default, specifying no flags outputs every query executed during the duration of the binlogs (default: 10 days).

In some cases, you may want to just specify a slice of time to restore the data from. If this is the case, you will need to specify the following flags:

--start-datetime=datetime
--stop-datetime=datetime

The datetime format follows YYYY-MM-DD HH:MM:SS. For this document, we will use the following parameters:

  - Start time: 2012-02-14 22:13:01
  - Stop time: 2012-05-14 00:00:00

The MySQL binlog command is as follows:

% mysqlbinlog --start-datetime="2012-02-14 22:13:01" --stop-datetime="2012-05-14 00:00:00" /var/log/mysql-bin.* > mysql-binlog-queries.sql

This  will generate a file called 'mysql-binlog-queries.sql'. You can then import this file:

% mysql -u [dbuser] -p [dbname] < mysql-binlog-queries.sql

Recovery: Maildir

Mail Restoration (Single User)


In the event of a need to restore a single user's account, you are to do the following:

- Login to WebAdmin > User Manager, and recreate the user
- Execute the following:

% cd /
% tar xvfz /path/to/backup-20120104.tar.gz /usr/local/atmail/users/[FIRST]/[SECOND]/[ACCOUNT NAME] -C /

Where [FIRST] is the first letter of the account, [SECOND] is the second letter, and [ACCOUNT NAME] is the email address. For example, a user called user@domain.com will need the following command:

% cd /
% tar xvf backup-20120104.tar.gz /usr/local/atmail/users/u/s/user@domain.com -C /
% chown -R atmail:atmail /usr/local/atmail/users/u/s/user@domain.com/

Then, go into the frontend servers (appliances), and execute:

% find /usr/local/atmail/users/u/s/user@domain.com/ -name dovecot* | xargs rm -rf

Where the format is:

% find /usr/local/atmail/users/[FIRST]/[SECOND]/[ACCOUNT NAME]/ -name dovecot* | xargs rm -rf

 

Mail Restoration (Global)

In the even of a filesystem catastrophic failure that prevents access to the maildir files, you are torestore the MailDir via the following steps:

- Create the base directory:

% mkdir -p /usr/local/atmail/users/

- Unpack Atmail:

% cd /
% tar xvf /path/to/backup-20120104.tar.gz -C /usr/local/atmail/users/
% chown -R atmail:atmail /usr/local/atmail/users/

 


 

 

Have more questions? Submit a request

Comments


Contact our support team


+61 (7) 5357 6605       support@atmail.com