Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 5536

Advanced users • Re: A bit of advice and some questions about disk and drives

$
0
0
cron is used to schedule command to execute at a certain time. rsync is used to sync two directories. So, yes, you need both. Both are part of a default installation.

I use the following in root's crontab to backup the root partition on one of my Pi:

Code:

00 05 * * * /usr/bin/rsync -axrH / /backup >/backups/log 2>&1
At 5 minutes past midnight every day cron runs rsync with the axrH option to copy the root partition to the backup location mounted at /backup.

a: archive mode; equals -rlptgoD (no -H,-A,-X)
x: don't cross filesystem boundaries
r: recurse into directories
H: preserve hard links

">/backups/log 2>&1" shoves the ouput from rsync into a file rather than having cron throw it away.

For more see the output from man cron and man rsync

Ok I see, so I spend some time and read some and experimented a bit with rsync to ty and get the basics, this is what I have come up with so far

Code:

sudo rsync -axrHv --exclude=/mnt/* --exclude=/media/* --exclude="lost+found" --exclude="swapfile" --exclude=/run/* --exclude=/tmp/* --exclude=/var/tmp/* --exclude=/proc/* / /mnt/Backup/ > /mnt/Pi5-NAS/Backup/backup.log
Is it smart to exclude what I have? (my thinking was/is, if and when things are so bad that I have to restore then I wouldn't need to restore what a freshly installed os would install by itself,

There's nothing worng with having additional excliudes but you have more that you need.

Including the -x options meand rsync won't cross between the root partition and a device mounted on to it. That means at least the following will not be copied with rsync:
  • /sys
  • /proc
  • /dev
  • /run
  • Any partition you have mounted via the desktop's automouunter, /etc/fstab, or a mount command.
  • Any network resources you have mounted via the desktop's automouunter, /etc/fstab, or a mount command.
Unfortunately it also means it won't backup /boot/firmware so you'll need a separate cron job to do that.
also I think I should use --delete in the the restore command to prevent the restore process from overtiring what it should not (I bit of guessing and some assuming, cause I haven't tried to restore yet).

Be very careful when using --delete. Getting it wrong could potentially make your OS unbootable. It's not an option I have much experience with.
Btw, for some reason the .log was empty without the -v (for verbose) so I added that but what I'm really after is basically the last lines of the output and if there has been any errors. I guess I have to use grep or something to archive that kind of functionality(?).

It was likely empty because your rsync ran without errors and you're not redirecting all of the potential output to the log file. Every process has at least two output streams: one for normal output and one for errors. appending 2>&1 will send the error output to the same place as the normal (officially called "standard") output
My next step is to put it in an executable .sh file and use date as a function so every time the .sh is execute I would get a subfolder with the current date as its name.
If you're going to create a new directory on the target device and rsync into that there won't be anything to overwrite or for --delete to process on the target. When you have moved to that script you could do both rsync in there but make sure you do / before /boot/firmware

And be aware that you canot just rsync back to an empty SD card to restore the backup. You have to prepare the partitions first and have an OS running from some other device before you can restore.

If you want to have something that you can jusr write to an SD card with RPi Imager, rsync is not the tool for you. RonR's image-backup may be of interest.

Statistics: Posted by thagrol — Sun May 05, 2024 12:10 am



Viewing all articles
Browse latest Browse all 5536

Trending Articles