Bidirectional Rsync/Unison based SysVol replication workaround: Difference between revisions

From SambaWiki
m (→‎FAQ: Cleanup to formatting to make it look nicer)
 
(2 intermediate revisions by the same user not shown)
Line 58: Line 58:


==== Setup Unison defaults running parameters ====
==== Setup Unison defaults running parameters ====
Please run the following on DC1
To check which version of unison you have installed, you can run the following command:


unison -version

Please run one of the following on DC1 depending on what version of unison you have installed.

Before Unison 2.53.0:
install -o root -g root -m 0750 -d /root/.unison
install -o root -g root -m 0750 -d /root/.unison
cat << EOF > /root/.unison/default.prf
cat << EOF > /root/.unison/default.prf
Line 89: Line 94:
copymax = 1
copymax = 1
logfile = /var/log/sysvol-sync.log
logfile = /var/log/sysvol-sync.log
EOF

From Unison 2.53.0:

install -o root -g root -m 0750 -d /root/.unison
cat << EOF > /root/.unison/default.prf
# Unison preferences file
# Roots of the synchronization
#
# copymax & maxthreads params were set to 1 for easier troubleshooting.
# Have to experiment to see if they can be increased again.
root = /var/lib/samba
# Note that 2 x / behind DC2, it is required
root = ssh://root@DC2//var/lib/samba
#
# Paths to synchronize
path = sysvol
#
#ignore = Path stats ## ignores /var/www/stats
acl=true
auto=true
batch=true
perms=0
rsync=true
maxthreads=1
retry=3
confirmbigdeletes=false
servercmd=/usr/bin/unison
copythreshold=0
copymax = 1
logfile = /var/log/sysvol-sync.log
xattrs=true
EOF
EOF


Line 128: Line 165:


* What to do if I've more than two DC's?
* What to do if I've more than two DC's?
** In Theory, We would just make more cron jobs on DC1 and the complete sync will be perform next sync to all server. Follow below steps:
** In Theory, we would just make more cron jobs on DC1 and the complete sync will be perform next sync to all server. Follow below steps:
:# Copy '''default.prf''' to another file called, eg: '''root1.prf'''
:# Copy '''default.prf''' to another file called, eg: '''DC2.prf'''
:# Open '''DC2.prf''':
:# Open '''root1.prf''' and named '''root = ...''' line to '''root1 = ...''' and save the file
:#* change <code>logfile = /var/log/sysvol-sync.log</code> line to <code>logfile = /var/log/sysvol-sync-DC2.log</code>
:# Repeat step 1 and 2 for all DCs
:#* save the file
:# Copy '''default.prf''' to another file called, eg: '''DC3.prf'''
:# Open '''DC3.prf''':
:#* change <code>root = ssh://root@DC2//var/lib/samba</code> line to <code>root = ssh://root@DC3//var/lib/samba</code>
:#* change <code>logfile = /var/log/sysvol-sync.log</code> line to <code>logfile = /var/log/sysvol-sync-DC3.log</code>
:#* save the file
:# Repeat step 3 and 4 for all DCs
:# Change cronjob to:
:# Change cronjob to:
* * * * * /usr/bin/unison root1 -silent
*/5 * * * * /usr/bin/unison DC2 -silent
* * * * * /usr/bin/unison root2 -silent
1-59/5 * * * * /usr/bin/unison DC3 -silent
2-59/5 * * * * /usr/bin/unison DC4 -silent
and so on.
and so on. This cron syntax will run of jobs every 5 minutes, but each is offset by 1 minute so they do not all start at the same time.

{{Imbox
| type = note
| text = Explanation of this cron syntax: "An <code>*</code> in the minute field is the same as <code>0-59/1</code> where <code>0-59</code> is the range and <code>1</code> is the step. The command will run at the first minute in the range (0), then at all successive minutes that are distant from the first by step (1), until the last (59)."
<br>See https://stackoverflow.com/a/19204734 and https://serverfault.com/a/429397
}}




