I’ve used this simple SQL Server backup script a number of times to backup sql server to a file or network drive. It’s ideal if you are running SQL Server Express for example which does not come equipped with maintenance plans. This script can be used in conjunction with SQLCMD to backup SQL Server from the command line.
So let’s take a look at this.
- Where source is the folder that you want to backup and destination is the backup on the network drive. The arguments tell xcopy how to handle certain situations. For my script I used /Y to suppress prompting when overwriting files. If you do not include this the script will ask you before copying each file, that wouldn’t be automated.
- The backup script will accomplish the following goals: Create a backup archive that is as easy to restore a single file as it is to restore an entire file system. The backup script will run autonomously. The only human intervention will be to swap media and review output.
Using scripts can be a powerful way to automate things that a GUI will fail in many cases. I was working with a client who wanted to write a PowerShell script that would help him automate his backups. This is a pretty simple requirement that can be easily achieved using SMO as shown below in my scripts. I have been using these scripts for a number of years at various places. Let us see how we. The Data Backup Windows Batch Script. I want to emphasize this script is very basic, as all it does is create backups by a utilizing a simple file copy. There are some configuration options you can set: The backup storage location where the resulting compressed backup files are stored.
What we want to achieve here is a way of executing the same backup database command for each database in SQL Server. The “bare bones” T-SQL used might look like this. In this example, I am using my local drive as the backup destination.
I say bare bones because the above command will backup a database to disk for you but there are a wide selection of different options which can be used to customize the backup. For more on that visit this link
We can query SQL Server for a list of the databases installed on the instance. You can filter out the databases which you are not interested in backing up.
So I get back this list..
I now want to write a simple loop to run through each of these names and execute the BACKUP DATABASE command producing a file on disk. For this, you could use a CURSOR or a WHILE LOOP. I’ll show you both ways.
Which one you choose is a matter of preference. Note that by default if you backup to the same file on disk, each backup is appended to the backup file. If you want to overwrite your backup files, use WITH INIT. Refer to the link above for more information on the syntax.
Backup SQL Server databases using a cursor
Backup SQL Server databases using a while loop
(Apologies if this code looks a little messy. I am looking for a better code formatting plugin – any suggestions? 🙂 )
Once you have customized the script to your needs, wrap it inside of a stored procedure. Depending on what version of SQL Server you running, you can either schedule this using SQL Server Agent or via the windows task scheduler by calling SQLCMD.
Backing up SQL Server using SQLCMD at the command line
Start a command prompt and type SQLCMD /? to get the help options up.
Connect to your SQL Server instance using the options available and then run the backup to test that it works ok.
You can set up a windows batch file to call SQLCMD. I could add the following line to a batch file and I am now ready to schedule this using windows task scheduler.
Next Steps
It’s important that you get the right plan in place for your server. What I have provided is just a basic script, call it a template if you will. You can customize it to include differential backups, transaction log backups, different file locations, multiple backup sets, multiple files etc.
Have a read of the BACKUP DATABASE documentation to get a idea for what is possible. Communicate with the business to agree requirements, write a backup plan and test whatever code you write thoroughly to ensure that it is meeting the backup plan requirements.
Finally
Script Backup Postgresql
Backing up using scripts is very handy for editions of SQL Server which do not come equipped with maintenance plans and SQL Server Agent. If you are running one of those editions that does have those features, then you might decide to choose those tools instead. Using backup scripts however still remain highly viable and are preferable to some DBA’s.
Related Posts:
MongoDB is a cross-platform, document-oriented NoSql database server. In this tutorial, you will find a shell script to backup MongoDB database. The script also remove backups from server older than specified days.
Download MongoDB Backup Script
You can copy or download this script directly from Github.
Also, you can copy this script below:
Script Backup
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 | ###################################################################### ## MongoDB Database Backup Script ## URL: https://tecadmin.net/shell-script-backup-mongodb-database/ ## ###################################################################### export PATH=/bin:/usr/bin:/usr/local/bin ###################################################################### ###################################################################### DB_BACKUP_PATH='/backup/mongo' MONGO_PORT='27017' # If mongodb is protected with username password. # and add MONGO_USER and MONGO_PASSWD values correctly AUTH_ENABLED=0 MONGO_PASSWD=' # Set DATABASE_NAMES to 'ALL' to backup all databases. # or specify databases names seprated with space to backup #DATABASE_NAMES='mydb db2 newdb' ## Number of days to keep local backup copy ###################################################################### ###################################################################### mkdir-p${DB_BACKUP_PATH}/${TODAY} AUTH_PARAM=' if[${AUTH_ENABLED}-eq1];then AUTH_PARAM=' --username ${MONGO_USER} --password ${MONGO_PASSWD} ' echo'You have choose to backup all databases' mongodump--host${MONGO_HOST}--port${MONGO_PORT}${AUTH_PARAM}--out${DB_BACKUP_PATH}/${TODAY}/ echo'Running backup for selected databases' do mongodump--host${MONGO_HOST}--port${MONGO_PORT}--db${DB_NAME}${AUTH_PARAM}--out${DB_BACKUP_PATH}/${TODAY}/ fi ######## Remove backups older than {BACKUP_RETAIN_DAYS} days ######## DBDELDATE=`date+'%d%b%Y'--date='${BACKUP_RETAIN_DAYS} days ago'` if[!-z${DB_BACKUP_PATH}];then if[!-z${DBDELDATE}]&& [ -d ${DBDELDATE} ];then fi ######################### End of script ############################## |
Schedule MongoDB Backup Script
You can easily schedule this script under crontab to backup databases regularly.