You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Linux系统每日增量备份定时任务配置需求(支持tar/cpio/dump命令)

Hey there! Let's figure out how to set up a daily incremental backup that only grabs files modified in the previous day. I'll break down methods using tar, cpio, and dump—plus how to automate it with cron.

1. Using tar for Daily Incremental Backups

tar has a built-in --newer flag that makes filtering files by modification date straightforward. Here's how to use it:

Step 1: Test the Backup Command

First, run this command manually to verify it works (replace /path/to/backup with the directory/files you want to back up):

tar -czf /backup/daily_incr_$(date +%Y%m%d).tar.gz \
  --newer=$(date -d "yesterday" +%Y-%m-%d) \
  /path/to/backup \
  --exclude=/proc --exclude=/sys --exclude=/dev
  • -c: Create a new archive
  • -z: Compress with gzip
  • -f: Specify the output file name (we include the date for easy tracking)
  • --newer=$(date -d "yesterday" +%Y-%m-%d): Only include files modified on or after yesterday's date
  • --exclude: Skip system directories that don't need backing up (adjust these based on your setup)

Step 2: Automate with Cron

Open your crontab editor (use sudo crontab -e if you need root access):

0 2 * * * /bin/bash -c 'tar -czf /backup/daily_incr_$(date +%Y%m%d).tar.gz --newer=$(date -d "yesterday" +%Y-%m-%d) /path/to/backup --exclude=/proc --exclude=/sys --exclude=/dev >> /var/log/daily_backup.log 2>&1'

This runs the backup every day at 2 AM. The >> /var/log/daily_backup.log 2>&1 part logs all output and errors to a file for troubleshooting.

2. Using cpio for Daily Incremental Backups

cpio works with find to filter files by modification time. It's a bit more flexible for complex filtering:

Step 1: Test the Backup Command

Run this to test (again, replace /path/to/backup):

find /path/to/backup -type f -mtime -1 \
  ! -path '/proc/*' ! -path '/sys/*' ! -path '/dev/*' \
  | cpio -ocv | gzip > /backup/daily_incr_$(date +%Y%m%d).cpio.gz
  • find ... -mtime -1: Finds files modified in the last 24 hours (yesterday to now)
  • ! -path: Excludes system directories
  • cpio -ocv: Creates an archive (-o), uses ASCII format for compatibility (-c), and shows verbose output (-v)
  • gzip: Compresses the archive to save space

Step 2: Automate with Cron

Add this line to your crontab:

0 2 * * * /bin/bash -c "find /path/to/backup -type f -mtime -1 ! -path '/proc/*' ! -path '/sys/*' ! -path '/dev/*' | cpio -ocv | gzip > /backup/daily_incr_$(date +%Y%m%d).cpio.gz >> /var/log/daily_backup.log 2>&1"

Same as before—runs at 2 AM and logs activity.

3. Using dump for Filesystem-Level Incremental Backups

dump is a filesystem-specific tool, so it works on entire partitions rather than individual directories. It's great for consistent backups of system partitions.

Step 1: First, Take a Full Backup

Before doing incremental backups, you need a base full backup (replace /dev/sda1 with your target filesystem):

dump -0u -z -f /backup/full_$(date +%Y%m%d).dump.gz /dev/sda1
  • -0: Full backup (level 0)
  • -u: Updates /etc/dumpdates to track when the backup was run
  • -z: Compresses the archive with gzip
  • -f: Specifies the output file

Step 2: Daily Incremental Backups

Now, set up daily incremental backups that only include files modified since the last backup:

dump -1u -z -f /backup/daily_incr_$(date +%Y%m%d).dump.gz /dev/sda1
  • -1: Incremental backup (level 1), which backs up all files changed since the last level 0 or level 1 backup (since we run this daily, it'll only grab yesterday's changes)

Step 3: Automate with Cron

Add these lines to your crontab to run a full backup on the first of every month, and incremental backups daily:

0 1 1 * * /bin/bash -c 'dump -0u -z -f /backup/full_$(date +%Y%m%d).dump.gz /dev/sda1 >> /var/log/daily_backup.log 2>&1'
0 2 * * * /bin/bash -c 'dump -1u -z -f /backup/daily_incr_$(date +%Y%m%d).dump.gz /dev/sda1 >> /var/log/daily_backup.log 2>&1'

The full backup runs at 1 AM on the first day of each month, and incremental backups run at 2 AM daily.

Important Notes for All Methods

  • Make sure the /backup directory exists and has write permissions for the user running the cron job (use sudo mkdir -p /backup if needed)
  • Test restoring backups periodically to ensure they work—there's no point in backups you can't recover from!
  • Adjust the exclude paths and backup times to fit your system's needs

内容的提问来源于stack exchange,提问作者Marcel Brou

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:08:09