backup – TecAdmin https://tecadmin.net How to guide for System Administrator's and Developers Tue, 17 Jan 2023 02:28:25 +0000 en-US hourly 1 https://wordpress.org/?v=6.1.1 Backing Up Your Linux System with Rsync: A Step-by-Step Guide https://tecadmin.net/backup-linux-system/ https://tecadmin.net/backup-linux-system/#respond Tue, 17 Jan 2023 02:28:25 +0000 https://tecadmin.net/?p=23062 For many computer users, the most stressful part of working with a Linux system is having to back up their data. The good news is that there is a simple solution to this problem: set up an automatic rsync backup script that will automatically keep your data safe. In this article, we will go over [...]

The post Backing Up Your Linux System with Rsync: A Step-by-Step Guide appeared first on TecAdmin.

]]>
For many computer users, the most stressful part of working with a Linux system is having to back up their data. The good news is that there is a simple solution to this problem: set up an automatic rsync backup script that will automatically keep your data safe. In this article, we will go over the tools and steps that you need to take to set up an automated backup system on a Linux system with rsync. You will learn how to use rsync to automatically create backups of files, how to keep these backups up-to-date, and how to restore them in the event of data loss or corruption.

If you regularly perform backups on your Linux system, chances are you already know about rsync, a command-line utility that can be used to back up and synchronize files and directories. However, if you’re new to rsync, it might come as a surprise that this simple command is capable of backing up your entire Linux system. In this guide, we’ll show you how to use rsync to back up your Linux system using different strategies.

