Rsync useful commands

rsync command

rsync -aczvAXHhSW --no-compress --progress --info=progress2 $FROM_DIR $TO_DIR

Rsync - Sync some newest directories only from HOST_REMOTE to HOST_LOCAL

- Make sure you can run SSH login from $HOST_LOCAL to $HOST_REMOTE without password by using RSA keys
https://tuan.nguoianphu.com/SSH-login-without-password-by-using-RSA-keys

- Run this script at HOST_LOCAL

# Number of newest directories to sync
LATEST_DIRS=10

# Workspace for Jenkins, change or remove it if you don't need
WORKSPACE="$HOME"

# Hosts
HOST_LOCAL="test1.com"
DEST_DIR="/opt/destination/local"

HOST_REMOTE="test2.com"
SC_DIR="/opt/source/remote"

# Rsync options. Exclude tests and logs directories
RSYNC_OPS="-Whav --info=progress2 --exclude tests --exclude logs"

# Create a comand to get newest directories on REMOTE and write into a file
echo "Command to get ${LATEST_DIRS} newest directotries on $HOST_REMOTE server"
echo -ne $(eval "echo ls ${SC_DIR}/* -td") > $WORKSPACE/COMMAND
echo -ne ' | ' >> $WORKSPACE/COMMAND
echo -ne $(eval "echo grep -v tests") >> $WORKSPACE/COMMAND
echo -ne ' | ' >> $WORKSPACE/COMMAND
echo -ne $(eval "echo grep -v logs") >> $WORKSPACE/COMMAND
echo -ne ' | ' >> $WORKSPACE/COMMAND
echo -ne $(eval "echo head -${LATEST_DIRS}") >> $WORKSPACE/COMMAND

# Run the command to get list of directories
COMMAND_EVAL=$(cat $WORKSPACE/COMMAND)
echo $(ssh ${HOST_REMOTE} "${COMMAND_EVAL}") > $WORKSPACE/NEW_DIRS

# Now run rsync by above list of directories
echo "Sync ${LATEST_DIRS} newest directotries from $HOST_REMOTE to $HOST_LOCAL"
for some_dir in $( cat $WORKSPACE/NEW_DIRS ); do
echo "INFO SYNC $some_dir"
rsync ${RSYNC_OPS} -e ssh ${HOST_REMOTE}:$some_dir ${DEST_DIR}/
done

Loading