2019.10.12
Nathan Thompson
Following on the heels of the server configuration, we are now in deep with this project, no turning back and no slowing down. Keep on keeping on.
Here is where the actual backup magic will happen. We need a backup script and a way to schedule the backup script.
ssh-keygen
ssh-copy-id
which will send the public key if it is stored in the default location (~/.ssh/).ssh serveruser1@Server_IP_Address
The authenticity of host 'Server_IP_Address (Server_IP_Address)' can't be established.
…key fingerprint is YourServerKeyFingerPrint
Are you sure you want to continue connecting (yes/no/[fingerprint])?
~/.ssh/known_hosts
.sudo ssh serveruser1@Server_IP_Address
The authenticity of host 'Server_IP_Address (Server_IP_Address)' can't be established.
…key fingerprint is YourServerKeyFingerPrint
Are you sure you want to continue connecting (yes/no/[fingerprint])?
/root/.ssh/known_hosts
sudo pacman -S keychain
.bashrc
file{1} -- nano /home/clientuser1/.bashrc
eval `keychain --agents ssh --eval /home/clientuser1/.ssh/NameOfKey`
{1} Not .bash_profile
, as .bashrc
is sourced by login and non-login shells.
rsync -abvh -e ssh --backup-dir=/media/serveruser1/Backup_DATA/old_`date +%F_%H-%M-%S` --delete /home/clientuser1/ serveruser1@ServerIPAddress:/media/serveruser1/Backup_DATA/ClientUser1_Backup
-a
stands for archive and is actually a grouping of -rlptgoD
options.-b
stands for backup, notice I specified a backup directory to coincide with this option.-v
stands for verbose, in other words when you execute the script it will show everything that is happening.-h
stands for human readable progress, it gives output number in a human readable format.-e
specifies the remote shell to use, ssh in our case, hence the SSH following--backup-dir
shows the directory on our server and makes date stamped folders in this format old_YYYY-MM-DD_HH-MM-SS, so old_2019-10-07_20-40-53 stands for October 7, 2019 at 10:40PM and 53 seconds.--delete
tells our backup to delete files not found on the host, so the main backup folder on the server matches the user folder on the client computer. Which is why I decided to setup the "changed/deleted items folder".-n
to it. For instance you can start with rsync -abvhn
followed by the rest of the script./home/clientuser1/Documents/Backup/users_remote_backup
{2}#!/bin/bash
# Make sure you can log in to remote server without a password
eval `keychain --noask --eval /home/clientuser1/.ssh/NameOfKey`
source /home/clientuser1/.keychain/ComputerHostName-sh; rsync -abvh -e ssh --backup-dir=/media/serveruser1/Backup_DATA/old_`date +%F_%H-%M-%S` --delete /home/clientuser1/ serveruser1@ServerIPAddress:/media/serveruser1/Backup_DATA/ClientUser1_Backup
So what does all the gibberish mean? We are telling the script where the private key is located and the source of our keychain variable so it can be unlocked, followed by the actually rsync script.
{2} Whether you end your backup script with .sh
or if you leave it bare will depend on how you execute the script.
/etc/cron.daily
, /etc/cron.weekly
, or /etc/cron.monthly
directories, anacrontab
will use run-parts to execute the script. Do not append your script with .sh if placed in those directories as run-parts will error out.anacrontab
to launch your script, then appending the backup script with .sh
is suggested.sudo pacman -S cronie
/etc/cron.daily/
/etc/cron.weekly/
/etc/cron.monthly/
sudo cp /home/clientuser1/Documents/Backup/users_remote_backup /etc/cron.daily/
/etc/anacrontab
file needs to be edited to add a line for your script./etc/anacrontab
@daily 10 mybackup.daily /bin/bash /home/clientuser1/Documents/Backup/users_remote_backup.sh
{4}/etc/anacrontab
,0anacron
file cpsudo nano etc/cron.hourly/0anacron
# Do not run jobs when on battery power
{3} This is the method I use, copying the backup script into /etc/cron.daily/
and allowing run-parts
to execute.
{4} As described in footnote 2, run-parts does not like files ending in .
or .sh
, but since we are adding a specific script in this instance, you can leave .sh
appended to your script.
We now have our Linux clients configured to backup our data to the server. Excellent work so far, but now we have one more task ahead with configuring Windows clients to do the same. Also check out our alternate method for Linux rsync backups.