Back up local disk files

更新时间:
复制 MD 格式

You should regularly back up files on your local disks to prevent data loss or corruption caused by accidental deletion, modification, or overwrites. This practice improves data security. This topic describes three common methods to back up your files: using Cloud Backup for scheduled backups, backing up to OSS, and backing up to a cloud disk or NAS. This topic does not apply to backing up self-managed databases.

Important

This topic applies only to backing up files on local disks. To back up a database that is stored on your local disk, see Back up a self-managed database on an ECS instance.

Method 1: Use Cloud Backup for scheduled backups

Scenarios

Limitations

Features

Costs

Cloud Backup supports scheduled backups of files and folders on ECS instances, such as local disks and self-managed databases like Oracle, MySQL, and SQL Server. You can restore data when needed. This method is suitable for scenarios that require a highly reliable backup solution. For more information about Cloud Backup, see Why choose Cloud Backup?.

  • Not available in all regions. View supported regions

  • The Cloud Assistant Agent must be installed on the instance.

  • A convenient and efficient Software-as-a-Service (SaaS) backup service.

  • No scripting required.

  • File-level deduplication and compression to save storage costs.

  • Supports backup and restore.

Includes charges for File Backup software usage and storage capacity. For more information about billing, see ECS file backup costs.

Procedure

  1. Make preparations.

    • Ensure that the region where the local disk resides supports Cloud Backup. For a list of supported regions, see Available regions.

    • Ensure that the Cloud Assistant Agent is installed on the ECS instance where the local disk is located.

      Important

      If you purchased your instance after December 1, 2017, the Cloud Assistant client is pre-installed by default. Otherwise, you must install the Cloud Assistant Agent.

  2. Log on to the Cloud Backup console and select the region where the local disk is located.

  3. In the navigation pane on the left, choose Back Up > ECS File Backup. On the ECS Instances tab, find the instance that contains the local disk. In the Actions column, click Backup.

    image

  4. On the Create Backup Plan page, configure the parameters as prompted and click OK.

    Take note of the following parameters. Configure all other parameters as needed. For more information, see Back up ECS files:

    • Backup Folder Rule: Select Specified Folders.

    • Source Paths: Enter the absolute paths of the local disk data that you want to back up. You can specify multiple paths. For information about specific rules, refer to the on-screen prompts.

    • Backup Policy: Specifies the backup time, epoch, retention period, and other settings. If you have not created a backup policy, you must first create a backup policy.

    When the scheduled backup time is reached, the system starts the backup job. When the backup job Status changes to successful, the backup for the day is complete. You can view the backup point in the backup history.

    image

Related operations
  • Restore data: After you back up local disk data to a Cloud Backup vault, you can restore files from a historical backup point if the files are lost or become abnormal. For more information, see Restore ECS files.

  • Browse and download backed up files: For more information, see Back up ECS files.

Method 2: Regularly back up to OSS

You can use the ossutil and crontab commands with an automated script to regularly back up local disk data to OSS.

Scenarios

Features

Costs

Suitable for large-scale data backups, especially for solutions that require low costs and high reliability. For more information about the features of OSS, see Benefits of OSS.

Requires scripting.

Charges for OSS storage fees. For more information about billing, see Storage fees.

Important

This solution is a simple example that provides a basic approach. It has limitations, and you must adapt it to your business needs.

For example, this solution performs a full backup every time, which consumes an increasing amount of storage space over time. Packaging the entire directory into a single ZIP file can negatively affect backup speed and storage efficiency. In real-world business scenarios, you may need to implement additional policies, such as:

  • Incremental or differential backups: Back up only the data that has changed since the last backup. This method uses storage resources more efficiently and speeds up the backup process.

  • Chunked backups: Divide the dataset into smaller chunks or group them for backup based on directory structure, file type, or other logical criteria.

