Automated Backup on Windows. Updated to reflect changes from 8.3 to 11 This method uses pgdump.exe along with a batch file to call it. This batch file will create a file/directory for each day it is run. Keep in mind pgdump and pgdumpall are version specific meaning do not use pgdump from 9.3 to backup version 11.0. Batch script to make backup folder. Only new and modified files. Assuming the batch script is called backup.bat. Split long commands in multiple lines through. I have a script a friend wrote to back up profiles to the H drive I have another to back up everything from H to the new PC I was wondering if this would still work for win10 as this was for win7?
I am using adityasatrio's batch file to backup local MySQL dbs and like to be able to only keep the 30 latest backup files. Using root:root for this example.
Now I have had a good look at the following questions:
and am now wondering if I can simply add (https://stackoverflow.com/a/14267137/1010918)
(yes I will later change echo to del but first I like to see what will happen)
or (https://stackoverflow.com/a/13368077)
at the end of the batch file, right under
to make this happen?
I have never done this before but have searched for automated local backup apps/php scripts/mysqldump commands/etc for MySQL dbs, even had a go with Workbench only to discover that no scheduling can be set in the community edition (thank you Oracle).
All the other apps either need to have someone open the app and hit 'run now' or want you to pay for setting up a schedule (no thanks).
I think this can be done with the tools at hand on a Windows 7 and later versions machine. Please help me add this functionality to the script, that would be great, thank you.
edit1:
When adding the quote commands nothing happens.Also the created backup directory only displays the time but not the year, month and day. Doing further research to find out why. Any ideas?
This comment delete all but X most recent folders talks about deleting the 5 latest folders, though when I use it like this
for /f 'skip=2 delims=' %%a in ('dir %backupDir%%dirName% /o-d /b') do rd /S /Q '%backupDir%%dirName%%%a'
the error is the following.
edit2:Below is the code that with @foxidrive help sets the folder name as I like to have it, but the last bit, trying to only keep the 3 latest folders (for testing purposes only 3) and delete the rest of the folders in the backupDir does not seem to work out.
Thank you for any help.
With the help of @foxidrive above I managed to get the date of the folders as I wanted them to be, them being YYYY-MM-DD HH-MIN-SEC.
In these folders are the the gzipped .sql databses stored thanks to adityasatrio's MySQL Backup Batch Script.
With the help of @Magoo from this answer https://stackoverflow.com/a/17521693/1010918 I managed to get all folders (nameDir) deleted while keeping the latest N folders (nameDir) and also not touching any files that might be in the directory (backupDir).
Here is the complete working script.
Feel free to remove any occurrence of pause
and and echo
to not see what is going on inside the command prompt.
Additionally add this to Windows Task Scheduler and you have yourself a solid backup solution for a local development environment that makes use of MySQL databases.
Please thank the people that helped me get this done. Without you guys I would have had to use a costly Windows app only to locally save MySQL databases.
(.and for our next trick we are going to email an error log to ourselves if there are errors while backing up the .sql files. but that is another question and story for another day. )
This is a little more resilient to spaces in folder names, and the date and time routines have been altered
- run it and first check that the 'dirName'=
folder is in the right format
- and the line at the end should echo the del
commands for keeping the lastest 3 backups.
Test the archiving routine and then
remove the echo
before the del
keyword if it all looks right to you.