Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Syncing entire computer with rsync

If none of the specific sub-forums seem right for your thread, ask here.
Post Reply
Message
Author
bedtime
Posts: 146
Joined: 2012-12-16 19:34
Has thanked: 1 time
Been thanked: 6 times

Syncing entire computer with rsync

#1 Post by bedtime »

I'd like to sync my ram root drive '/' to my USB device '/dev/sda1', which is mounted on '/mnt'. The entire system is running on USB, and is copied and ran in ram on boot.

To put it simply, I want to sync / to /mnt.

For the most part, what I have so far works, but I've noticed that it won't sync the apt repos when I update them; I have to do an 'apt update' after every reboot if I want to install a new package.

syncroot.sh

Code: Select all

#!/bin/sh

mkdir /mnt

mount /dev/sda1 /mnt

# View in dry run mode which only shows what files would have been changed but makes no actual changes:
rsync --dry-run -av --delete --exclude proc/ --exclude media/ --exclude tmp/ --exclude mnt/ --exclude dev/ --exclude sys/ --exclude run/ / /mnt/ | less

echo
echo "Syncing in 5 seconds. Press ctrl-c to exit."
sleep 5

# DO NOT RUN ON A PRODUCTION MACHINE!!!!
# UNCOMMENT ONLY IF YOU'RE A MASOCHIST!!!
#rsync -av --delete --exclude proc/ --exclude media/ --exclude tmp/ --exclude mnt/ --exclude dev/ --exclude sys/ --exclude run/ / /mnt/

echo "Running sync..."
sync
I installed nano, and the nano ended up working well, but who knows what might happen if I tried to update a complex package like the kernel.

Anything I can change or add to make this run better?

Segfault
Posts: 993
Joined: 2005-09-24 12:24
Has thanked: 5 times
Been thanked: 17 times

Re: Syncing entire computer with rsync

#2 Post by Segfault »

Computer programs run from RAM, this was the very cause RAM was invented. So you copy your binaries into RAM and then when they are executed they will be copied into another region of RAM ... I fail to see the rationale here?

bedtime
Posts: 146
Joined: 2012-12-16 19:34
Has thanked: 1 time
Been thanked: 6 times

Re: Syncing entire computer with rsync

#3 Post by bedtime »

Segfault wrote: 2021-12-06 18:34 Computer programs run from RAM, this was the very cause RAM was invented. So you copy your binaries into RAM and then when they are executed they will be copied into another region of RAM ... I fail to see the rationale here?
I agree, in part, but the ram boot allows the user to do some pretty cool things. You could change stuff around on a ram disk, seeing the results, without ruining your OS, so it can have its uses for experimentation. You could also 'freeze' a computer, only running in ram. You'd get it how you liked it, then you just run it in ram from then on, so if you ever got a virus or wrecked anything, it would just be reset upon reboot. Since you can take the USB stick out of the computer, there is little chance of those changes persisting.

One could always use virtual software, but this is native and works better on USB drives with little space. And sometimes you just don't want to save your session in any way. Lets say you wanted to use this for a public kiosk. That would work nicely too.

I now have two USBs with the exact same contents, one being the backup. They're an entire OS on a stick, able to be carried around from computer to computer. And they could be backed up in minutes via an rsync (once I get this all setup).

bedtime
Posts: 146
Joined: 2012-12-16 19:34
Has thanked: 1 time
Been thanked: 6 times

Re: Syncing entire computer with rsync

#4 Post by bedtime »

So far, after dozens of syncs, it's been working perfectly. :D

Here is the update script:

sync.sh:

Code: Select all

#!/bin/sh

set -ep


if [ ! "$(mount | grep 'none on / type tmpfs')" ] && [ ! "$2" = "/dev/sda3" ]; then
   echo "You cannot backup $2 onto itself. Exiting."
   exit
fi

sudo mkdir -p /mnt

if [ "$(mount | grep ^$2)" ]; then
   echo "\n$2 already mounted.\nUnmounting..."
   sudo umount "$2"
fi

echo "\nMounting $2 to /mnt...\n"
sudo mount "$2" /mnt

sudo rsync --dry-run -av --delete --exclude /proc/ --exclude /media/ --exclude /tmp/ --exclude /mnt/ --exclude /dev/ --exclude /sys/ --exclude /run/ --exclude .cache/ "$1" /mnt"$1" | less

if [ "$3" = "-s" ]; then

   t=5; while [ $t -gt 0 ]; do echo -ne "\rBacking up in $t seconds..."; t=$(( t - 1 )); sleep 1; done

   echo "\nRunning rsync...\n" 

   sudo rsync -av --delete --exclude /proc/ --exclude /media/ --exclude /tmp/ --exclude /mnt/ --exclude /dev/ --exclude /sys/ --exclude /run/ --exclude .cache/ "$1" /mnt"$1" 

   echo "\nRunning sync..."
   sync

fi

echo "\nUnmounting $2..."
sudo umount "$2"

echo "\nFinished without errors.\nYou may remove the USB stick.\n\nPress any key to exit."

bash -c 'read -n 1'
It is run as (where /dev/sda1) is the target device:

Code: Select all

# ./sync / /dev/sda1 -s
I got the backup partition working, so the entire system can be synced to the backup partition using this above script, within a matter of seconds or minutes. Getting everything working involved in me messing up the OS several times, but the secondary backup partition allowed for easy restoration of the primary system. I then cloned the entire USB, in the event that I borked the entire first USB.

The breakdown of the 30G USB is as follows:

1G GPT partition to boot on UEFI (though the system can also boot in legacy)
8G ext4 partition for /root and /home files together
8G ext4 partition, which is a clone of the first ext4 partition and used as a backup
8G ext4 partition for media (music, videos, large documents....) will not be loaded in ram
4G swap partition for suspend, hibernate, hybrid-sleep... this works in either ram boot or regular persistent boot

Whole thing works like a charm, and I've been watching videos on it, writhing this post on it, and using it as my main system. It's used on both my stationary computer and my laptop, as the kernel is the non-targeted sort. Non-free firmware packages have been installed to allow for booting from just about any machine.

It's basically a computer in a pocket. :)

Post Reply