SoFunction
Updated on 2025-05-20

Run rsync in the background and logging methods using nohup and --remove-source-files

1. What is --remove-source-files?

  • effect: After the file is successfully synchronized to the target path, delete the file in the source path.
  • Applicable scenarios: You need to "move" files from one place to another, rather than keeping copies, such as cleaning old data, migrating files to new storage.
  • Notice: This option only deletes files and does not delete the source directory structure.

With commonmvCompared with commands,rsync --remove-source-filesMore suitable for mobile operations across devices or across networks, as it supports incremental transmission and error recovery.

2. Sample commands

The following is a usenohupand--remove-source-filesThe complete command template:

nohup rsync -av --remove-source-files /source/path/ /destination/path/ > /var/log/rsync_$(date +%Y%m%d_%H%M%S).log 2>&1 &

Let's break this command step by step.

3. Detailed explanation of the command

1. nohup

  • effect: Make sure the task continues to run after the terminal is closed, ignoring the suspend signal (SIGHUP).
  • usage: Put it at the beginning of the command.

2. rsync -av --remove-source-files

  • -a(Archive mode): Recursively synchronize directories, retaining file attributes (such as permissions, timestamps).
  • -v(Detailed output): Displays detailed information about synchronization and deletion.
  • --remove-source-files: Delete the source file after synchronization is completed.

Source path and destination path

  • /source/path/: Contains files or directories that need to be moved.
  • /destination/path/: The destination location of the file.

3. Log redirection

> /var/log/rsync_$(date +%Y%m%d_%H%M%S).log

  • Redirects standard output to log file.
  • use$(date +%Y%m%d_%H%M%S)Generate timestamps, e.g.rsync_20250324_153022.log

2>&1: Redirect standard errors to the log file as well, ensuring that all information is logged.

4. Backend Run &

  • effect: Put the task into the background and release the terminal.

4. Use scenarios and examples

Scene 1: Local file movement

Will/data/old/Move the file in/data/new/

nohup rsync -av --remove-source-files /data/old/ /data/new/ > /var/log/rsync_$(date +%Y%m%d_%H%M%S).log 2>&1 &
  • result/data/old/The files in it are synchronized to/data/new/and delete, and the directory structure is retained.

Scenario 2: Remote file migration

Move local files to a remote server:

nohup rsync -avz --remove-source-files -e ssh /local/data/ user@remote:/remote/data/ > /var/log/rsync_$(date +%Y%m%d_%H%M%S).log 2>&1 &
  • -z: Compress data and optimize network transmission.
  • -e ssh: Connect to the remote server via SSH.

Scene 3: Specific file movement

Move only specific types of files (e.g..txtdocument):

nohup rsync -av --remove-source-files --include '*.txt' --exclude '*' /source/ /destination/ > /var/log/rsync_$(date +%Y%m%d_%H%M%S).log 2>&1 &
  • --includeand--excludeCombination filters files that need to be moved.

5. Operation steps

Preparation

  • Ensure that the source and destination paths exist and are accessible.
  • Create a log directory:mkdir -p /var/log/
  • Check permissions: Have read and write permissions to the source path and write permissions to the target path and log path.

Execute the command

  • Enter the command and run it, and the task will be started in the background.
  • View the process:ps -ef | grep rsync

Verification results

  • Check the log:tail -f /var/log/rsync_*.log, confirm the synchronization and deletion operations.
  • Check the source path: Confirm that the file has been deleted.
  • Check the target path: Confirm that the file is synchronized correctly.

6. --Remove-source-files working principle

  • Synchronization priority: The source file will be deleted only after the file is successfully synchronized to the target path.
  • Security: If synchronization fails (such as the destination disk is full or the network is out), the source file is retained.
  • Directory reserved: The empty directory in the source path will not be deleted, only the file will be affected.

For example:

  • Source Directory/source/Include:
/source/
├── 
├── 
└── subdir/
    └── 

runrsync -av --remove-source-files /source/ /dest/back:

  • /dest/Contains all files.
  • /source/Change to:
/source/
└── subdir/
  • The file is deleted, but the directory structure remains.

7. Things to note

Use with caution

  • --remove-source-filesThe source file will be permanently deleted, and it is recommended to backup or test before operation.
  • Can be used first--dry-runSimulate the run, check the results:rsync -av --remove-source-files --dry-run /source/ /dest/

Disk space

  • Make sure that the target path has enough space, otherwise the synchronization fails and the source file will not be deleted.

Log Importance

  • The log records the details of deletion and synchronization, and if problems occur, you can quickly locate it.

Permissions issues

  • Write permission is required for deleting source files, and corresponding permissions are required for writing target paths.

Network outage

  • If the remote synchronization is interrupted,rsyncSupports continuous transmission, unfinished files will not be deleted.

8. Advanced usage

  • Limit bandwidth: Avoid affecting the network, add--bwlimit=1000(1MB/s)。

Timing tasks: Combined with cron, move files at 2 a.m. every day:

0 2 * * * nohup rsync -av --remove-source-files /source/ /dest/ > /var/log/rsync_$(date +\%Y\%m\%d_\%H\%M\%S).log 2>&1 &

Error notification: Add a failed prompt:

nohup rsync -av --remove-source-files /source/ /dest/ > /var/log/rsync_$(date +%Y%m%d_%H%M%S).log 2>&1 || echo "Rsync failed" | mail -s "Error" admin@ &

Summarize

passnohupand--remove-source-files, We can safely and efficiently move files from one place to another and use logging to record the entire process.

This method is particularly practical in scenarios such as data migration and cleaning old files. As long as you pay attention to permissions, space and backups, you can give full play to its advantages.

The above is personal experience. I hope you can give you a reference and I hope you can support me more.