Skip to content

Instantly share code, notes, and snippets.

@Ryex
Created May 22, 2016 21:45
Show Gist options
  • Save Ryex/a63fb689427223c0ca5210cd7d0c72e2 to your computer and use it in GitHub Desktop.
Save Ryex/a63fb689427223c0ca5210cd7d0c72e2 to your computer and use it in GitHub Desktop.
seedbox rsync automation: down load all files from a seed box or other remote server with rsync
#!/bin/bash
pid_file="/path/to/destination/rsync.pid"
rsync_pid=$(cat $pid_file)
if [[ -z "${rsync_pid// }" ]] ; then
echo "[$(date)] no PID for seedbox rsync, exiting"
exit
else
echo "[$(date)] killing seedbox rsync PID: ($rsync_pid)"
kill -9 $rsync_pid
fi
#!/bin/bash
bandwidth_limit="100" #in kB/s
sync_user="public"
sync_remote_path="user@remotehost:path/to/folder"
sync_local_path="/path/to/desination"
sync_partial_path="/path/to/destination/.rsync_partial"
lock_file="/path/to/desination/rsyncjob.lock"
pid_file="/path/to/destination/rsync.pid"
rsync_options="-crzP --compress-level=9 -hh --chmod=D0775,F0775,+X --partial-dir=$sync_partial_dir --stats"
rsync_log_file="/path/to/desination/rsync.log.$(date +%Y%m%d%H%M%S)"
rsync_log_format="%i %n%L %''l (%''b)"
rsync_pid=""
limit=""
while getopts l opt; do
case $opt in
l)
echo "[$(date)] Limiting bandwidth to $bandwidth_limit kb/s"
limit="--bwlimit=$bandwidth_limit"
;;
esac
done
shift $((OPTIND - 1))
rsync_log_options="--log-file=$rsync_log_file --log-file-format=\"$rsync_log_format\""
rsync_cmd="rsync $rsync_options $rsync_log_options $limit $sync_remote_path $sync_local_path & echo \$! > $pid_file"
run_rsync () {
echo "[$(date)] Running rsync as $sync_user"
echo "[$(date)] Command: $rsync_cmd"
su -c "$rsync_cmd" - $sync_user
sleep 1
rsync_pid=$(cat $pid_file)
}
create_lock () {
echo "[$(date)] Creating lockfile"
touch $lock_file
}
remove_lock () {
echo "[$(date)] Removing lockfile"
rm -f $lock_file
rm -f $pid_file
}
if [ -e /home/public/rsyncjob.lock ]
then
echo "[$(date)] Rsync job already running...\nexiting"
exit
fi
create_lock
run_rsync
while [ -e "/proc/$rsync_pid" ]; do sleep 0.1; done
echo "[$(date)] Finished rsync"
remove_lock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment