0

I'm using Ubuntu 14.04.4 LTS as a home NAS server. I already made some scheduled rsync script to backup my photos, documents etc to an external drive.

I want to do the same with the entire system, like cloning the "/" partition to that external drive on-the-fly, or after an automated reboot weekly.

Is there any method for that which can be automated with a cron job?

Thanks in advance!

1

You can do what you want with one single command, with caveats.
Read on.

NOTICE: What follows is almost integrally quoted from this archlinux.org wiki. I introduced just a few changes in wording and format here and there, but nothing substantial. All the merit definitely goes to maintainers of and contributors to the excellent wiki.archlinux.org.

This command depends on brace expansion available in both the bash and zsh shells. When using a different shell, --exclude patterns should be repeated manually.

$ rsync -aAXv --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} / /path/to/backup/folder

Using the -aAX set of options, the files are transferred in archive mode, ensuring that symbolic links, devices, permissions and ownerships, modification times, ACLs and extended attributes are preserved.

The --exclude option will cause files that match the given patterns to be excluded.
The contents of /dev, /proc, /sys, /tmp and /run were excluded because they are populated at boot (while the folders themselves are not created), /lost+found is filesystem-specific.
Quoting the exclude patterns will avoid expansion by shell, which is necessary e.g. when backing up over SSH.
Ending the excluded paths with * will still ensure that the directories themselves are created if not already existing.

Note:
- If you plan on backing up your system somewhere other than /mnt or /media, do not forget to add it to the list of exclude patterns to avoid an infinite loop.
- If there are any bind mounts in the system, they should be excluded as well, so that the bind mounted contents is copied only once.
- If you use a swap file, make sure to exclude it as well.
- Consider also whether you really want to backup the /home/ folder. If it contains user data (e.g. on a desktop OS), it might be considerably larger than the system itself and NOT relevant to the backed up system itself.
- Otherwise [e.g. on a server machine] consider excluding unimportant subdirectories such as /home/*, /.thumbnails/*, /home/*/.cache/mozilla/*, /home/*/.cache/chromium/*, /home/*/.local/share/Trash/*, depending on software installed on the system. If GVFS is installed, /home/*/.gvfs must be excluded to prevent rsync errors.

If you want to do this in a cron job, e.g. at reboot, use the @reboot directive in the cron job instead of a particular day or time. For example, the following cron job runs the script.sh script file one time only after the system restarts:

In terminal (CTRL+ALT+T), from a non-root user account:

$ crontab -e
@reboot /absolute/path/to/script.sh

To program the job as a root cron job:

$ sudo -e crontab -e
@reboot /absolute/path/to/script.sh

Remember to specify the full path to the file you want to run in the cron job.
For more information about the @reboot directive and other directives you can use in cron jobs, type the following command at the command prompt:

$ man 5 crontab

Your script should be made executable.

$ sudo chmod +x /absolute/path/to/script.sh

If you plan to trigger it from a udev rule instead of a cron job, you should run the rsync cmd in the background, like so:

$ cat script.sh
#!/bin/bash
{
/usr/bin/rsync -aAXv --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} / /path/to/backup/folder
} &
exit 0

Remember Startpage is yr friend !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.