Skip to content

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:

  1. 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.

  2. 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.

18 points18 comments
Open on Reddit
About this post
Posted
Archived
Score
18 · 100% up
Kind
Text
ID
4bie03

Author

72
posts
211
comments
6
media
View profile

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.

7 points

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.

2 points

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.

7 points

Do it by MAC address.

sudo arping -f -I wlp5s0 192.168.1.1 | cut -d ' ' -f 5 | sed -n 2p

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.

4 points

This looks like a promising approach, although, having adjusted the arping args for my system, I'm getting:

cut: ' ': No such file or directory

The response of the arping command alone is:

ARPING 192.168.0.1 from 192.168.0.7 enp0s25
Unicast reply from 192.168.0.1 [80:EE:73:10:95:C1]  0.768ms
Sent 1 probes (1 broadcast(s))
Received 1 response(s)
1 points

You're missing the -d option from cut. It's really important. Command absolutely needs to be

cut -d ' ' -f 5

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>

1 points

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.

3 points

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.

2 points

If you are using bash:

if [[ "$(iwgetid --raw)" == "HOME_NETWORK_SSID" ]]; then
    # do some stuff
fi
4 points

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:

if ping -c 5 <SERVER_ADDRESS> >/dev/null; then
    echo "Network is good."
else
    echo "Network is down."
fi

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.

2 points

The cronie package 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.

2 points

Really don't mind how long it takes. What's the best nice "setup"?

1 points

Lowering priority below everything else. You don't need cronie, just use a systemd timer. :)

Are you a networkmanager user?

1 points

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.

1 points

Keyboard shortcuts

Move and act

jork
Next / previous post or comment
Enteroro
Open the post
f
Open the media viewer
s
Save
h
Hide (the toast has Undo)
Tab
Right after an action: the toast's Undo
x
Collapse the comment
v
Next layout: cards, compact, grid

Go to

/
Search
gthenh
Home
gthena
All
gthens
Subreddits
gthenl
Library
?
This list
Esc
Close a menu or this list

In the viewer

←or→
Previous / next file
↑or↓
Previous / next post
korj
Previous / next post
Space
Play / pause
m
Mute
z
Zoom
s
Save
o
Open the post (on its own page: back to it)
Esc
Close

Keys do nothing while you type in a box.