Last active
January 15, 2016 20:17
-
-
Save AaronGhent/31d744ab887b867b6448 to your computer and use it in GitHub Desktop.
Sync Latest Downloads From A Remote Server (Ubuntu 14.04)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# requirements: sudo apt-get install lftp | |
# add to crontab -e | |
# */15 * * * * sh ~/bin/sync.sh | while read line; do echo "[$(date -R)] $line" >> ~/.log/sync_cron.log; done; 2>&1 | |
server="<<SSH HOST>>" | |
remote_dir=files/complete | |
local_dir=/media/dump/ | |
log_file=~/.log/sync.log | |
username=$LOGNAME | |
connections=3 | |
days_old=now-3days | |
LOCKFILE="/var/lock/`basename $0`" | |
LOCKFD=8 | |
_start_ssh() { eval `ssh-agent -s` > /dev/null; ssh-add; } | |
_cleanup_ssh() { eval `ssh-agent -k`; } | |
_cleanup() { _cleanup_ssh; } | |
_lock() { flock -$1 $LOCKFD; } | |
_cleanup_lock() { _lock u; _lock xn && { rm -f $LOCKFILE; _cleanup; }; trap - EXIT HUP INT QUIT TERM; } | |
_prepare_locking() { eval "exec $LOCKFD> \"$LOCKFILE\""; trap _cleanup_lock EXIT HUP INT QUIT TERM; } | |
_prepare_locking | |
_lock xn || exit 1; | |
_start_ssh | |
# sync with server | |
lftp sftp://$server <<EOF | |
mirror -c -P${connections} --newer-than=${days_old} --no-empty-dirs --log='${log_file}' '${remote_dir}' '${local_dir}' -p --no-umask | |
quit | |
EOF | |
# set file permissions | |
sudo chmod 775 -R ${local_dir} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment