Skip to content

Instantly share code, notes, and snippets.

@skarllot
Last active December 17, 2015 15:09
Show Gist options
  • Save skarllot/5629228 to your computer and use it in GitHub Desktop.
Save skarllot/5629228 to your computer and use it in GitHub Desktop.
Script to sync sysvol (and netlogon) folder to Samba 4.
#!/bin/bash
#
# Copyright (C) Matthieu Patou <mat at matws.net> 2011-2012
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
export KRB5CCNAME=/tmp/sync.$$
LIST_DC="server01 server02"
contacteddc=0
me=`hostname -s`
domain=`hostname -d`
PROVISIONDIR=/usr/local/domain/$domain/
STAGING=$PROVISIONDIR/staging
SYSVOL=$PROVISIONDIR/sysvol
# Add some randomness
sleep $(( $RANDOM % 50 ))
rm -rf $STAGING/*
kinit -k -t /etc/krb5.keytab `hostname -s | tr "[:lower:]" "[:upper:]"`\$
date +%s >$SYSVOL/.flag
for dc in $LIST_DC; do
if [ $dc == $me ]; then
continue
fi
# can we ping this dc ?
ping -c 2 $dc >/dev/null 2>&1
if [ $? -ne 0 ]; then
continue
fi
[ ! -f $STAGING/.lastts.$dc ]&& touch $STAGING/.lastts.$dc
dc_account_name=`echo $me | tr "[:lower:]" "[:upper:]"`
rsync -X -u -a $dc_account_name\$@${dc}.${domain}:$SYSVOL $STAGING
[ $? -ne 0 ]&& continue
contacteddc=$(($contacteddc + 1))
if [ -f $STAGING/sysvol/.flag ]; then
# A sync is running on another dc ...
now=`date +%s`
ts=`cat $SYSVOL/.flag`
delta=$(( $now - $ts ))
if [ $delta -gt 300 ]; then
echo -ne "A sync is already running on $dc for more than 5 minutes, you should "
echo "check and if needed remove the following file: $SYSVOL/.flag"
else
sleep $(( $RANDOM % 50 + 20))
rm -rf $STAGING/*
rsync -X -u -a $dc_account_name\$@${dc}.${domain}:$SYSVOL $STAGING
if [ -f $STAGING/sysvol/.flag ]; then
#echo "Sync is running"
rm -f $KRB5CCNAME
[ -f $SYSVOL/.flag ]&& rm $SYSVOL/.flag
exit 0
fi
fi
fi
done
if [ $contacteddc -eq 0 ]; then
if [ -f $STAGING/sysvol/.flag ]; then
rm $STATING/sysvol/.flag
exit 0
fi
fi
csync $STAGING $SYSVOL
cd $STAGING
find . >/tmp/listfiles.$$
cd /
while read l; do
nb=`getfattr -d -m "" "$SYSVOL/$l" 2>/dev/null |wc -l`
nb2=`getfattr -d -m "" "$STAGING/sysvol/$l" 2>/dev/null |wc -l`
if [ ! -L "$STAGING/sysvol/$l" -a $nb -eq 0 -a $nb2 -ne 0 ]; then
echo "setting acls on $l"
getfattr -d -m "" "$STAGING/sysvol/$l" 2>/dev/null | sed 's at staging/@@' |setfattr --restore=- 2>/dev/null
fi
done </tmp/listfiles.$$
rm /tmp/listfiles.$$
rm -f $KRB5CCNAME
[ -f $SYSVOL/.flag ]&& rm $SYSVOL/.flag
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment