# Is it possible to pause and resume data copy dynamically on pt-online-schema-change?

**URL:** <https://forums.percona.com/t/is-it-possible-to-pause-and-resume-data-copy-dynamically-on-pt-online-schema-change/5163>\
**Category:** Percona Toolkit\
**Created:** [October 21, 2016, 2:12am UTC](https://forums.percona.com/t/is-it-possible-to-pause-and-resume-data-copy-dynamically-on-pt-online-schema-change/5163 "2016-10-21T02:12:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![RyanE](https://avatars.discourse-cdn.com/v4/letter/r/8e7dd6/32.png) [@RyanE](https://forums.percona.com/u/RyanE)\
**Post date:** [October 21, 2016, 2:12am UTC](https://forums.percona.com/t/is-it-possible-to-pause-and-resume-data-copy-dynamically-on-pt-online-schema-change/5163/1 "2016-10-21T02:12:05Z")

</div>

Hello.

When there is a replication lag on slaves, pt-online-schema-change can pause copying data.

In the same way, is it possible to pause and resume copying them dynamically?

Below code is simple and works when I create pause\_please.txt file.  
I am wondering if I lose data while pausing it.

[B]package NibbleIterator;  
sub next()

NIBBLE:[/B]  
while ( $self-\>{have\_rows} || $self-\>\_next\_boundaries() ) {  
if ( !$self-\>{have\_rows} ) {  
$self-\>{nibbleno}++;  
PTDEBUG && \_d(‘Nibble:’, $self-\>{nibble\_sth}-\>{Statement}, ‘params:’,  
join(', ', (@{$self-\>{lower} || }, @{$self-\>{upper} || })));  
if ( my $callback = $self-\>{callbacks}-\>{exec\_nibble} ) {  
$self-\>{have\_rows} = $callback-\>(%callback\_args);  
}  
else {  
$self-\>{nibble\_sth}-\>execute(@{$self-\>{lower}}, @{$self-\>{upper}});  
$self-\>{have\_rows} = $self-\>{nibble\_sth}-\>rows();  
}  
PTDEBUG && \_d($self-\>{have\_rows}, ‘rows in nibble’, $self-\>{nibbleno});  
}

if ( $self-\>{have\_rows} ) {  
my $row = $self-\>{nibble\_sth}-\>fetchrow\_arrayref();  
if ( $row ) {  
$self-\>{rowno}++;  
PTDEBUG && \_d(‘Row’, $self-\>{rowno}, ‘in nibble’,$self-\>{nibbleno});  
return [@$row];  
}  
}

PTDEBUG && \_d(‘No rows in nibble or nibble skipped’);  
if ( my $callback = $self-\>{callbacks}-\>{after\_nibble} ) {  
$callback-\>(%callback\_args);  
}  
$self-\>{rowno} = 0;  
$self-\>{have\_rows} = 0;

[COLOR=#0000FF] [B] # =============================================  
while(-f “pause\_please.txt”) {  
print “Im sleeping\n”;  
my $dbh = $self-\>{Cxn}-\>dbh();  
if ( !$dbh || !$dbh-\>ping() ) {  
eval { $dbh = $self-\>{Cxn}-\>connect() }; # connect or die trying  
if ( $EVAL\_ERROR ) {  
#$oktorun = 0; # flag for cleanup tasks  
chomp $EVAL\_ERROR;  
die "Lost connection to " . $self-\>{Cxn}-\>name() . " while waiting for "  
. “replica lag ($EVAL\_ERROR)\n”;  
}  
}  
$dbh-\>do(“SELECT ‘pt-online-schema-change keepalive’”);  
sleep(60);  
}

# =============================================[/B]

}

PTDEBUG && \_d(‘Done nibbling’);  
if ( my $callback = $self-\>{callbacks}-\>{done} ) {  
$callback-\>(%callback\_args);  
}

---

<div class="post-metadata">

**Author:** ![carlos.salguero](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/carlos.salguero/32/20_2.png) [@carlos.salguero](https://forums.percona.com/u/carlos.salguero)\
**Post date:** [October 27, 2016, 9:00am UTC](https://forums.percona.com/t/is-it-possible-to-pause-and-resume-data-copy-dynamically-on-pt-online-schema-change/5163/2 "2016-10-27T09:00:40Z")

</div>

As I’ve mentioned on Launchpad, if you create a pull request in github, I can review and merge your fix. Please remember to include a unit test.  
You can find more information, help and guidelines in [url][https://github.com/percona/percona-toolkit/blob/2.2/CONTRIBUTE.md[/url]](https://github.com/percona/percona-toolkit/blob/2.2/CONTRIBUTE.md%5B/url%5D).
