This is a little bash script to able to make an offline backup from a specific folder to a plugged in disk(s).
For my specific case it s a solution to be able to make backup(s) by the end-user from a Raspberry Pi used as an Owncloud server.
In ideal case, script should be scheduled by root's cron to run at every minute. If no any disk was attached or the disk is not a backup disk or not run by root, the script will quit.
If disk (USB drive) was attached in time when script runs, the script checks that a specific file, described in script as FILE variable, is available on the visible disk(s)'s root.
If Yes, backup process will be started and depends on script's COMPRESS variable value, copy the SOURCE (path to backup) to the BACKUP_FOLDER aka mounted disk or compress SOURCE and then copy compressed file to BACKUP_FOLDER.
If no, script will check the next available disk. If no any other available disk, script will exit as no any backup disk
At the end as an indicator, power led of Raspberry Pi starts to blinking until you didn't remove the disk. If you have multiple backup disks, will blink until the lastly attached backup disk was removed.
Before you start to implement the solution (no matter that it is an automatic or manual), modify the script's variables in backup.sh to your needs. Defaults are fine for an Raspberry Pi Owncloud setup.
#VARIABLES
NOW=$(date +"%Y.%m.%d_%H_%M") # Actual time. It is used for backup time-stamp.
FILE="backup.disk" # Backup disk should contain this file on the root to be able to detected by script as destination aka backup disk.
SOURCE="/var/www/owncloud/data" # Source of backup. This folder will be copied to BACKUP_FOLDER.
BACKUP_FOLDER="/mnt/backup" # Destination of backup. This folder will contain the same files and folders as SOURCE.
RUNNING="${0}_RUNNING" # This is the lock file. If file is exist, means script is running (prevent concurrent running).
COMPRESS="" # If vale is "YES" (case-sensitive), SOURCE will be compressed and copied to BACKUP_FOLDER instead of just copying.
It is not so hard to implement it by hand, simply copy backup.sh to any place in your Raspberry Pi and schedule it by root's cron like:
pi@raspberrypi:~ $ sudo su
root@raspberrypi:/home/pi# crontab -e
- Add the following line (runs at every minute):
* * * * * /bin/timeout -s 2 345600 /bin/bash /root/backup.sh >/dev/null 2>&1
- Do not left to modify the absolute path of your script like:
/root/backup.sh
. It depends on you, where you put previously the script.
The easiest way is to SSH in to your Raspberry Pi and execute the following command as root:
apt-get install ansible git -y && cd /tmp && git clone https://github.com/ch-e-mistry/rpi-owncloud-backup.git && ansible-playbook ./rpi-owncloud-backup/backup.yaml
It will install git and ansible. If it was done, clone this repository to /tmp
and run the ansible playbook.
Of course if you have a "central" machine which manages your Raspberry Pi devices, you can run from it as well (or from AWX).
- Implemented script in your Raspberry Pi
- An NTFS formatted disk
- A specific file on this disk's root, described by FILE variable in the script (by default: backup.disk) like:
Check this youtube video:
At the end you should have this / these files based on COMPRESS variable's value in the script.
MIT
Peter Mikaczo - [email protected]