Trying to organise my backup script
I currently have a working script which uses rsync to back up most of my home directory and my /etc configs. My problem is figuring out when this script should be run. It's for a laptop that stays mostly in the same place, but isn't (usually) left on overnight.
I have, currently, two possible approaches I'm trying to decide between, but I need help with whichever of them I implement:
-
Have the script try to determine if the laptop is connected to my home network (via either wired or wireless) and, if it is, perform the backup. This script would then be run at shutdown with the laptop only shutting down after it's completed the backup - if it performs one. I'm quite happy for my laptop to take a little while to shut down.
-
Use something like ionice or renice or some other nice so that the backup can run at startup without hogging my CPU/network bandwidth etc.
Is either of these approaches better than the other? Is there a better solution? Either way, I'm not entirely certain how to implement either of these solutions, so a little nudge in the right direction would be appreciated.
Thanks in advance.
About this post
- Posted
- Archived
- Score
- 18 · 100% up
- Kind
- Text
- ID
- 4bie03
18 comments
Take a look at rsnapshot:
https://wiki.archlinux.org/index.php/Rsnapshot
You can create a systemd timer to execute however frequent you like, or setup the service to run on startup/shutdown. You can also add the network/share as a dependency for the rsnapshot backup service to run.
thanks for the reply. rsnapshot looks interesting, but it still doesn't help with how I actually check what network the system is connected to.
To be honest, I already have the rsync script set up and working- it's just the when and how it should run I'm trying to figure out.
That said, rsnapshot does look like a neat solution.
Take a look at systemd services. You can set dependencies for the service to run, such as a network mount or network status. You might be able to figure out something there.
https://wiki.archlinux.org/index.php/systemd#Handling_dependencies
My only comment is to ensure your automatic backups do occur when you think they are, and that the files are actually restoreable. There is something to be said for manually kicking off a script and watching it run to completion.
We had one high profile incident where "automatic" backups had failed over weeks, leaving us in a bad spot.
Do it by MAC address.
will return the string [00:00:00:00:00:00] you can match on for your if statement. Brackets don't matter, but if you're a perfectionist you can s/ them with sed.
This looks like a promising approach, although, having adjusted the arping args for my system, I'm getting:
The response of the arping command alone is:
You're missing the -d option from cut. It's really important. Command absolutely needs to be
Nothing else. Just to explain that command after arping:
cut -d <field delimiter, required> ' ' <field delimiter is a space> -f <select field, delimited by space> 5 <5th field from left>
sed -n 2p <print the second line from the output, suppress all other lines>
Do you always manually shutdown your laptop? You could create a script to replace the typical shutdown command that checks for network connectivity, runs your rsync, then proceeds to issue the shutdown. Just use a name unique to your script and drop it in the /usr/local/bin directory. That would be my preferred solution as opposed to slowing the boot process or fiddling with evenly distributing system resources.
that's exactly what I meant by "solution" 1 - it's just that I'm not clear on how to check that I'm on the home network. The rest of the script works well.
If you are using bash:
There are a few ways to do this, how thorough of a test you want to run is up to you. The simplest test I can think of would be a ping of your remote host like this:
However this will fail if ICMP is disabled, and it won't tell you if the service you are trying to reach on the server is responding (an NFS export or HTTP server for instance). This also won't give you much information as to why the network is down. You can find some more in depth methods here, but a ping might be suitable for your case.
The
croniepackage includes anacron, which is used to run cron jobs asynchronously. Say for instance, you want a backup to run at 3am - If you're laptop is on at this time, it will back up then. If it's not, it will run the job the next time your laptop is on (with a random delay after startup).https://wiki.archlinux.org/index.php/Cron#Asynchronous_job_processing
Would definitely recommend running the process niced if you're not worried about how long it takes as well.
Really don't mind how long it takes. What's the best nice "setup"?
Lowering priority below everything else. You don't need cronie, just use a systemd timer. :)
Are you a networkmanager user?
Do you have a way to detect when your screen saver kicks on (like dbus?) Perhaps begin incremental backups 60 seconds after a screen saver event happens.