2018-01-14 12:49:22 +01:00
### Backup
2021-05-04 13:14:39 +02:00
#### Manual
2018-01-14 12:49:22 +01:00
You can use the provided script `helper-scripts/backup_and_restore.sh` to backup mailcow automatically.
Please do not copy this script to another location.
To run a backup, write "backup" as first parameter and either one or more components to backup as following parameters.
2019-12-21 15:56:33 +01:00
You can also use "all" as second parameter to backup all components. Append `--delete-days n` to delete backups older than n days.
2018-01-14 12:49:22 +01:00
```
# Syntax:
2019-12-21 15:56:33 +01:00
# ./helper-scripts/backup_and_restore.sh backup (vmail|crypt|redis|rspamd|postfix|mysql|all|--delete-days)
2018-01-14 12:49:22 +01:00
2019-12-21 15:56:33 +01:00
# Backup all, delete backups older than 3 days
./helper-scripts/backup_and_restore.sh backup all --delete-days 3
2018-01-14 12:49:22 +01:00
2019-12-21 15:56:33 +01:00
# Backup vmail, crypt and mysql data, delete backups older than 30 days
./helper-scripts/backup_and_restore.sh backup vmail crypt mysql --delete-days 30
# Backup vmail
./helper-scripts/backup_and_restore.sh backup vmail
2018-01-14 12:49:22 +01:00
```
The script will ask you for a backup location. Inside of this location it will create folders in the format "mailcow_DATE".
You should not rename those folders to not break the restore process.
2018-10-15 11:39:20 +02:00
To run a backup unattended, define MAILCOW_BACKUP_LOCATION as environment variable before starting the script:
2018-01-14 12:49:22 +01:00
```
2018-10-15 11:39:20 +02:00
MAILCOW_BACKUP_LOCATION=/opt/backup /opt/mailcow-dockerized/helper-scripts/backup_and_restore.sh backup all
2018-01-14 12:49:22 +01:00
```
2021-04-21 19:17:14 +02:00
#### Cronjob
2021-05-04 13:13:58 +02:00
You can run the backup script regularly via cronjob. Make sure `BACKUP_LOCATION` exists:
2021-05-05 22:59:08 +02:00
```
5 4 * * * cd /opt/mailcow-dockerized/; MAILCOW_BACKUP_LOCATION=/mnt/mailcow_backups /opt/mailcow-dockerized/helper-scripts/backup_and_restore.sh backup mysql crypt redis --delete-days 3
2021-05-04 13:13:58 +02:00
```
Per default cron sends the full result of each backup operation by email. If you want cron to only mail on error (non-zero exit code) you may want to use the following snippet. Pathes need to be modified according to your setup (this script is a user contribution).
This following script may be placed in `/etc/cron.daily/mailcow-backup` - do not forget to mark it as executable via `chmod +x` :
2021-04-21 19:17:14 +02:00
```
#!/bin/sh
# Backup mailcow data
# https://mailcow.github.io/mailcow-dockerized-docs/b_n_r_backup/
set -e
OUT="$(mktemp)"
export MAILCOW_BACKUP_LOCATION="/opt/backup"
SCRIPT="/opt/mailcow-dockerized/helper-scripts/backup_and_restore.sh"
PARAMETERS="backup all"
OPTIONS="--delete-days 30"
# run command
set +e
"${SCRIPT}" ${PARAMETERS} ${OPTIONS} 2>& 1 > "$OUT"
RESULT=$?
if [ $RESULT -ne 0 ]
then
echo "${SCRIPT} ${PARAMETERS} ${OPTIONS} encounters an error:"
echo "RESULT=$RESULT"
echo "STDOUT / STDERR:"
cat "$OUT"
fi
```