Vagrant: Ruby script to return name of last created file
We have vagrant file with trigger like
DB_NAME="mydb"
TIME=(Time.now.strftime("%Y%m%d%H%M%S"))
SQL_BACKUPS=(Dir["./config/schema/*_#{DB_NAME}.sql"])
config.trigger.before [:destroy, :provision] do |trigger|
trigger.info = "Dumping database to /vagrant/config/schema/#{TIME}_#{DB_NAME}.sql"
trigger.run_remote = {inline: "mysqldump --add-drop-table -u #{DB_USERNAME} -p#{DB_PASSWORD} #{DB_NAME} > /vagrant/config/schema/#{TIME}_#{DB_NAME}.sql"}
end
in /vagrant/config/schema/
we have backup files like:
20181116160919_mydb.sql
How to find in ruby all files like *_mydb.sql
and / or return name of latest one created?
We want automatize db backup on destroy, provision & up.
EDIT:
SQL_BACKUPS=(Dir["./config/schema/*_#{DB_NAME}.sql"]).sort
SQL_BACKUPS.reverse.each do |filename|
puts "#{filename}"
end
return lists with sql files
ps, I don't have Experience with Ruby.
ruby vagrant
|
show 3 more comments
We have vagrant file with trigger like
DB_NAME="mydb"
TIME=(Time.now.strftime("%Y%m%d%H%M%S"))
SQL_BACKUPS=(Dir["./config/schema/*_#{DB_NAME}.sql"])
config.trigger.before [:destroy, :provision] do |trigger|
trigger.info = "Dumping database to /vagrant/config/schema/#{TIME}_#{DB_NAME}.sql"
trigger.run_remote = {inline: "mysqldump --add-drop-table -u #{DB_USERNAME} -p#{DB_PASSWORD} #{DB_NAME} > /vagrant/config/schema/#{TIME}_#{DB_NAME}.sql"}
end
in /vagrant/config/schema/
we have backup files like:
20181116160919_mydb.sql
How to find in ruby all files like *_mydb.sql
and / or return name of latest one created?
We want automatize db backup on destroy, provision & up.
EDIT:
SQL_BACKUPS=(Dir["./config/schema/*_#{DB_NAME}.sql"]).sort
SQL_BACKUPS.reverse.each do |filename|
puts "#{filename}"
end
return lists with sql files
ps, I don't have Experience with Ruby.
ruby vagrant
What are the current results you are seeing?
– Matt Schuchard
Nov 16 '18 at 15:39
1
I hope you're absolutely certain that the things you're interpolating in those strings can never, ever contain spaces or characters that might cause your shell to freak out.
– tadman
Nov 16 '18 at 15:50
@MattSchuchard return array with sql files.
– Salines
Nov 16 '18 at 15:53
1
@tadman The sql file names are predefined.
– Salines
Nov 16 '18 at 16:01
1
are you looking for.sort.last
?
– Stefan
Nov 16 '18 at 16:39
|
show 3 more comments
We have vagrant file with trigger like
DB_NAME="mydb"
TIME=(Time.now.strftime("%Y%m%d%H%M%S"))
SQL_BACKUPS=(Dir["./config/schema/*_#{DB_NAME}.sql"])
config.trigger.before [:destroy, :provision] do |trigger|
trigger.info = "Dumping database to /vagrant/config/schema/#{TIME}_#{DB_NAME}.sql"
trigger.run_remote = {inline: "mysqldump --add-drop-table -u #{DB_USERNAME} -p#{DB_PASSWORD} #{DB_NAME} > /vagrant/config/schema/#{TIME}_#{DB_NAME}.sql"}
end
in /vagrant/config/schema/
we have backup files like:
20181116160919_mydb.sql
How to find in ruby all files like *_mydb.sql
and / or return name of latest one created?
We want automatize db backup on destroy, provision & up.
EDIT:
SQL_BACKUPS=(Dir["./config/schema/*_#{DB_NAME}.sql"]).sort
SQL_BACKUPS.reverse.each do |filename|
puts "#{filename}"
end
return lists with sql files
ps, I don't have Experience with Ruby.
ruby vagrant
We have vagrant file with trigger like
DB_NAME="mydb"
TIME=(Time.now.strftime("%Y%m%d%H%M%S"))
SQL_BACKUPS=(Dir["./config/schema/*_#{DB_NAME}.sql"])
config.trigger.before [:destroy, :provision] do |trigger|
trigger.info = "Dumping database to /vagrant/config/schema/#{TIME}_#{DB_NAME}.sql"
trigger.run_remote = {inline: "mysqldump --add-drop-table -u #{DB_USERNAME} -p#{DB_PASSWORD} #{DB_NAME} > /vagrant/config/schema/#{TIME}_#{DB_NAME}.sql"}
end
in /vagrant/config/schema/
we have backup files like:
20181116160919_mydb.sql
How to find in ruby all files like *_mydb.sql
and / or return name of latest one created?
We want automatize db backup on destroy, provision & up.
EDIT:
SQL_BACKUPS=(Dir["./config/schema/*_#{DB_NAME}.sql"]).sort
SQL_BACKUPS.reverse.each do |filename|
puts "#{filename}"
end
return lists with sql files
ps, I don't have Experience with Ruby.
ruby vagrant
ruby vagrant
edited Nov 16 '18 at 16:37
asked Nov 16 '18 at 15:29
Salines
2,95931134
2,95931134
What are the current results you are seeing?
– Matt Schuchard
Nov 16 '18 at 15:39
1
I hope you're absolutely certain that the things you're interpolating in those strings can never, ever contain spaces or characters that might cause your shell to freak out.
– tadman
Nov 16 '18 at 15:50
@MattSchuchard return array with sql files.
– Salines
Nov 16 '18 at 15:53
1
@tadman The sql file names are predefined.
– Salines
Nov 16 '18 at 16:01
1
are you looking for.sort.last
?
– Stefan
Nov 16 '18 at 16:39
|
show 3 more comments
What are the current results you are seeing?
– Matt Schuchard
Nov 16 '18 at 15:39
1
I hope you're absolutely certain that the things you're interpolating in those strings can never, ever contain spaces or characters that might cause your shell to freak out.
– tadman
Nov 16 '18 at 15:50
@MattSchuchard return array with sql files.
– Salines
Nov 16 '18 at 15:53
1
@tadman The sql file names are predefined.
– Salines
Nov 16 '18 at 16:01
1
are you looking for.sort.last
?
– Stefan
Nov 16 '18 at 16:39
What are the current results you are seeing?
– Matt Schuchard
Nov 16 '18 at 15:39
What are the current results you are seeing?
– Matt Schuchard
Nov 16 '18 at 15:39
1
1
I hope you're absolutely certain that the things you're interpolating in those strings can never, ever contain spaces or characters that might cause your shell to freak out.
– tadman
Nov 16 '18 at 15:50
I hope you're absolutely certain that the things you're interpolating in those strings can never, ever contain spaces or characters that might cause your shell to freak out.
– tadman
Nov 16 '18 at 15:50
@MattSchuchard return array with sql files.
– Salines
Nov 16 '18 at 15:53
@MattSchuchard return array with sql files.
– Salines
Nov 16 '18 at 15:53
1
1
@tadman The sql file names are predefined.
– Salines
Nov 16 '18 at 16:01
@tadman The sql file names are predefined.
– Salines
Nov 16 '18 at 16:01
1
1
are you looking for
.sort.last
?– Stefan
Nov 16 '18 at 16:39
are you looking for
.sort.last
?– Stefan
Nov 16 '18 at 16:39
|
show 3 more comments
1 Answer
1
active
oldest
votes
I know you have timestamped files but, if one doesn't have a timestamp or a way to distinguish the creation times, the method below is what I used for non-timestamped files.
File::Stat
has a method ctime
(returns the creation time), So it can be done like.
SQL_BACKUPS=Dir["./config/schema/*_#{DB_NAME}.sql"].map { |f| {name: f, ctime: File::Stat.new(f).ctime } }
sorted = SQL_BACKUPS.sort_by { |f| f[:ctime] }
sorted.last.name # gives the one that was created the last.
1
PerhapsSQL_BACKUPS.max_by { |f| f[:ctime] }.name
if "or" in "and / or" in the question.
– Cary Swoveland
Nov 16 '18 at 19:36
@cary That even better. But in my case I needed the complete ordered list.
– Minato
Nov 16 '18 at 19:50
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53340845%2fvagrant-ruby-script-to-return-name-of-last-created-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I know you have timestamped files but, if one doesn't have a timestamp or a way to distinguish the creation times, the method below is what I used for non-timestamped files.
File::Stat
has a method ctime
(returns the creation time), So it can be done like.
SQL_BACKUPS=Dir["./config/schema/*_#{DB_NAME}.sql"].map { |f| {name: f, ctime: File::Stat.new(f).ctime } }
sorted = SQL_BACKUPS.sort_by { |f| f[:ctime] }
sorted.last.name # gives the one that was created the last.
1
PerhapsSQL_BACKUPS.max_by { |f| f[:ctime] }.name
if "or" in "and / or" in the question.
– Cary Swoveland
Nov 16 '18 at 19:36
@cary That even better. But in my case I needed the complete ordered list.
– Minato
Nov 16 '18 at 19:50
add a comment |
I know you have timestamped files but, if one doesn't have a timestamp or a way to distinguish the creation times, the method below is what I used for non-timestamped files.
File::Stat
has a method ctime
(returns the creation time), So it can be done like.
SQL_BACKUPS=Dir["./config/schema/*_#{DB_NAME}.sql"].map { |f| {name: f, ctime: File::Stat.new(f).ctime } }
sorted = SQL_BACKUPS.sort_by { |f| f[:ctime] }
sorted.last.name # gives the one that was created the last.
1
PerhapsSQL_BACKUPS.max_by { |f| f[:ctime] }.name
if "or" in "and / or" in the question.
– Cary Swoveland
Nov 16 '18 at 19:36
@cary That even better. But in my case I needed the complete ordered list.
– Minato
Nov 16 '18 at 19:50
add a comment |
I know you have timestamped files but, if one doesn't have a timestamp or a way to distinguish the creation times, the method below is what I used for non-timestamped files.
File::Stat
has a method ctime
(returns the creation time), So it can be done like.
SQL_BACKUPS=Dir["./config/schema/*_#{DB_NAME}.sql"].map { |f| {name: f, ctime: File::Stat.new(f).ctime } }
sorted = SQL_BACKUPS.sort_by { |f| f[:ctime] }
sorted.last.name # gives the one that was created the last.
I know you have timestamped files but, if one doesn't have a timestamp or a way to distinguish the creation times, the method below is what I used for non-timestamped files.
File::Stat
has a method ctime
(returns the creation time), So it can be done like.
SQL_BACKUPS=Dir["./config/schema/*_#{DB_NAME}.sql"].map { |f| {name: f, ctime: File::Stat.new(f).ctime } }
sorted = SQL_BACKUPS.sort_by { |f| f[:ctime] }
sorted.last.name # gives the one that was created the last.
answered Nov 16 '18 at 16:56
Minato
2,65811422
2,65811422
1
PerhapsSQL_BACKUPS.max_by { |f| f[:ctime] }.name
if "or" in "and / or" in the question.
– Cary Swoveland
Nov 16 '18 at 19:36
@cary That even better. But in my case I needed the complete ordered list.
– Minato
Nov 16 '18 at 19:50
add a comment |
1
PerhapsSQL_BACKUPS.max_by { |f| f[:ctime] }.name
if "or" in "and / or" in the question.
– Cary Swoveland
Nov 16 '18 at 19:36
@cary That even better. But in my case I needed the complete ordered list.
– Minato
Nov 16 '18 at 19:50
1
1
Perhaps
SQL_BACKUPS.max_by { |f| f[:ctime] }.name
if "or" in "and / or" in the question.– Cary Swoveland
Nov 16 '18 at 19:36
Perhaps
SQL_BACKUPS.max_by { |f| f[:ctime] }.name
if "or" in "and / or" in the question.– Cary Swoveland
Nov 16 '18 at 19:36
@cary That even better. But in my case I needed the complete ordered list.
– Minato
Nov 16 '18 at 19:50
@cary That even better. But in my case I needed the complete ordered list.
– Minato
Nov 16 '18 at 19:50
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53340845%2fvagrant-ruby-script-to-return-name-of-last-created-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What are the current results you are seeing?
– Matt Schuchard
Nov 16 '18 at 15:39
1
I hope you're absolutely certain that the things you're interpolating in those strings can never, ever contain spaces or characters that might cause your shell to freak out.
– tadman
Nov 16 '18 at 15:50
@MattSchuchard return array with sql files.
– Salines
Nov 16 '18 at 15:53
1
@tadman The sql file names are predefined.
– Salines
Nov 16 '18 at 16:01
1
are you looking for
.sort.last
?– Stefan
Nov 16 '18 at 16:39