Photo by Markus Spiske
Considering an NFS share is mounted at /nfs_share/
, a work space is located at /home/me/workspace/
, a directory /nfs_share/backups/
exists, and a file at /nfs_share/backups/backup_workspace.sh
contains the following bash script, the cron job below will use rsync and tar to create a copy of a work space, compress it, and creat daily copies of it in another directory.
backup_workspace.sh
#!/usr/bin/env bash
# backup workspace to NFS share
SRC="~/workspace/"
SYNC="/nfs_share/backups/workspace"
DEST="/nfs_share/backups/workspace_backups/"
FILE="workspace-$(date +%Y-%m-%d).tar.gz"
# Ensure directories exist
mkdir -p $DEST
# Record start time by epoch second
START=$(date '+%s')
if ! rsync -av --delete $SRC $SYNC ; then
STATUS="rsync failed"
elif ! tar -czf $FILE $SYNC ; then
STATUS="tar failed"
elif ! mv $FILE $DEST ; then
STATUS="mv failed"
else
STATUS="success: size=$(stat -c%s $DEST$FILE) duration=$((`date '+%s'` - $START))"
fi
# Log to system log; handle this using syslog(8)
logger -t backup "$STATUS"
echo "$STATUS"
Run it as a cron job at 01:00 by editing sudo crontab -e
# Backup workspace
0 1 * * * bash /nfs_share/backups/backup_workspace.sh