Skip to main content

I had generated a backup of key directories on the server - see Create a Ubuntu backup shell script.  With a backup in hand, now it was time to test how to unpack a backup and overwrite the directories.

First I unpacked the backup to a temporary directory tmp-cc.

Now I wanted to replace several of the directories on the server.  Starting with home, var and etc.   But what is an efficient method to overwrite a directory?   Also removing any directories that aren't in the copied version.  Initially, I tried

cp -rlf tmp-cc/home/ home

And other varying forms of cp and mv.  None had the impact I was seeking.  Instead, I began to investigate rsync

rsync -a --delete tmp-cc/home/ home/
-a is 'archive mode', which copies faithfully files in foo/ to bar/
--delete removes extra files not in tmp-cc/home/ from home/ as well, ensuring home/ ends up identical
-vh verbose and human-readable

If you want to see what it's doing, then add -vh as noted in the table above

Note: the slash after foo is required, otherwise rsync will copy tmp-cc/home/ to home/home/ rather than overwriting home/ itself.

Related articles

Andrew Fletcher20 May 2024
Create a copy of files that go to the tmp directory
To review the content of files being generated in the /tmp directory on an Ubuntu server before Microsoft Defender removes them, you can use several approaches.  Following is the approach we took. Real-Time MonitoringYou can set up a script to monitor the /tmp directory and log the...