brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: rsync --remove-source-files moves files instead of copying them

Backup
~/tips/tip-rsync-remove-source-files

rsync -av --remove-source-files ~/Downloads/done/ [email protected]:/mnt/user/media/incoming/

Copies files to another machine with rsync and deletes each one once it safely arrived.

mv can’t move files to another machine, and copying first and deleting by hand later leaves a window where you delete the wrong thing. rsync has a flag that does both in one go, and only deletes what really arrived.

rsync -av --remove-source-files ~/Downloads/done/ [email protected]:/mnt/user/media/incoming/

Every file that was copied successfully to the other side is removed from the sending side. A file that failed stays where it was, so you can run the same command again. That makes it a good fit for an outbox folder: things land there, a cron job pushes them to the server, and the folder empties itself.

It only removes files, though. Folders stay, so after a move you’re left with an empty tree on the source. Clean that up with find:

find ~/Downloads/done -mindepth 1 -type d -empty -delete

The -mindepth 1 keeps the top folder itself.

The caveat the manual stresses: only use it on files that are finished. If a program is still writing a file when rsync picks it up, you can end up with half a file on the server. rsync does skip the removal and prints an error when a file’s size or modification time changed during the transfer, but my advice is not to lean on that. Let whatever writes into the folder use a temporary name, like foo.part, rename it when it’s done, and add --exclude='*.part' to the rsync command. And run it once with --dry-run first, because a typo in the source path now deletes things.

Further reading: rsync(1) manual

More Backup tips restic snapshots --group-by host,paths mysqldump --routines --events mydb > mydb.sql rsync -a --files-from=list.txt /home/me/ backup:/backup/ restic find "*.txt" restic stats All Backup tips (45) All topics

Link