Operations 11 min read

Master Linux Data Backup: Tar, Gzip, and Cron Automation Guide

This guide explains how to use the Linux tar command for manual and compressed backups, create shell scripts for automated backups with cron, and apply these techniques to back up and restore Foxmail Server data, covering command syntax, scheduling, and recovery steps.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Linux Data Backup: Tar, Gzip, and Cron Automation Guide

Manual Backup of Local Data

Linux provides the powerful tar command for creating archive backups. Originally designed for tape backups, it can now archive to any storage medium.

Typical tar command format:

The command creates backup.tar from the /home/html directory. Options: c – create an archive v – verbosely list processed files f – specify the archive filename /home/html – source directory

By default tar does not compress, resulting in large files. To compress with gzip after archiving:

This produces backup.tar.gz, a compressed backup file.

Restoring Data with Tar

The above command extracts backup.tar into the current directory.

Automatic Backup of Local Data

Running manual commands daily is tedious; Linux’s cron daemon can schedule them. Write a shell script (e.g., backup.sh) that creates a dated directory, archives and compresses files, then schedule it with cron.

Make the script executable with chmod +x backup.sh and run it via sh backup.sh or ./backup.sh. Schedule it for low‑load times, e.g., 3 AM daily.

Configure cron with crontab -e and add: 0 3 * * * /home/foxmail/bin/backup.sh Verify the entry with crontab -l. Cron will then invoke the script each night.

FMS (Foxmail Server) Backup Method

Foxmail Server data includes mail files, user info, address book (OpenLDAP), logs, and temporary data.

Mail data resides under /home/webmail/ (e.g., /home/webmail/magedu.com/a/admin).

User info is stored in the MySQL database qmail.

Address book data is in /usr/local/var/openldap-ldbm.

Log files are in /var/log/qmail (mail) and /usr/local/resin/log (web).

Temporary data is under /tmp/webuser.

Backup steps:

Create directories for backup data and logs (e.g., /home/foxmail/backup).

Archive mail data with tar (example image):

Export user database:

mysqldump -u[username] -p[password] qmail > userdata.sql

Backup OpenLDAP data: tar zcvf addrdata.tar.gz [LDAP_directory] Archive log files from /var/log/qmail and /usr/local/resin/log.

Delete temporary data after stopping the server.

Automate FMS backup with cron:

crontab -e
0 3 * * * /home/foxmail/bin/backup.sh

When a disaster occurs, restore data by extracting the mail archive, importing userdata.sql back into MySQL, and replacing OpenLDAP files.

Summary

The provided backup.sh script (shown below) performs all required steps, enabling reliable nightly backups of Foxmail Server data.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Data Recoverycronshell scripttar
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.