It’s important to back up your iMac regularly to keep your files safe. You can back up your apps, accounts, settings, music, photos, movies, and documents using Time Machine, which is built into your Mac. Time Machine doesn’t back up the macOS operating system.
Decide where you want to back up your files. Time Machine can back up to an external storage device connected to your iMac, as well as other supported storage devices. For a list of devices supported by Time Machine, see the Apple Support article > System Settings, then click Control Center in the sidebar. Click the pop-up menu next to Time Machine on the right, then choose Show in Menu Bar.
Reinstall macOS. Your operating system files are kept separate from your personal files in a sealed system disk. However, some actions, like erasing or inadvertently damaging a disk, require that you restore your iMac. You can reinstall macOS and then use Time Machine to restore your personal files from your backup. With macOS Big Sur and later, there are several ways to restore your Mac. You may be required to install a newer version of macOS than what your computer originally came with, or what you were using before the disk was damaged. To learn more about these options, see Time Machine troubleshooting on Mac in the macOS User Guide. Also see the Apple Support article What to do before you sell, give away, or trade in your Mac.