Procedure

  1. Make preparations.

    • Activate OSS and create an OSS bucket. For more information, see Create a bucket.

    • Obtain the OSS bucket name, the OSS endpoint, and the storage path of the local disk data you want to back up.

  2. Log on to the ECS instance.

  3. Install ossutil and configure access credentials.

    Important

    The ECS instance that has the local disk must have Internet access to download ossutil. For more information, see How do I enable Internet access for an ECS instance?

    1. Install ossutil.

      sudo yum install unzip -y
      sudo -v ; curl https://gosspublic.alicdn.com/ossutil/install.sh | sudo bash
    2. Configure access credentials for ossutil.

      Create a .ossutilconfig file in the user directory and configure the access credentials.

      sudo -i  # Switch to the root user. If the current user does not have sudo permissions, log on or escalate privileges using another method.
      
      cat <<EOF > /root/.ossutilconfig
      [Credentials]
      language=EN
      endpoint=YourEndpoint
      accessKeyID=YourAccessKeyId
      accessKeySecret=YourAccessKeySecret
      EOF

      Replace YourEndpoint, YourAccessKeyId, and YourAccessKeySecret with your actual values.

  4. Implement scheduled backups.

    1. Install a compression tool. This example uses zip.

      sudo yum install zip
    2. Write a backup script. For example, `backup_to_oss.sh`.

      Example script (for reference only, modify as needed): Backs up local disk data as a ZIP package to a specified path in an OSS bucket.

      • /path/to/your/local/data: Replace this with the directory of your local disk data.

      • your-bucket-name: Replace this with your OSS bucket name.

      • path/in/oss/to/store/backups/: Replace this with the directory in the OSS bucket where you want to store backups.

      • /path/to/backup_tmp/: This path is used to temporarily store the packaged ZIP file. The ZIP file is deleted to free up space after it is successfully uploaded to OSS. Replace this with a temporary backup directory that has sufficient space.

      #!/bin/bash
      
      LOCAL_DIR="/path/to/your/local/data/"
      BACKUP_TMP_DIR="/path/to/backup_tmp/"
      OSS_BUCKET="your-bucket-name"
      OSS_PREFIX="path/in/oss/to/store/backups/"
      SYNC_TIME_FILE="/var/tmp/last_backup.timestamp"
      OSSUTIL_PATH="/usr/bin/ossutil"
      LOG_FILE="/var/log/backup_to_oss.log"
      DATE_STAMP=$(date +%Y%m%d%H%M%S)
      ZIP_FILE_NAME="backup_$DATE_STAMP.zip"
      
      # Make sure the zip tool is installed
      if ! command -v zip &> /dev/null; then
          echo "zip command not found. Please install zip." >&2
          exit 1
      fi
      
      # Check if LOCAL_DIR exists and is not empty
      if [ -z "$(ls -A "$LOCAL_DIR")" ]; then
          echo "No files to backup in $LOCAL_DIR" | tee -a "$LOG_FILE"
          exit 0
      fi
      
      # Package the files to be backed up and catch any error output
      (cd "$LOCAL_DIR" && zip -r "$BACKUP_TMP_DIR/$ZIP_FILE_NAME" .) >> "$LOG_FILE" 2>&1 || {
          echo "Failed to create ZIP archive. Error: $(zip -r "$BACKUP_TMP_DIR/$ZIP_FILE_NAME" . 2>&1)" | tee -a "$LOG_FILE"
          exit 1
      }
      
      if [ $? -eq 0 ]; then
          # Use ossutil to upload the ZIP file
          OSS_PATH="oss://$OSS_BUCKET/$OSS_PREFIX$ZIP_FILE_NAME"
          if "$OSSUTIL_PATH" cp "$BACKUP_TMP_DIR/$ZIP_FILE_NAME" "$OSS_PATH" >> "$LOG_FILE" 2>&1; then
              echo "Uploaded: $ZIP_FILE_NAME" | tee -a "$LOG_FILE"
          else
              echo "Failed to upload: $ZIP_FILE_NAME" | tee -a "$LOG_FILE"
          fi
          rm "$BACKUP_TMP_DIR/$ZIP_FILE_NAME" # Delete the local ZIP file after a successful upload
      else
          echo "Failed to create ZIP archive." | tee -a "$LOG_FILE"
      fi
      
      # Record the time of this backup. This is updated even if the backup fails to avoid re-uploading the same content.
      date +%s > "$SYNC_TIME_FILE"
      echo "Backup process completed." | tee -a "$LOG_FILE"
  5. Grant execute permissions to the script and test it.

    sudo chmod +x /home/backup_to_oss.sh
    /home/backup_to_oss.sh

    Ensure that the script runs without errors and that the data is successfully uploaded to OSS.

  6. Run crontab -e to open the crontab editor. Add a line to schedule your backup script. For example, to run it at 2:00 AM every day:

    0 2 * * * /home/backup_to_oss.sh

    Replace /home/backup_to_oss.sh with the actual path to your script.

  7. Configure other settings as needed.

    • (Optional) Configure the script to run at startup.

      1. Create the backup_to_oss.service file.

        sudo vi /etc/systemd/system/backup_to_oss.service
      2. In the file, add the following content. After you add the content, press the Esc key, enter :wq, and then save and close the file.

        [Unit]
        Description=Back to OSS
        After=network.target
        
        [Service]
        ExecStart=/home/backup_to_oss.sh
        RestartSec=3
        Restart=always
        
        [Install]
        WantedBy=default.target
      3. Run the following command to reload the systemd configuration.

        sudo systemctl daemon-reload
      4. Run the following commands to start the service and enable it to run at startup.

        sudo systemctl start backup_to_oss.service
        sudo systemctl enable backup_to_oss.service
    • (Optional) Set a retention period for OSS backup files.

      1. Create a local file and configure a lifecycle rule in XML format within the file.

        vim OSSLifecycleConfig.xml

        Example rule (modify as needed): Files in the `test/` path of the bucket are retained for only 30 days. Files that are older than 30 days are deleted. For more information about the rule parameters, see Lifecycle.

        <?xml version="1.0" encoding="UTF-8"?>
        <LifecycleConfiguration>
          <Rule>
            <ID>test-rule1</ID>
            <Prefix>test/</Prefix>
            <Status>Enabled</Status>
            <Expiration>
              <Days>30</Days>
            </Expiration>
          </Rule>
        </LifecycleConfiguration>
      2. Use ossutil to read the lifecycle configuration and add it to the specified bucket.

        ossutil lifecycle --method put oss://bucketname OSSLifecycleConfig.xml

        Replace bucketname with your actual OSS bucket name.