Line 146: Line 197:
*** Comment out the "copyprog" entry with a #
*** Comment out the "copyprog" entry with a #
*** Add the following lines to your .prf file (requires Unison version 2.53.0 or newer)
*** Add the following lines to your .prf file (requires Unison version 2.53.0 or newer)
acl=true
**** <code>acl=true</code>
xattrs=true
**** <code>xattrs=true</code>
** Long answer:
** Long answer:
*** The original reason for using `'''copyprog = /usr/bin/rsync...'''` in the unison preferences file was to support syncing extended attributes (xattrs) and ACLs, which are required for the sysvol to function properly. These features were not natively supported by unison when this guide was first written, so the `'''copyprog'''` feature was used as a workaround. However, support for syncing extended attributes and ACLs was added to unison in version 2.53.0, so this workaround is no longer needed.
*** The original reason for using <code>copyprog = /usr/bin/rsync...</code> in the unison preferences file was to support syncing extended attributes (xattrs) and ACLs, which are required for the sysvol to function properly. These features were not natively supported by unison when this guide was first written, so the `'''copyprog'''` feature was used as a workaround. However, support for syncing extended attributes and ACLs was added to unison in version 2.53.0, so this workaround is no longer needed.
*** As the time of writing this, the unison devs will be removing support for the `'''copyprog'''` feature entirely, so this feature should be considered deprecated. See https://github.com/bcpierce00/unison/issues/1060 and https://github.com/bcpierce00/unison/issues/871
*** At the time of writing this, the unison devs plan to remove support for the `'''copyprog'''` feature entirely, so this feature should be considered deprecated. See https://github.com/bcpierce00/unison/issues/1060 and https://github.com/bcpierce00/unison/issues/871


----
----

Latest revision as of 17:26, 19 October 2024

Introduction

Samba AD currently doesn't provide support for SysVol replication. To achieve this important feature in a Multi-DC environment, until it's implemented, workarounds are necessary to keep it in sync. This HowTo provides a basic workaround solution based on rsync and unison.

Information on unison + rsync replication

This HowTo describes a solution for SysVol replication, that is based on rsync and unison. As Compare to the rsync method, it is bidirectional. This howto only covers a two DC setup.

It has the following advantages:

  • Quick setup
  • Configuration is very easy
  • Can work with windows (Please add in)

We will use rsync through a SSH tunnel.

Setup the SysVol replication

Some assumptions:

You are running all commands as root.
rsync location  /usr/bin/rsync
sysvol is located at /var/lib/samba/sysvol on both DC1 and DC2
unison location /usr/bin/unison
The first DC is DC1
The second DC is DC2
sysvolsync log location /var/log/sysvol-sync.log

Change the paths if your setup is different.

Setup on the Domain Controller with the PDC Emulator FSMO role

  • Install rsync by using your package manager or compile from source. Make sure, that you use a version that supports extended ACLs!
  • You don't need to setup the rsync server.
  • Install unison by using your package manager or compile from source. (On Gentoo you need to do eselect unison to create the link)

Creating SSH Public Key and ssh-copy to DC2

ssh-keygen -t rsa
ssh-copy-id -i ~/.ssh/id_rsa.pub root@DC2

You can try to access DC2 via ssh

ssh DC2

Setup ssh Control

If the remote system enforces rate limits on incoming ssh connections, unison will fail if you try to run it this way. So we create the first ssh connection as a controlpath file in the location specified, all subsequent connections will reuse on the first connection.

mkdir ~/.ssh/ctl
cat << EOF > ~/.ssh/config
Host *
ControlMaster auto
ControlPath ~/.ssh/ctl/%h_%p_%r
ControlPersist 1
EOF

Setup Sysvolsync Log files

Do the following on DC1 so that you can check what happens during the sync. Please include this file into logrotate as the log size is not controlled.

touch /var/log/sysvol-sync.log
chmod 640 /var/log/sysvol-sync.log

Setup Unison defaults running parameters

To check which version of unison you have installed, you can run the following command:

unison -version

Please run one of the following on DC1 depending on what version of unison you have installed.

Before Unison 2.53.0:

install -o root -g root -m 0750 -d /root/.unison
cat << EOF > /root/.unison/default.prf
# Unison preferences file
# Roots of the synchronization
#
# copymax & maxthreads params were set to 1 for easier troubleshooting.
# Have to experiment to see if they can be increased again.
root = /var/lib/samba
# Note that 2 x / behind DC2, it is required
root = ssh://root@DC2//var/lib/samba 
# 
# Paths to synchronize
path = sysvol
#
#ignore = Path stats    ## ignores /var/www/stats
auto=true
batch=true
perms=0
rsync=true
maxthreads=1
retry=3
confirmbigdeletes=false
servercmd=/usr/bin/unison
copythreshold=0
copyprog = /usr/bin/rsync -XAavz --rsh='ssh -p 22' --inplace --compress
copyprogrest = /usr/bin/rsync -XAavz --rsh='ssh -p 22' --partial --inplace --compress
#copyquoterem = true
copymax = 1
logfile = /var/log/sysvol-sync.log
EOF

