Ultra.cc External Backup Script using Rsync
Adding SSH Keys for passwordless connection
TBA
The Shell script
This Shell script will allow for an automatic backup of important information related to the configuration of your applications excluding Plex.. It can then be configured to store it or either google drive or a local folder on your Ultras Slot.
Initial Setup and Configuration
Your first step is to find the full path of your home directory
pwd
Something like this will be outputted be sure to make a note of it :
/home1/usbdocs
then create three new folders, one may already exist this is fine
mkdir ~/lock
mkdir ~/scripts
mkdir ~/autobackup
Then you need to enter the new folder called scripts
cd ~/scripts
And create the script file
nano backup.sh
Paste the following lines into it :
#!/bin/bash
exec {lock_fd}>/home1/usbdocs/lock/BackupLock || exit 1
flock -n "$lock_fd" || { echo "ERROR: flock() failed." >&2; exit 1; }
if [ -z "$STY" ]; then exec screen -dm -S autobackup /bin/bash "$0"; fi
DATE=$(date +%Y-%m-%d-%H%M%S)
tar --exclude="$HOME"/.config/plex -czvf "$HOME"/autobackup/MyUSBbackup-$DATE.tar.gz "$HOME"/.apps "$HOME"/.config
rsync -aHAXxv --numeric-ids --info=progress2 -e "ssh -p portnumberhere" /home1/usbdocs/autobackup username@destinationip:/home/username/destonation/folder
flock -u "$lock_fd"
Save it by pressing Ctrl+X then Y then Enter
.
You will need to change the paths home1/usbdocs
to match your own home and username. You will also be required to change destinationip
to match your destination
Testing
So to test first we navigate to ~/scripts folder we made earlier
cd ~/scripts
Then we need to allow the backup.sh permissions to run
chmod +x backup.sh
And finally, run it
./backup.sh
If all went well * tar -ztvf ~/autobackup/.tar.gz should show a bunch of files starting with /home/username/.config and /home/username/.apps inside.
If the script is running and you were to rerun it, you may see an error message
“Flock Failed” this is a file lock to stop multiple downloads running and is
normal. If you are sure it isn’t running you can delete the lock file from
~/lock. You can also check the progress of the backup script, which is running
in a screen with the command screen -rd autobackup
If all is well after the test, we can automate the check via crontab
Open crontab with
crontab -e
You may have a choice of editors. We recommend Nano
Inside the crontab add a single line under everything else in the file that looks like this
0 0 */3 * * /home/usbdocs/backup.sh
Save it by pressing Ctrl+X then Y then Enter.
The script will now run every 3 days, checking for files that have changed and syncing them to the destination folder