Script to backup a directory in unix - rsync with crontab

21 views
Skip to first unread message

dilawar....@gmail.com

unread,
Apr 19, 2014, 1:42:43 AM4/19/14
to wncc...@googlegroups.com
<html>
<head>
<meta name="generator" content="text2mime-markdown0.1">
<style>
code { font-family: 'Andale Mono', 'Lucida Console', 'Bitstream Vera Sans Mono', 'Courier New', monospace; }
pre { border-left: 20px solid #ddd; margin-left: 10px; padding-left: 5px; }
</style>
</head>
<body>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta name="generator" content="pandoc" />
<title></title>
<style type="text/css">code{white-space: pre;}</style>
</head>
<body>
<p>Utility <code>rsync</code> is a versatile tool for creating backups. A simple script which back-up dir1 to dir2 is following. It can also be found <a href="http://dilawar.github.com/Scripts/backup.sh">here</a>.</p>
<pre><code> #!/bin/bash
# Original script : https://wiki.archlinux.org/index.php/Full_System_Backup_with_rsync
# Modified by : Dilawar Singh &lt;dila...@ncbs.res.in&gt;

if [ $# -lt 2 ]; then
echo &quot;No source and destination defined. Usage: $0 source destination&quot; &gt;&amp;2
exit 1
elif [ $# -gt 2 ]; then
echo &quot;Too many arguments. Usage: $0 source destination&quot; &gt;&amp;2
exit 1
elif [ ! -d &quot;$1&quot; ]; then
echo &quot;Invalid path: $1&quot; &gt;&amp;2
exit 1
if [ ! -d &quot;$1&quot; ]; then
echo &quot;Invalid path: $2&quot; &gt;&amp;2
exit 1
fi
elif [ ! -w &quot;$2&quot; ]; then
echo &quot;Directory not writable: $2&quot; &gt;&amp;2
exit 1
fi

# Enable it if you are backing up in /media mount point.
#case &quot;$2&quot; in
# &quot;/mnt&quot;) ;;
# &quot;/mnt/&quot;*) ;;
# &quot;/media&quot;) ;;
# &quot;/media/&quot;*) ;;
# *) echo &quot;Destination not allowed.&quot; &gt;&amp;2
# exit 1
# ;;
#esac

START=$(date +%s)
rsync -azv --progress $1 $2
FINISH=$(date +%s)
echo &quot;total time: $(( ($FINISH-$START) / 60 )) minutes, $(( ($FINISH-$START) % 60 )) seconds&quot;
touch $2/&quot;Backup from $(date &#39;+%A, %d %B %Y, %T&#39;)&quot;
notify-send &quot;Done backing up $1 to $2&quot;</code></pre>
<p>Let's name this script <code>backup.sh</code> and make it executable (chmod +x backup.sh). Save it somewhere on system $PATH (e.g. /usr/local/bin). From command line:</p>
<pre><code> $ backup.sh /home/dilawar/Work /home/dilawar/Bhandar/Backup</code></pre>
<p>Usually one needs to repeatedly run this command every two hours or so to create backup. One can use <code>crontab</code> to automate this task. For example, if I want to run this script every two hours every-day. I do the following in terminal.</p>
<pre><code> $ crontab -e</code></pre>
<p>This will open an editor, write/append the following line to the opened file and close it.</p>
<pre><code> 0 0-23/2 * * * /usr/local/bin/backup.sh /home/dilawar/Work /home/dilawar/Bhandar/Backup</code></pre>
<p>There you go. Every two hours, you will see a small notification on the right-top corner of your screen that backup was successful.</p>
<p>NOTE: When copying using rsync, be careful about ending paths with <code>/</code>. If a path is ended with <code>/</code> e.g. <code>a/</code> it means copy everything under a, while <code>a</code> means copy directory a. Effectively, you don't get <code>a</code> in backup in the former (only its subdirectories).</p>
<pre><code> Dilawar</code></pre>
</body>
</html>
</body></html>
html-markdown-alternative.html
Reply all
Reply to author
Forward
0 new messages