All Products
Search
Document Center

Elastic Compute Service:Back up files on a local disk

Last Updated:May 15, 2026

Back up local disk files regularly with Cloud Backup, OSS, or cloud disks and NAS to prevent data loss.

Important

This topic applies only to backing up files on a local disk. If your local disk stores a database and you need to back up the database, see Back up self-managed databases on ECS.

Method 1: Use Cloud Backup

Scenario

Limit

Benefit

Billing

Cloud Backup supports regular backup of files or directories on ECS instances (such as local disks or self-managed Oracle/MySQL/SQL Server databases) and data restoration when needed. Suitable for scenarios that require highly reliable backup. See Advantages of Cloud Backup?

  • Cloud Backup is not supported in all regions. See Supported regions.

  • Cloud Assistant Agent is installed on the instance.

  • SaaS-based, convenient and efficient

  • No scripts required

  • File-level deduplication and compression to reduce storage costs

  • Backup and recovery support

You are charged for file backup software usage and storage capacity. See ECS file backup fees.

Procedure

  1. Preparations.

    • Cloud Backup is supported in the region of the local disk. See Supported regions.

    • Cloud Assistant Agent is installed on the instance.

      Important

      Instances purchased after December 1, 2017 include Cloud Assistant Agent by default. If not installed, see Install Cloud Assistant Agent.

  2. Log on to the Cloud Backup console and select the region of the local disk.

  3. In the navigation pane on the left, choose Backup > ECS File Backup. On the ECS Instances tab, find the instance equipped with the local disk and click Backup in the Actions column.

    image

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

    Note the following configurations. For other parameters, see Create a backup plan to periodically back up ECS files:

    • Backup Folder Rule: Select Specified Folders.

    • Source Paths: The absolute path of the local disk data to back up. You can enter multiple paths. For rules, see the prompts.

    • Backup Policy: Select a policy that specifies the backup time, cycle, and retention period. If no policy exists, create one first. See Create a backup policy.

    When the scheduled backup time arrives, the system runs the backup job. If the Status shows Completed, the backup is done. You can view backup points in the backup history.

    image

Related operations

Method 2: Back up to OSS

Use ossutil and crontab to schedule automated backups from a local disk to OSS.

Scenario

Features

Billing

Suitable for large-scale data backup that requires low cost and high reliability. See Benefits.

Requires a custom script.

OSS storage fees apply. See Storage fees.

Important

This is a basic example with limitations. Enhance it to meet your specific business requirements.

For example, full backup each time increases storage usage, and packaging entire directories into ZIP files reduces backup speed. Consider custom backup policies such as:

  • Incremental or differential backups: Back up only modified data since the last backup to improve efficiency.

  • Block backup: Divide data into blocks by directory structure or file type before backup.

Procedure

  1. Preparations.

    • Activate OSS and create a bucket. See Create a bucket.

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

  2. Log on to the ECS instance.

  3. Install ossutil and configure the access credentials.

    Important

    The ECS instance must have Internet access to download ossutil. 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 ossutil access credentials.

      Create a .ossutilconfig file in the user directory and add the credentials.

      sudo -i  # Switch to the root user. If the current user does not have sudo permissions, use other logon methods or grant the permissions.
      
      cat <<EOF > /root/.ossutilconfig
      [Credentials]
      language=EN
      endpoint=YourEndpoint
      accessKeyID=YourAccessKeyId
      accessKeySecret=YourAccessKeySecret
      EOF

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

  4. Set up scheduled backup.

    1. Install the compression tool. In this example, zip is used.

      sudo yum install zip
    2. Create the backup script. In this example, the script is named backup_to_oss.sh.

      Sample script: compresses local disk data into a ZIP package and uploads it to the specified OSS bucket. Modify as needed.

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

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

      • path/in/oss/to/store/backups/: Replace with the OSS directory for storing backups.

      • /path/to/backup_tmp/: Temporary directory for ZIP files before upload. Files are deleted after successful upload. Replace with a 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"
      
      # Check whether 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 whether 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 that you want to back up and capture error outputs.
      (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 a 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 backup time 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 and test the script.

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

    Verify that the script runs correctly and data is uploaded to OSS.

  6. Open the crontab editor with crontab -e and add a schedule entry. For example, to run the backup at 02:00 daily:

    0 2 * * * /home/backup_to_oss.sh

    /home/backup_to_oss.sh: Replace with the actual script path.

  7. Configure additional settings as needed.

    • (Optional) Run the script on system startup.

      1. Create the backup_to_oss.service file.

        sudo vi /etc/systemd/system/backup_to_oss.service
      2. Add the following content. Press Esc, enter :wq, and press Enter to 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. Reload the systemd configuration:

        sudo systemctl daemon-reload
      4. Start the script and enable it on system startup:

        sudo systemctl start backup_to_oss.service
        sudo systemctl enable backup_to_oss.service
    • (Optional) Specify the retention period of backup files in OSS.

      1. Create a local file and configure lifecycle rules in XML format.

        vim OSSLifecycleConfig.xml

        The following example retains files in the test/ path for 30 days and deletes older files. Modify as needed. 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. Apply the lifecycle configuration to the bucket with ossutil.

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

        bucketname: Replace with your actual OSS bucket name.

Download backup data

Download backup data from OSS through the console or ossutil. See Simple download.

Method 3: Back up to a cloud disk or NAS

Compress local disk data into a ZIP package and back it up to a cloud disk or NAS file system on a schedule.

Scenario

Features

Billing

  • Cloud disk: suitable for online storage with easy access to backup files.

  • NAS file system: suitable for data sharing, backup, or quick access to backup data.

Requires a custom script.

Important

This is a basic example with limitations. Enhance it to meet your specific business requirements.

For example, full backup each time increases storage usage, and packaging entire directories into ZIP files reduces backup speed. Consider custom backup policies such as:

  • Incremental or differential backups: Back up only modified data since the last backup to improve efficiency.

  • Block backup: Divide data into blocks by directory structure or file type before backup.

Procedure

  1. Preparations.

  2. Set up scheduled backup.

    1. Log on to the ECS instance.

    2. Install the ZIP tool. In the following example, Alibaba Cloud Linux is used.

      sudo yum install zip
    3. Create the backup script. In this example, the script is stored at /home/backup_script.sh.

      Create and save the script:

      vim /home/backup_script.sh

      Sample script: compresses local disk data into a ZIP package and backs it up to a specified path. Modify as needed:

      • /path/to/local_disk/: Replace with the absolute path of the local disk data.

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

      #!/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 that 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
      
      # Back up data.
      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
      
      # Clear expired backups. For example, retain the backups within the previous 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.

      sudo chmod +x /home/backup_script.sh

      /home/backup_script.sh: Replace with the actual script path.

    5. Open the crontab editor with crontab -e and add a schedule entry. For example, to run the backup at 02:00 daily:

      0 2 * * * /home/backup_script.sh

      /home/backup_script.sh: Replace with the actual script path.

    6. (Optional) Run the script on system startup.

      1. Create the backup_script.service file.

        sudo vi /etc/systemd/system/backup_script.service
      2. Add the following content. Press Esc, enter :wq, and press Enter to save and close the file.

        [Unit]
        Description=Backup Files Script
        After=network.target
        
        [Service]
        ExecStart=/home/backup_script.sh
        
        [Install]
        WantedBy=default.target
      3. Reload the systemd configuration:

        sudo systemctl daemon-reload
      4. Start the script and enable it on system startup:

        sudo systemctl start backup_script.service
        sudo systemctl enable backup_script.service
Download backup data

References