Download backed up data

You can download backed up data from OSS using the OSS console, the ossutil command line interface, or other methods. For more information, see Simple download.

Method 3: Regularly back up to a disk or NAS on the same instance

You can regularly back up local disk data as a ZIP package to a specified path on a cloud disk or in a File Storage NAS file system.

Scenarios

Features

Costs

  • Cloud disk: Suitable for scenarios that require online storage and easy access to backup files.

  • NAS: Suitable for data sharing and backup, or scenarios that require fast access to backup data.

Requires scripting.

Important

This solution is a simple example that provides a basic approach. It has limitations, and you must adapt it to your business needs.

For example, this solution performs a full backup every time, which consumes an increasing amount of storage space over time. Packaging the entire directory into a single ZIP file can negatively affect backup speed and storage efficiency. In real-world business scenarios, you may need to implement additional policies, such as:

  • Incremental or differential backups: Back up only the data that has changed since the last backup. This method uses storage resources more efficiently and speeds up the backup process.

  • Chunked backups: Divide the dataset into smaller chunks or group them for backup based on directory structure, file type, or other logical criteria.

Procedure

  1. Make preparations.

    • Create a new cloud disk (data disk) for the instance that has the local disk, and then attach and initialize the cloud disk. Alternatively, you can attach a NAS file system.

      For more information, see Create and use a cloud disk or Create a NAS file system and attach it to an ECS instance.

    • Obtain the mount path of the cloud disk or NAS file system and the storage path of the local disk data that you want to back up.

  2. Set up scheduled backups.

    1. Log on to the ECS instance.

    2. Install the ZIP tool. The following example uses Alibaba Cloud Linux.

      sudo yum install zip
    3. Write a backup script. For example, `/home/backup_script.sh`.

      Run the following command to write and save the script.

      vim /home/backup_script.sh

      Example script (for reference only, modify as needed): Backs up local disk data as a ZIP package to a specified path.

      • /path/to/local_disk/: Replace this with the absolute path of the local disk data that you want to back up.

      • /path/to/backup/: Replace this with the destination path for the backup.

      #!/bin/bash
      
      # Configure variables
      LOCAL_DISK="/path/to/local_disk/"
      NAS_MOUNT="/path/to/backup/"
      ZIP_NAME="backup_$(date +%Y%m%d%H%M%S).zip"
      LOG_FILE="/var/log/backup_to_nas.log"
      
      # Make sure the ZIP tool is installed
      if ! command -v zip &> /dev/null; then
          echo "Error: zip command not found. Please install zip." >&2
          exit 1
      fi
      
      # Perform the backup
      echo "Starting backup at $(date)" >> "$LOG_FILE"
      zip -r "$NAS_MOUNT/$ZIP_NAME" "$LOCAL_DISK" >> "$LOG_FILE" 2>&1
      if [ $? -eq 0 ]; then
          echo "Backup completed successfully at $(date)" | tee -a "$LOG_FILE"
          echo "Backup file: $NAS_MOUNT/$ZIP_NAME" | tee -a "$LOG_FILE"
      else
          echo "Backup failed. Check log for details." >> "$LOG_FILE"
          exit 1
      fi
      
      # Clean up expired backups (Example: Retain backups from the last 30 days)
      
      # find "$NAS_MOUNT" -type f -name 'backup_*' -mtime +30 -delete >> "$LOG_FILE" 2>&1
      # if [ $? -eq 0 ]; then
      #    echo "Old backups cleaned up successfully." >> "$LOG_FILE"
      # else
      #    echo "Error occurred while cleaning up old backups. Check log for details." >> "$LOG_FILE"
      # fi
      
      echo "Backup process finished at $(date)" >> "$LOG_FILE"
    4. Save the script and grant execute permissions to the script.

      sudo chmod +x /home/backup_script.sh

      Replace /home/backup_script.sh with the actual path to your script.

    5. Run crontab -e to open the crontab editor. Add a line to schedule your backup script. For example, to run it at 2:00 AM every day:

      0 2 * * * /home/backup_script.sh

      Replace /home/backup_script.sh with the actual path to your script.

    6. (Optional) Configure the script to run at startup.

      1. Create the backup_script.service file.

        sudo vi /etc/systemd/system/backup_script.service
      2. In the file, add the following content. After you add the content, press the Esc key, enter :wq, and then save and close the file.

        [Unit]
        Description=Backup Files Script
        After=network.target
        
        [Service]
        ExecStart=/home/backup_script.sh
        
        [Install]
        WantedBy=default.target
      3. Run the following command to reload the systemd configuration.

        sudo systemctl daemon-reload
      4. Run the following commands to start the service and enable it to run at startup.

        sudo systemctl start backup_script.service
        sudo systemctl enable backup_script.service
Download backed up data

Related operations

  • Migrate local disk data to another ECS instance

    You can use a one-click solution to migrate all data from one or more instances that have local disks to other ECS instances. The data is stored on the cloud disks of the destination instances, which creates a complete backup. For more information, see Migrate a source server to a destination instance.

  • Handle local disk corruption

    If a local disk is corrupted, Alibaba Cloud triggers a system event and promptly sends you a notification that contains response measures and event lifecycle information. You can perform O&M operations based on the scenario. For more information, see O&M scenarios and system events for instances with local disks.