Steps to Backup Your Linux System

  1. Prepare a Backup Device
  2. To make a full backup of the system, you need a device that has much space to keep all files. A backup device can be a locally attached drive, network device, or cloud storage like Amazon S3, Azure Spaces, etc.

    Create a directory to store the backup on the backup device. Assuming you have attached a separate disk in your local machine mounted at /mnt directory.

    mkdir /mmnt/full-backup 
    

  3. Install Rsync Utility
  4. Rsync is a command line utility that helps to synchronize content between two directories. They either exist on the local system or one can be the remote. You can quickly install on using the default package manager on most modern Linux distributions. To install Rsync on Debian-based systems, type:

    sudo apt install rsync 
    

  5. Backup Your System
  6. You can run the command directly to make a backup of the complete system. For example, to create a backup of the system to the “/mnt/full-backup” directory, run the following command.

    sudo rsync -aAXv / --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} /mnt/full-backup 
    

    The above command will backup the entire root (/) directory, excluding /dev, /proc, /sys, /tmp, /run, /mnt, /media, and /lost+found directories, and save the data in /mnt/full-backup folder. Here:

    The `-aAXv` options are used so that the files are transferred in “archive” mode, which ensures that symbolic links, devices, permissions, ownerships, modification times, ACLs, and extended attributes are preserved.

  7. Automate the Backup
  8. It’s good practice to schedule automatic backups. You can simply add the above command in crontab, or write them in a shell script and then schedule the script.

    #!/usr/bin/evn bash
    
    BACKUP_PATH="/mnt/full-backup"
    EXCLUDE_DIR='{"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"}'
    SOURCE_DIR="/"
    
    sudo rsync -aAXv ${SOURCE_DIR} --exclude=${EXCLUDE_DIR} ${BACKUP_PATH}
    
    if [ $? -eq 0 ]; then
        echo "Backup completed successfully"
    else
        echo "Some error occurred during backup"
    fi

    You can schedule the script to run automatically using a tool such as cron. This will allow you to create regular backups of your system without having to manually run the script.

    To schedule the script, edit the crontab:

    crontab -e 
    

    Append the following entry. Make sure to set the correct script name and path. The below schedule will run the script at 02 AM every day.

    0  2  *  *  *  bash backup.sh >> backup.log

    Save and close the editor.

    Conclusion

    Now that you know how to use rsync, you may want to take advantage of its advanced features. For instance, you can use rsync to efficiently copy files from one directory to another. You can also generate incremental backups that allow you to quickly recover files at any time. If you want even more control over your backup process, you can even schedule backups.

    The post Backing Up Your Linux System with Rsync: A Step-by-Step Guide appeared first on TecAdmin.

    ]]> https://tecadmin.net/backup-linux-system/feed/ 0 Backup MySQL Databases to Amazon S3 (Shell Script) https://tecadmin.net/backup-mysql-database-to-amazon-s3-shell-script/ https://tecadmin.net/backup-mysql-database-to-amazon-s3-shell-script/#respond Sat, 28 May 2022 06:02:54 +0000 https://tecadmin.net/?p=29616 A shell script is a collection of commands to perform a specific job. MySQL is a relational database management system widely used on Linux systems. Amazon S3 is a cloud storage device provided by Amazon Web Services. It’s a good practice for the system administrator to back up databases at regular intervals and store them [...]

    The post Backup MySQL Databases to Amazon S3 (Shell Script) appeared first on TecAdmin.

    ]]>
    A shell script is a collection of commands to perform a specific job. MySQL is a relational database management system widely used on Linux systems. Amazon S3 is a cloud storage device provided by Amazon Web Services. It’s a good practice for the system administrator to back up databases at regular intervals and store them in a remote location like Amazon S3.

    This tutorial contains a shell script that creates MySQL databases backup and uploads them to Amazon S3 buckets. You can also use this shell script to back up MariaDB or Amazon Aurora (MySQL compatible) databases.

    Backup MySQL Databases to S3

    Use the below step-by-step tutorial to back up the MySQL databases and upload them to the Amazon S3 bucket.

    1. Install AWS CLI

    In order to use this script, the system must have AWS CLI installed.

    https://tecadmin.net/installing-aws-cli-in-linux/

    2. Create S3 Bucket

    Login to AWS Management Console and create a new s3 bucket.

    Alternatively, you can also create s3 bucket via AWS CLI. The command will be like:

    aws s3api create-bucket --bucket s3-bucket-name --region us-east-1 
    

    Just replace the bucket name and region.

    3. Shell Script to Backup MySQL database to S3

    Copy the below shell script to a file like db-backup.sh. This script uses mysqldump command to create databases backups. Then use gzip command to archive backup files and finally use aws command to upload backup files to Amazon S3 bucket.

    Create a file like /backup/scripts/s3-backup-mysql.sh in edit your favorite text editor. Then add the below content:

    #!/usr/bin/env bash
    
    #########################################################################
    #########################################################################
    ###
    ####       Author: Rahul Kumar
    #####      Website: https://tecadmin.net
    ####
    #########################################################################
    #########################################################################
    
    # Set the folder name formate with date (2022-05-28)
    DATE_FORMAT=$(date +"%Y-%m-%d")
    
    # MySQL server credentials
    MYSQL_HOST="localhost"
    MYSQL_PORT="3306"
    MYSQL_USER="user"
    MYSQL_PASSWORD="password"
    
    # Path to local backup directory
    LOCAL_BACKUP_DIR="/backup/dbbackup"
    
    # Set s3 bucket name and directory path
    S3_BUCKET_NAME="s3-bucket-name"
    S3_BUCKET_PATH="backups/db-backup"
    
    # Number of days to store local backup files
    BACKUP_RETAIN_DAYS=30 
    
    # Use a single database or space separated database's names
    DATABASES="DB1 DB2 DB3"
    
    ##### Do not change below this line
    
    mkdir -p ${LOCAL_BACKUP_DIR}/${DATE_FORMAT}
    
    LOCAL_DIR=${LOCAL_BACKUP_DIR}/${DATE_FORMAT}
    REMOTE_DIR=s3://${S3_BUCKET_NAME}/${S3_BUCKET_PATH}
    
    for db in $DATABASES; do
       mysqldump \
            -h ${MYSQL_HOST} \
            -P ${MYSQL_PORT} \
            -u ${MYSQL_USER} \
            -p${MYSQL_PASSWORD} \
            --single-transaction ${db} | gzip -9 > ${LOCAL_DIR}/${db}-${DATE_FORMAT}.sql.gz
    
            aws s3 cp ${LOCAL_DIR}/${db}-${DATE_FORMAT}.sql.gz ${REMOTE_DIR}/${DATE_FORMAT}/
    done
    
    DBDELDATE=`date +"${DATE_FORMAT}" --date="${BACKUP_RETAIN_DAYS} days ago"`
    
    if [ ! -z ${LOCAL_BACKUP_DIR} ]; then
    	cd ${LOCAL_BACKUP_DIR}
    	if [ ! -z ${DBDELDATE} ] && [ -d ${DBDELDATE} ]; then
    		rm -rf ${DBDELDATE}
    
    	fi
    fi
    
    ## Script ends here

    Update all the necessary variables as per your system environment.

    4. How to run backup script

    Set the execute (x) permission on script:

    chmod +x s3-backup-mysql.sh 
    

    Then run the backup script.

    ./s3-backup-mysql.sh 
    

    5. Schedule backup script to run daily

    Schedule the shell script using crontab to run on a daily basis.

    crontab -e 
    

    Add the below settings to end of the file:

    # Run daily @ 2am
    0 2 * * * /backup/scripts/s3-backup-mysql.sh > /dev/null 2>&1
    

    Save the file and close it.

    Conclusion

    This tutorial provides you with a shell script to back up MySQL databases and upload them to the Amazon S3 bucket. That could be helpful for you to automate database backups and save a copy on cloud storage.

    The post Backup MySQL Databases to Amazon S3 (Shell Script) appeared first on TecAdmin.

    ]]>
    https://tecadmin.net/backup-mysql-database-to-amazon-s3-shell-script/feed/ 0
    How to Backup Website to Amazon S3 using Shell Script https://tecadmin.net/backup-website-to-amazon-s3-using-shell-script/ https://tecadmin.net/backup-website-to-amazon-s3-using-shell-script/#respond Wed, 23 Mar 2022 12:44:05 +0000 https://tecadmin.net/?p=28756 Amazon Simple Storage Service (Amazon S3) is an cloud based object storage device. It is a low cost storage widely used for the backup or static website content. You can use AWSCLI command line utility for managing s3 bucket and its content. In this tutorial, you will learn about backup a website to Amazon s3 [...]

    The post How to Backup Website to Amazon S3 using Shell Script appeared first on TecAdmin.

    ]]>
    Amazon Simple Storage Service (Amazon S3) is an cloud based object storage device. It is a low cost storage widely used for the backup or static website content.

    You can use AWSCLI command line utility for managing s3 bucket and its content. In this tutorial, you will learn about backup a website to Amazon s3 bucket using a shell script.

    Installing AWS CLI

    The AWS CLI packages are available under the default repositories on most of the Linux systems. You can install it by running one of the following commands:

    sudo dnf install awscli    ## Fedora, Redhat and CentOS
    sudo apt install awscli    ## Ubuntu, Debian and Linux Mint
    

    You can also another article to install latest AWS CLI on any Linux system.

    Once the installation finished, check the awscli version by executing:

    aws --version  
    

    Create A Shell Script

    Now, create a shell script file on your system and add the below content. For this tutorial, I created file using:

    nano /scripts/s3WebsiteBackup.sh   
    

    and added the following content:

    #/usr/bin/env bash
    
    ################################################################
    ##
    ## Shell script to archive website code and upload to S3 bucket.
    ## Written by: Rahul Kumar
    ## Website: https://tecadmin.net
    ##
    #################################################################
    
    
    S3_BUCKET_NAME=""
    DIR_TO_BACKUP="/var/www/html"
    BACKUP_FILENAME='website'
    
    TODAY=`date +%Y%m%d`
    YY=`date +%Y`
    MM=`date +%m`
    AWSCMD="/usr/local/bin/aws"
    TARCMD="/usr/bin/tar"
    
    ${TARCMD} czf /tmp/${BACKUP_FILENAME}-${TODAY}.tar.gz
    
    ${AWSCMD} cp /tmp/${BACKUP_FILENAME}-${TODAY}.tar.gz s3://${S3_BUCKET_NAME}/${YY}/${MM}/
    
    
    if [ $? -eq 0 ]; then
    	echo "Backup successfully uploaded to s3 bucket"
    else
        echo "Error in s3 backup"
    fi

    Make sure to update S3_BUCKET_NAME and DIR_TO_BACKUP in the script. You can also change the backup file name in BACKUP_FILENAME variable.

    Save file and close it. Now, you have a shell script to backup website content to s3 buckets.

    Running Shell Script

    Make the shell script executable by running the following command.

    chmod +x /scripts/s3WebsiteBackup.sh 
    

    Now, you can test the script by executing it manually.

    bash /scripts/s3WebsiteBackup.sh 
    

    On successful, backups will be uploaded to s3 bucket. Which you can view using aws s3 ls command.

    Schedule Script in Cron

    Next, schedule your script to crontab to automate this job. To edit the crontab of current user, type:

    crontab -e 
    

    Add the following entry to the crontab:

    0 2 * * * bash /scripts/s3WebsiteBackup.sh 
    

    Save file and close the editor.

    Wrap Up

    This tutorial provides you a shell script to backup website content to the S3 bucket. Also includes the instruction to run this script.

    The post How to Backup Website to Amazon S3 using Shell Script appeared first on TecAdmin.

    ]]>
    https://tecadmin.net/backup-website-to-amazon-s3-using-shell-script/feed/ 0
    How to Backup SQL Server Database https://tecadmin.net/backup-sql-server-database/ https://tecadmin.net/backup-sql-server-database/#respond Mon, 28 Jun 2021 02:53:50 +0000 https://tecadmin.net/?p=26293 SQL Server is a relational database management system developed by Microsoft. It provides a powerful graphical interface called SQL Server Management Studio (SSMS), which provides all the features to manage your databases. Also provides options to backup and restore the database using graphical interfaces. Also, we can execute the T-SQL statement through SSMS to take [...]

    The post How to Backup SQL Server Database appeared first on TecAdmin.

    ]]>
    SQL Server is a relational database management system developed by Microsoft. It provides a powerful graphical interface called SQL Server Management Studio (SSMS), which provides all the features to manage your databases. Also provides options to backup and restore the database using graphical interfaces. Also, we can execute the T-SQL statement through SSMS to take a backup of the database.

    How to Backup SQL Server Database

    We can backup the SQL Serer database either with the T-SQL statements or we can use the SSMS wizard process to take full, differential, or transactional backup of the database.

    Use one of the below options to backup the SQL Server database:

    1. Backup Database using T-SQL

    Launch SQL Server Management Studio and connect to the database server. Now open a query window and execute the following statement to back up the database in your local drive.

    BACKUP DATABSAE [Test_db] TO DISK = 'D:\backup\Test_db.bak'
    Go
    

    Here Test_db is the database name and D:\backup\Test_db.bak is the backup file name with full path.

    T-SQL Statement to Backup SQL Server Database

    The above screenshot shows a successful database backup in the SQL server using a T-SQL statement. The next option will help you to backup the SQL Server database with a graphical interface.

    2. Backup Database using SSMS

    SQL Server Management Studio provides an option to backup SQL Server databases manually. To back up a SQL server database follow the below steps.

    1. Launch Management Studio and Connect to SQL Server
    2. Under databases, Select your database to backup
    3. Right-click on database and navigate to >> Tasks >> Backup

      Backup SQL Server Database - Step1

    4. Under the Source option, Correct databse is selected in dropdownlist. The backup type is set to Full.
    5. Under the Destination section, Remove any entry that already exists by clicking the remove button available on the right side. Then Click Add button to provide the correct backup path with filename.
    6. See the below screenshot, taking a full database backup of “Test_db” at path D:\backup\Test_db.bak.

      Backup SQL Server Database - Step2

    7. Once the backup completed successfully, you will see a message like below.

      Backup SQL Server Database Completed

    Conclusion

    In this tutorial, you have learned two methods to backup a SQL Server database. Next, you may like our tutorial to restore SQL Server database with T-SQL and SQL Server Management Studio

    The post How to Backup SQL Server Database appeared first on TecAdmin.

    ]]>
    https://tecadmin.net/backup-sql-server-database/feed/ 0
    How To Import and Export MySQL Database https://tecadmin.net/how-to-import-and-export-mysql-database/ https://tecadmin.net/how-to-import-and-export-mysql-database/#respond Thu, 04 Feb 2021 16:29:03 +0000 https://tecadmin.net/?p=24493 MySQL is an relation database management system to storing data in table format. It is an opensource database server available to install on variety of operating systems In case of mysql database migration, you can easily create a dump of database and restore it on target database server. MySQL server provides console utilities to export [...]

    The post How To Import and Export MySQL Database appeared first on TecAdmin.

    ]]>
    MySQL is an relation database management system to storing data in table format. It is an opensource database server available to install on variety of operating systems

    In case of mysql database migration, you can easily create a dump of database and restore it on target database server. MySQL server provides console utilities to export and import databases.

    This tutorial help you to export MySQL database using system console. Also helped you to restore database from dump file.

    Step 1 — Export MySQL Database

    Use mysqldump command line utility to perform a database backp. Which makes the process more easier to transfer database to other system. In order to export database, you need database’s name and login credentials with at least read-only privileges to the databases.

    Let’s export your database using mysqldump:

    mysqldump -u root -p database_name > db_backup.sql 
    

    Here:

    • root – is the username to login to database server
    • database_name – is the name of the database to export
    • db_backup.sql -is the text file name, that will stores the output

    The above command will run silently with no output on screen. If any errors occur during the export process, mysqldump will print them to the screen.

    Step 2 — Verify Backup File

    Let’s, verify the database dump file created in above step. First make sure there is no error displayed on screen with mysqldump command.

    Next, Run the following command:

    head db_backup.sql 
    

    This will show you the database details like below

    -- MySQL dump 10.13  Distrib 8.0.23, for Linux (x86_64)
    --
    -- Host: localhost    Database: mysql
    -- ------------------------------------------------------
    -- Server version       8.0.23
    
    /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
    /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
    /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
    /*!50503 SET NAMES utf8mb4 */;
    

    Next, Run the following command to view the last line from the backup file.

    tail db_backup.sql 
    
    /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
    /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
    /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
    
    -- Dump completed on 2021-02-02 17:07:24
    

    The last line must contain a message like “Dump completed” followed by the date time.

    Step 3 — Import MySQL Database

    Before importing the data from backup file, make sure to create database on database server.

    You can use “mysqladmin” console command to create a new database. To create a new database, execute:

    mysqladmin -u root -p create new_database_name 
    

    Enter mysql user password to complete process. This will create a database in mysql server.

    Next, you can import the dump file using “mysql” console command. The command will be like below:

    mysql -u root -p new_database_name < db_backup.sql
    

    Here:

    • root - is the username to access database server
    • database_name - is the name of the newly created database
    • db_backup.sql -is the dump file name taken from source database server.

    On successfuly command execution, you will get the command prompt back without any message. In case of any error occurs with restore process, the error message will be printed on terminal screen.

    Now, you can connect your database and access database tables from mysql shell.

    Conclusion

    In this tutorial, you have learned to create dump of mysql database. Then create a new database on destination and restore from backup file.

    You can visit mysqldump official documentation page to read more about this command.

    The post How To Import and Export MySQL Database appeared first on TecAdmin.

    ]]>
    https://tecadmin.net/how-to-import-and-export-mysql-database/feed/ 0
    A Shell Script to Backup MongoDB Database https://tecadmin.net/shell-script-backup-mongodb-database/ https://tecadmin.net/shell-script-backup-mongodb-database/#comments Sun, 21 Jun 2020 02:42:29 +0000 https://tecadmin.net/?p=21810 Did you know that MongoDB databases have a built-in backup mechanism that is accessible via shell or the mongod process? The mongod process automatically takes a snapshot every time a database transitions to another state. These different states are: starting, stopping, upgrading, and recovering after a crash. However, these snapshots won’t be enough in case [...]

    The post A Shell Script to Backup MongoDB Database appeared first on TecAdmin.

    ]]>
    Did you know that MongoDB databases have a built-in backup mechanism that is accessible via shell or the mongod process? The mongod process automatically takes a snapshot every time a database transitions to another state. These different states are: starting, stopping, upgrading, and recovering after a crash. However, these snapshots won’t be enough in case of catastrophic failures such as disk corruption or natural disaster. To protect your valuable data from such threats, it is advisable to implement an automated backup strategy for your MongoDB databases.

    In this article, we will discuss how to create automated backups for your MongoDB databases using a simple Shell script.

    Shell Script for MongoDB Backup

    The shell script for MongoDB database backup is available on Github. You can use the below link to get access of the shell script.

    https://github.com/tecrahul/shell-scripts/blob/master/backup-mongo.sh

    Alternatively, You can copy the below script and save it on your Linux system.

    #!/bin/bash
     
    ######################################################################
    ##
    ##   MongoDB Database Backup Script 
    ##   Written By: Rahul Kumar
    ##   URL: https://tecadmin.net/shell-script-backup-mongodb-database/
    ##   Update on: June 20, 2020
    ##
    ######################################################################
     
    export PATH=/bin:/usr/bin:/usr/local/bin
    TODAY=`date +"%d%b%Y"`
     
    ######################################################################
    ######################################################################
     
    DB_BACKUP_PATH='/backup/mongo'
    MONGO_HOST='localhost'
    MONGO_PORT='27017'
    
    # If MongoDB is protected with a username password.
    # Set AUTH_ENABLED to 1 
    # and add MONGO_USER and MONGO_PASSWD values correctly
    
    AUTH_ENABLED=0
    MONGO_USER=''
    MONGO_PASSWD=''
    
    
    # Set DATABASE_NAMES to "ALL" to backup all databases.
    # or specify databases names separated with space to backup 
    # specific databases only.
    
    DATABASE_NAMES='ALL'
    #DATABASE_NAMES='mydb db2 newdb'
    
    ## Number of days to keep a local backup copy
    BACKUP_RETAIN_DAYS=30   
     
    ######################################################################
    ######################################################################
     
    mkdir -p ${DB_BACKUP_PATH}/${TODAY}
    
    AUTH_PARAM=""
    
    if [ ${AUTH_ENABLED} -eq 1 ]; then
    	AUTH_PARAM=" --username ${MONGO_USER} --password ${MONGO_PASSWD} "
    fi
    
    if [ ${DATABASE_NAMES} = "ALL" ]; then
    	echo "You have choose to backup all databases"
    	mongodump --host ${MONGO_HOST} --port ${MONGO_PORT} ${AUTH_PARAM} --out ${DB_BACKUP_PATH}/${TODAY}/
    else
    	echo "Running backup for selected databases"
    	for DB_NAME in ${DATABASE_NAMES}
    	do
    		mongodump --host ${MONGO_HOST} --port ${MONGO_PORT} --db ${DB_NAME} ${AUTH_PARAM} --out ${DB_BACKUP_PATH}/${TODAY}/
    	done
    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
          cd ${DB_BACKUP_PATH}
          if [ ! -z ${DBDELDATE} ] && [ -d ${DBDELDATE} ]; then
                rm -rf ${DBDELDATE}
          fi
    fi
     
    ######################### End of script ##############################

    Run Script Manually

    Save the above script in a file with .sh extension. I want to save all the backups under /the backup directory. So placed the shell script in the same directory. Then set the execute permission on the script.

    chmod +x /backup/backup-mongo.sh 
    

    Run the shell script as below:

    bash /backup/backup-mongo.sh 
    

    Schedule MongoDB Backup Script

    You can easily schedule this script under crontab to backup databases regularly. To edit the crontab, run crontab -e command and append the below code:

    ## Backup database daily at 02:00 AM
    0 2 * * * /backup/mongo-backup.sh

    Wrap Up

    In this tutorial, we have discussed a shell script that helps to backup MongoDB databases manually. Also, you can schedule scripts to backup databases on regular basis.

    The post A Shell Script to Backup MongoDB Database appeared first on TecAdmin.

    ]]>
    https://tecadmin.net/shell-script-backup-mongodb-database/feed/ 2
    Magento 2 Codebase & Database Backup Script https://tecadmin.net/magento2-backup-script/ https://tecadmin.net/magento2-backup-script/#respond Tue, 10 Mar 2020 16:56:33 +0000 https://tecadmin.net/?p=20672 This tutorial will help you to automate the Magento2 codebase and database backup process using a shell script. The script will perform automatic backups on a scheduled interval. The script also has the ability to remove older backups as per configuration. Setup Magerun2 You need to download and configure the Magerun2 script on your system. [...]

    The post Magento 2 Codebase & Database Backup Script appeared first on TecAdmin.

    ]]>
    This tutorial will help you to automate the Magento2 codebase and database backup process using a shell script. The script will perform automatic backups on a scheduled interval. The script also has the ability to remove older backups as per configuration.

    Setup Magerun2

    You need to download and configure the Magerun2 script on your system.

    wget https://files.magerun.net/n98-magerun2.phar
    mv n98-magerun2.phar /usr/local/bin/n98-magerun2
    chmod +x /usr/local/bin/n98-magerun2 
    

    Download Shell Script

    You can download Magento2 backup script from here. Alternativly, use below command to download script using wget command.

    wget https://tecadmin.net/wp-content/downloads/scripts/magento2-backup.sh
    

    You can also copy the script below and paste it in a file on your machine.

    #!/bin/bash
    
    #######################################################################################
    ##
    ##   Magento 2 database and codebase backup script
    ##   Written By: Rahul Kumar
    ##   Written on: Mar 06, 2020
    ##   Last Update: Mar 11, 2020
    ##
    #######################################################################################
    
    ################## Modify below values  ###############################################
    
    
    MAGENTO_DOCUMENT_ROOT="/var/www/magento2"
    BACKUP_PATH="/var/www/magento2/var/backups"
    
    BACKUP_RETAIN_DAYS=30     # Number of days to keep a local backup copy
    
    GZIP="/bin/gzip"
    RM="/bin/rm"
    MKDIR="/bin/mkdir"
    N98_MAGERUN2="/usr/local/bin/n98-magerun2"
    
    
    
    #######################################################################################
    ##################              Do not change below values              ###############
    
    export PATH=/bin:/usr/bin:/usr/local/bin
    TODAY="$(date "+%Y-%m-%d-%H-%M")"
    CURRENT_BACKUP_DIR="${BACKUP_PATH}/${TODAY}"
    
    #######################################################################################
    ##################              Functions               ###############################
    
    exit_on_error(){
            echo -e "$@"
            exit 99
    }
    
    maintenance_mode(){
            ${N98_MAGERUN2} sys:maintenance ${1} --skip-root-check --root-dir=${MAGENTO_DOCUMENT_ROOT}
    }
    
    check_cmds(){
        [ ! -x ${GZIP} ] && exit_on_error "FILENAME $GZIP does not exists. Make sure correct path is set in config section."
        [ ! -x ${RM} ] && exit_on_error "FILENAME $RM does not exists. Make sure correct path is set in config section."
        [ ! -x ${MKDIR} ] && exit_on_error "FILENAME $MKDIR does not exists. Make sure correct path is set config section."
        [ ! -x ${N98_MAGERUN2} ] && exit_on_error "FILENAME $N98_MAGERUN2 does not exists. \nDownload script from https://files.magerun.net/ and Make sure correct path is set in config section."
    }
    
    create_backup_dir(){
            [ ! -d ${CURRENT_BACKUP_DIR} ] && ${MKDIR} -p ${CURRENT_BACKUP_DIR}
    }
    
    database_backup(){
    
            ${N98_MAGERUN2} --skip-root-check --root-dir=${MAGENTO_DOCUMENT_ROOT} db:dump ${CURRENT_BACKUP_DIR}/database-${TODAY}.sql
    
            if [ $? -eq 0 ]; then
                    echo "Database backup successfully completed"
            else
                    maintenance_mode --off    ##### Disable mainenence even database backup failed
                    exit_on_error "Database backup failed. "
            fi
    }
    
    
    
    codebase_backup(){
    
            cd $MAGENTO_DOCUMENT_ROOT && \
            tar -cpzf ${CURRENT_BACKUP_DIR}/codebase-${TODAY}.tar.gz --exclude=var/* .
    
            if [ $? -eq 0 ]; then
                    echo "Codebase backup successfully completed"
            else
                    maintenance_mode --off    ##### Disable mainenence even codebase backup failed
                    exit_on_error "Codebase backup failed. "
            fi
    }
    
    
    cleanup_old_backup(){
    
            REMOVE_DIR_NAME=`date "+%Y-%m-%d-%H-%M" --date="${BACKUP_RETAIN_DAYS} days ago"`
    
            if [ ! -z ${BACKUP_PATH} ]; then
                      cd ${BACKUP_PATH}
                      if [ ! -z ${REMOVE_DIR_NAME} ] && [ -d ${REMOVE_DIR_NAME} ]; then
                                    rm -rf ${REMOVE_DIR_NAME}
                      fi
            fi
    }
    
    ########################################################################################
    ##################              Main (Calling functions)           #####################
    
    check_cmds
    create_backup_dir
    maintenance_mode --on
    database_backup
    codebase_backup
    maintenance_mode --off
    cleanup_old_backup
    
    
    ##########################################################################################
    ##################                      Script Ends Here                ##################
    ##########################################################################################
    

    Schedule Backup Scrpt

    Schedule this script using crontab on your system to run on a daily basis. Use below command to edit crontab configuration:

    crontab -e
    

    And add below entry at the end of file.

    0 0 * * * sh magento2-backup.sh
    

    Save file and close. You have successfully scheduled cronjob to run on 12:00 AM daily basis. To learn more about using the cronjob read this tutorial.

    The post Magento 2 Codebase & Database Backup Script appeared first on TecAdmin.

    ]]>
    https://tecadmin.net/magento2-backup-script/feed/ 0
    An Advance Bash Script for MySQL Database Backup https://tecadmin.net/advance-bash-script-for-mysql-database-backup/ https://tecadmin.net/advance-bash-script-for-mysql-database-backup/#comments Sun, 30 Jul 2017 03:23:27 +0000 https://tecadmin.net/?p=13273 After writing a simple shell script for MySQL database backup. This is our new advance bash script for MySQL database dump. This script will provide you to backup MySQL database and upload backups to various remote locations like FTP, SFTP and Amazon s3 bucket. This script is available on our Github account with all supporting [...]

    The post An Advance Bash Script for MySQL Database Backup appeared first on TecAdmin.

    ]]>
    After writing a simple shell script for MySQL database backup. This is our new advance bash script for MySQL database dump. This script will provide you to backup MySQL database and upload backups to various remote locations like FTP, SFTP and Amazon s3 bucket.

    This script is available on our Github account with all supporting file. You can simply download the script and run it on your server. Use the following steps to use this script and configure backups within 5 min.

    Step 1 – Clone this Repository

    Download this repository put files under /etc/mydumpadmin directory. Alternatively, you may also clone this repository under /etc directory using the following commands.

    cd /etc/
    git clone https://github.com/tecrahul/mydumpadmin.git
    

    Step 2 – Configure Setup

  9. Edit settings.conf file and update all required values as per your requirements. You can enable/disable FTP, SFTP and s3 backups as well
  10. Now edit credentials.txt file and put your mysql server login details.
  11. Step 3 – Execute Backup Script

    Set the execute permission on shell script and run this as following commands.

    chmod a+x mysql-dump.sh
    ./mysql-dump.sh
    

    Step 4 – Schedule Daily Cron

    You can also schedule this to run on daily basis using crontab. Add the following settings to crontab to run on 2:00 AM daily.

     0 2 * * * cd /etc/mydumpadmin && ./mysql-dump.sh
    

    The post An Advance Bash Script for MySQL Database Backup appeared first on TecAdmin.

    ]]>
    https://tecadmin.net/advance-bash-script-for-mysql-database-backup/feed/ 18
    How to Backup Running Virtual Machine in XenServer https://tecadmin.net/backup-running-virtual-machine-in-xenserver/ https://tecadmin.net/backup-running-virtual-machine-in-xenserver/#comments Mon, 01 Aug 2016 04:28:21 +0000 https://tecadmin.net/?p=5463 Backup running Virtual Machine in XenServer. I am working with Citrix XenServer for many years and managing all XenServers using XenCenter installed on a standalone windows machine. We regularly take backup of VMs manually until today, I always take backups after shutting down the VM. Most of VM owner getting disappointed due to server down [...]

    The post How to Backup Running Virtual Machine in XenServer appeared first on TecAdmin.

    ]]>
    Backup running Virtual Machine in XenServer. I am working with Citrix XenServer for many years and managing all XenServers using XenCenter installed on a standalone windows machine. We regularly take backup of VMs manually until today, I always take backups after shutting down the VM. Most of VM owner getting disappointed due to server down for a long time. While searching for the Google I found a better way to back up VMs without shutdown them. It means we can take running VM backups and not downtime occurred.

    citrix-image

    This tutorial we will help you step by step backup process of running VM. Also here is a shell script which can take all VMs backup or specified VM backup, which we can schedule through crontab as well.

    Method 1 – Manual Backup of Running VM

    Following steps also can be performed through XenCenter, But Linux lovers love the command line. So find commands to do it.

    1.1. Find VMs UUID

    Use the following command to get list of UUIDs of all vms along with other details. this UUID will be used in next step

    xe vm-list is-control-domain=false is-a-snapshot=false
    

    Sample Output:

    uuid ( RO)           : 8ac95696-94f3-83c1-bc89-8bb2603f832b
         name-label ( RW): test-vm
        power-state ( RO): running
    

    As per above output test-vm uuid is “8ac95696-94f3-83c1-bc89-8bb2603f832b“. It can be different in your case.

    1.2. Create VMs Snapshot

    Now use the following command to create snapshot of vm using uuid found in above step. Make sure you are using correct uuid.

    xe vm-snapshot uuid=8ac95696-94f3-83c1-bc89-8bb2603f832b new-name-label=testvmsnapshot
    

    Above command will retrun a UUID of snapshot, Use that UUID to convert snapshot to a vm, so we can export it to file using below command.

    xe template-param-set is-a-template=false ha-always-run=false uuid=b15c0531-88a5-98a4-e484-01bc89131561
    

    1.3. Export Snapshot to File

    Now we can export created snapshot to .xva file, Which can be easily restored from command line or XenCenter.

    xe vm-export vm=b15c0531-88a5-98a4-e484-01bc89131561 filename=vm-backup.xva
    

    1.4. Destroy Snapshot

    Finally as we have already taken backup to xva file, so we can destroy created snapshot from xenserver.

    xe vm-uninstall uuid=b15c0531-88a5-98a4-e484-01bc89131561 force=true
    

    Method 2 – Using Script for Backup Running VMs

    To backup all VMs running on XenServer, we can use following shell script also. This script mounted remote file system exported through NFS. This script works for me perfectly, but it may not for you. So use this script at your own risk.

    #!/bin/bash
    #
    # Written By: Mr Rahul Kumar
    # Created date: Jun 14, 2014
    # Last Updated: Mar 08, 2017
    # Version: 1.2.1
    # Visit: https://tecadmin.net/backup-running-virtual-machine-in-xenserver/
    #
    
    DATE=`date +%d%b%Y`
    XSNAME=`echo $HOSTNAME`
    UUIDFILE=/tmp/xen-uuids.txt
    NFS_SERVER_IP="192.168.10.100"
    MOUNTPOINT=/xenmnt
    FILE_LOCATION_ON_NFS="/backup/citrix/vms"
    
    ### Create mount point
    
    mkdir -p ${MOUNTPOINT}
    
    ### Mounting remote nfs share backup drive
    
    [ ! -d ${MOUNTPOINT} ]  && echo "No mount point found, kindly check"; exit 0
    mount -F nfs ${NFS_SERVER_IP}:${FILE_LOCATION_ON_NFS} ${MOUNTPOINT}
    
    BACKUPPATH=${MOUNTPOINT}/${XSNAME}/${DATE}
    mkdir -p ${BACKUPPATH}
    [ ! -d ${BACKUPPATH} ]  && echo "No backup directory found"; exit 0
    
    
    # Fetching list UUIDs of all VMs running on XenServer
    xe vm-list is-control-domain=false is-a-snapshot=false | grep uuid | cut -d":" -f2 > ${UUIDFILE}
    
    [ ! -f ${UUIDFILE} ] && echo "No UUID list file found"; exit 0
    
    while read VMUUID
    do
        VMNAME=`xe vm-list uuid=$VMUUID | grep name-label | cut -d":" -f2 | sed 's/^ *//g'`
    
        SNAPUUID=`xe vm-snapshot uuid=$VMUUID new-name-label="SNAPSHOT-$VMUUID-$DATE"`
    
        xe template-param-set is-a-template=false ha-always-run=false uuid=${SNAPUUID}
    
        xe vm-export vm=${SNAPUUID} filename="$BACKUPPATH/$VMNAME-$DATE.xva"
    
        xe vm-uninstall uuid=${SNAPUUID} force=true
    
    done < ${UUIDFILE}
    
    umount ${MOUNTPOINT}
    

    Download this script directly from Github.com

    The post How to Backup Running Virtual Machine in XenServer appeared first on TecAdmin.

    ]]>
    https://tecadmin.net/backup-running-virtual-machine-in-xenserver/feed/ 24
    How to Backup and Restore SVN Repository in Linux https://tecadmin.net/how-to-svn-subversion-dump-and-restore/ https://tecadmin.net/how-to-svn-subversion-dump-and-restore/#comments Sun, 11 Oct 2015 10:08:17 +0000 https://tecadmin.net/?p=1575 Subversion is the popular version management system widely used for application developments. As a system administrator, you must know the importance of backups. So keep the backup of your all svn repositories on the local server as well as on remote systems. This article will help you to backup and restore the svn repository on [...]

    The post How to Backup and Restore SVN Repository in Linux appeared first on TecAdmin.

    ]]>
    Subversion is the popular version management system widely used for application developments. As a system administrator, you must know the importance of backups. So keep the backup of your all svn repositories on the local server as well as on remote systems. This article will help you to backup and restore the svn repository on the Linux system through the command line.

    You can also set up your own SVN server on Debian based systems and Redhat based systems.

    Backup SVN Repository

    Subversion provides svnadmin utility for managing svn repositories. We can also take a backup of svn repositories using the svnadmin command.

    svnadmin dump /var/svn/myrepo > /backup/svn/myrepo.dump
    
    
    * Dumped revision 0.
    * Dumped revision 1.
    * Dumped revision 2.
    ....
    

    We can also compress backup with gzip and save disk space. Use the following command to backup the svn repository and compress it using gzip command.

    svnadmin dump /var/svn/myrepo | gzip -9 > /backup/svn/myrepo.dump.gz
    

    Restore SVN Repository

    Now if you are required to restore your svn repository from backup. Use the following example to restore the repository from a backup file. For this example, we are creating a new repository to restore the dump.

    First create a new repository using create option.

    svnadmin create /var/svn/mynewrepo
    

    Now restore backup to newly created repository using following command.

    svnadmin load /var/svn/mynewrepo < /backup/svn/myrepo.dump
    
    
    
    <<< Started new transaction, based on original revision 1
         * adding path : svn-auth-screen.PNG ... done.
         * adding path : template.txt ... done.
    
    ------- Committed revision 1 >>>
    
    <<< Started new transaction, based on original revision 2
         * adding path : file1.txt ... done.
         * adding path : file2.txt ... done.
    
    ------- Committed revision 2 >>>
    

    The post How to Backup and Restore SVN Repository in Linux appeared first on TecAdmin.

    ]]>
    https://tecadmin.net/how-to-svn-subversion-dump-and-restore/feed/ 4