Cron Backups (for minecraft) - Pachwenko/my-docs GitHub Wiki

Minecraft server backup examples with versioning

A few things first:

  • It is not advisable to run cron jobs as root. Ensure all commands are run by a non-root user.
  • Cron has a limited PATH. Use full paths e.g. /usr/bin/cp not cp
  • When using any percent signs (%) cron will read the file incorrectly. All percent signs must be backslash escaped. e.g. $(date "+\%A")
  • Don't use sudo when opening crontab sudo crontab -e and don't edit /etc/crontab. Instead become the user you want to run the cron as and do crontab -e
  • Rsync is not worth it for this use case.
  • Do research and ensure the commands can run outside of Cron. See https://serverfault.com/questions/449651/why-is-my-crontab-not-working-and-how-can-i-troubleshoot-it

Daily backups kept for 1 week (will make 7 folders, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday)

36 01 * * * tar -cvf /mnt/backups/automated/daily_$(date "+\%A").tar.gz /home/minecraft/minecraft/ -I "gzip --best" >> /tmp/backups.log 2>&1

Weekly backups kept for 1 month

48 02 * * 6 tar -cvf /mnt/backups/automated/weekly_$((($(date "+\%-d")-1)/7+1)).tar.gz /home/minecraft/minecraft/ -I "gzip --best" >> /tmp/backups.log 2>&1

Monthly backups on 2nd day of the month at 1am. Kept for 1 year

55 01 2 * * tar -cvf /mnt/backups/automated/monthly_$(date "+\%m").tar.gz /home/minecraft/minecraft/ -I "gzip --best" >> /tmp/backups.log 2>&1

Notes for improvement