0

I would like to have a script that periodically and automatically backs up select system files to my NAS, which I have in my fstab to cifs mounts on boot. Based on the responses from Which Folders To Include In backup?, I put together the following script to backup to a "System Files" folder on my NAS:

#!/bin/bash  
sudo rsync -aAXv --progress --delete /etc /NAS/System\ Files/  
sudo rsync -aAXv --progress --delete /opt /NAS/System\ Files/  
sudo rsync -aAXv --progress --delete /root /NAS/System\ Files/  
sudo rsync -aAXv --progress --delete /snap /NAS/System\ Files/  
sudo rsync -aAXv --progress --delete /usr/local /NAS/System\ Files/usr/  
sudo rsync -aAXv --progress --delete /var /NAS/System\ Files/  

Once I work out the bugs, I intend to set up a cron job to schedule this.

After testing just the /etc line of this code, I noticed there are numerous permissions errors such as:

rsync: symlink "/NAS/System Files/etc/rc0.d/K01bluetooth" -> "../init.d/bluetooth" failed: Permission denied (13)

In this answer, the user states that "Backing them (some files in root) up would be meaningless or they might be even harmful."

Is there a safe way to accomplish this without any errors or without harming my system? Any help is greatly appreciated.


FYI, I am aware that I can create a disk image using a program like Clonezilla, but I would prefer to use a solution that doesn't have to be manually maintained. Also, not sure if Timeshift would do what I'm trying to do, but since I'm running ZFS on root, Timeshift gave me the "device not found" error, since ZFS is not currently supported (as of Timeshift 20.03).

I already have my applications, repos, and keys backing up to my home folder using Apt-Clone, and Duplicity set to backup my home folder to my NAS.

References:
How can I set up incremental backups to include my themes and customisation on Ubuntu 18.04?
How to backup settings and list of installed packages
What is the easiest NAS setup to back up an Ubuntu machine?

guttermonk
  • 1,004
  • 14
  • 29
  • As with everything linux there are loads of ways to do this. Lots depends on your expertise and concern for safety/security. You can put the sudo password into the line command and your permission erros will clear. You can add your user to the list of sudoers. You can create new user called secretuser for example and add him to the sudoers and execute the command as that user. Let me know what you think and I will spell out the answer in more details. – walttheboss Jul 12 '20 at 23:03
  • When I had tested just the /etc line of that code and got a bunch of errors, I ran the script from terminal and manually entered the password. So I don't think the errors are sudo/password related. That said, you are correct, if I am able to eventually set this up as a cron job, I will need to do something along the lines of what you suggested. Thank you for the thoughtful comment. – guttermonk Jul 12 '20 at 23:26
  • Timeshift is in the repositories and serves exactly for what you want to do: back up system files to be able to revert to a previous state in case you mess up with the system. You can run it in rsync mode, and then it should support any file system that supports linux permissions. – vanadium Jul 13 '20 at 08:00
  • I ran Timeshift in rsync mode, and my ZFS root pool drives shows up as type: "zfs_member" and when I select it, I receive a message that says "Selected device does not have Linux partition". If I click next anyways, I get a pop-up message that says "Snapshot device not available. Device not found UUID='d2b5fb5b-fe28-47c3-8bab-ff88ad1535f4'." My only options at this point are to click Ok and Finish, which takes me back to the main GUI window where the "Create" button is grayed out. – guttermonk Jul 13 '20 at 11:50

1 Answers1

0

It took several days of work, but I was able to solve my problem by changing my back-up command and changing a setting on my NAS.

Here is my updated script, which I created a cron job for:

#!/bin/bash

sudo rsync -aAXvlH --delete --exclude={"/bin/","/boot/","/dev/","/home/","/lib/","/lib32/","/lib64/","/media/","/mnt/","/NAS/","/Pool-1tb/","/Pool-500gb/","/proc/","/run/","/sbin/","/srv/","/sys/","/tmp/","/usr/bin/","/usr/games/","/usr/include/","/usr/lib/","/usr/lib32/","/usr/libexec/","/usr/sbin/","/usr/share/","/usr/src/",".Trash"} / /NAS/System\ Files/

Here are the flags I used:

-a - Archive mode includes among other things, the options -o and -g, which preserves owners and groups. This requires that you run rsync as root.
-A - Preserve Access Control List.
-X - Preserve extended attributes.
-v - It will show the progress of the backup.
-l - Copy symlinks as symlinks.
-H - Preserve hard links.

To resolve the plethora of symlink errors I got when running rsync, on my Synology, I had to go to Control Panel / File Services / SMB / Advanced Settings and had to check the box next to "Allow symbolic links within shared folders".

If you found this helpful, please consider an up-vote. Thanks!

guttermonk
  • 1,004
  • 14
  • 29