From Unison 2.53.0:

install -o root -g root -m 0750 -d /root/.unison
cat << EOF > /root/.unison/default.prf
# Unison preferences file
# Roots of the synchronization
#
# copymax & maxthreads params were set to 1 for easier troubleshooting.
# Have to experiment to see if they can be increased again.
root = /var/lib/samba
# Note that 2 x / behind DC2, it is required
root = ssh://root@DC2//var/lib/samba 
# 
# Paths to synchronize
path = sysvol
#
#ignore = Path stats    ## ignores /var/www/stats
acl=true
auto=true
batch=true
perms=0
rsync=true
maxthreads=1
retry=3
confirmbigdeletes=false
servercmd=/usr/bin/unison
copythreshold=0
copymax = 1
logfile = /var/log/sysvol-sync.log
xattrs=true
EOF

Setup SysVol on DC2

  • On DC2 Install rsync by using your package manager or compile from source. Make sure, that you use a version that supports extended ACLs!
  • On DC2 Install unison by using your package manager or compile from source. (On Gentoo you need to do eselect unison to create the link)
  • Make sure, that you have identical IDs of built-in groups on all DCs.


1st Trial

You now use rsync to create the directory structure with extended attributes Then the unison setup will only copy the extended attributes on files.


Please make a backup of your sysvol, just in case, this is because there is no dry-run

/usr/bin/rsync -XAavz --log-file /var/log/sysvol-sync.log --delete-after -f"+ */" -f"- *"  /var/lib/samba/sysvol root@DC2:/var/lib/samba  &&  /usr/bin/unison
Note: The path on DC2 is just /var/lib/samba which is different from DC1, it is by design, there is nothing wrong!

Add to Crontab on DC1

On DC1 run the following:

crontab -e 
*/5 * * * * /usr/bin/unison -silent

When you try to resync the folder

Warning: Please follow the steps below OR you can end up with an empty sysvol folder.
  1. Disable Cron on DC1, like Add a "#" on the line with crontab -e
  2. Check if rsync or unison are currently running in ps -aux if yes, wait for it to finish OR kill it (if it is zombie)
  3. Remove the hash files on both DC1 and DC2 on /root/.unison
  4. Now check your sysvol and resync
  5. Confirm that everything is ok again
  6. Re-enable the cron on DC1 again

FAQ

  • How can I do this on windows?
    • I don't have an answer, please post on the mailing list


  • What to do if I've more than two DC's?
    • In Theory, we would just make more cron jobs on DC1 and the complete sync will be perform next sync to all server. Follow below steps:
  1. Copy default.prf to another file called, eg: DC2.prf
  2. Open DC2.prf:
    • change logfile = /var/log/sysvol-sync.log line to logfile = /var/log/sysvol-sync-DC2.log
    • save the file
  3. Copy default.prf to another file called, eg: DC3.prf
  4. Open DC3.prf:
  5. Repeat step 3 and 4 for all DCs
  6. Change cronjob to:
  */5 * * * *  /usr/bin/unison DC2 -silent
  1-59/5 * * * *  /usr/bin/unison DC3 -silent
  2-59/5 * * * *  /usr/bin/unison DC4 -silent

and so on. This cron syntax will run of jobs every 5 minutes, but each is offset by 1 minute so they do not all start at the same time.


  • Why can't I simply use a distributed filesystem like GlusterFS, Lustre, etc. for SysVol?
    • A cluster file system with Samba requires CTDB to be able to do it safely. And CTDB and AD DC are incompatible.


  • Error: External copy program did not create target file (or bad length)?
    • Quick reply:
      • Comment out the "copyprog" entry with a #
      • Add the following lines to your .prf file (requires Unison version 2.53.0 or newer)
        • acl=true
        • xattrs=true
    • Long answer:
      • The original reason for using copyprog = /usr/bin/rsync... in the unison preferences file was to support syncing extended attributes (xattrs) and ACLs, which are required for the sysvol to function properly. These features were not natively supported by unison when this guide was first written, so the `copyprog` feature was used as a workaround. However, support for syncing extended attributes and ACLs was added to unison in version 2.53.0, so this workaround is no longer needed.
      • At the time of writing this, the unison devs plan to remove support for the `copyprog` feature entirely, so this feature should be considered deprecated. See https://github.com/bcpierce00/unison/issues/1060 and https://github.com/bcpierce00/unison/issues/871