DEV Community

Janak Shrestha
Janak Shrestha

Posted on

Data Backup for Developer

The jump host server hosts a directory named /data, serving as a repository for various developers non-confidential data. Developer james has requested a copy of their data stored in /data/james. The System Admin team has provided the following steps to fulfill this request:

a. Create a compressed archive named james.tar.gz of the /data/james directory.

b. Transfer the archive to the /home directory on the Jump Host Server.


Solution

Step 1: Connect to the Jump Host Server

ssh thor@jump-host
# Password: mjolnir123
Enter fullscreen mode Exit fullscreen mode

Step 2: Create the compressed archive

# Navigate to the /data directory and create the archive
cd /data
tar -czf james.tar.gz james/
Enter fullscreen mode Exit fullscreen mode

Command breakdown:

  • tar: Archive utility
  • -c: Create a new archive
  • -z: Compress with gzip
  • -f james.tar.gz: Output file name
  • james/: Directory to archive

Alternative: Create from any location with full path:

tar -czf /data/james.tar.gz -C /data james/
Enter fullscreen mode Exit fullscreen mode

Step 3: Move the archive to /home directory

mv /data/james.tar.gz /home/
Enter fullscreen mode Exit fullscreen mode

Step 4: Verify the operation

# Check if archive exists in /home
ls -lh /home/james.tar.gz

# Verify archive contents
tar -tzf /home/james.tar.gz | head -10

# Check file size
du -sh /home/james.tar.gz
Enter fullscreen mode Exit fullscreen mode

Complete One-Line Commands

From jump host directly:

cd /data && tar -czf james.tar.gz james/ && mv james.tar.gz /home/ && ls -lh /home/james.tar.gz
Enter fullscreen mode Exit fullscreen mode

With full paths:

tar -czf /data/james.tar.gz -C /data james/ && mv /data/james.tar.gz /home/ && ls -lh /home/james.tar.gz
Enter fullscreen mode Exit fullscreen mode

Step-by-Step Interactive Commands

# Connect to jump host
ssh thor@jump-host
# Password: mjolnir123

# Verify the source directory exists
ls -la /data/james/
# Should show files and directories

# Create the compressed archive
cd /data
tar -czf james.tar.gz james/

# Check the archive was created
ls -lh james.tar.gz
# Example output: -rw-r--r-- 1 thor thor 2.3M Jul 02 10:15 james.tar.gz

# Move the archive to /home
mv james.tar.gz /home/

# Verify it's in /home
ls -lh /home/james.tar.gz

# Check archive contents
tar -tzf /home/james.tar.gz | head -10

# Verify file integrity
tar -tzf /home/james.tar.gz > /dev/null && echo "✓ Archive is valid" || echo "✗ Archive is corrupted"

# Exit
exit
Enter fullscreen mode Exit fullscreen mode

Additional Options

Create archive with verbose output:

tar -czvf james.tar.gz james/
# -v shows files being added
Enter fullscreen mode Exit fullscreen mode

Create archive with timestamp:

tar -czf james_$(date +%Y%m%d).tar.gz james/
mv james_$(date +%Y%m%d).tar.gz /home/
Enter fullscreen mode Exit fullscreen mode

Create archive with maximum compression:

# Use gzip with maximum compression
tar -czf james.tar.gz --use-compress-program='gzip -9' james/
Enter fullscreen mode Exit fullscreen mode

Create archive excluding certain files:

tar -czf james.tar.gz --exclude='*.log' --exclude='temp/*' james/
Enter fullscreen mode Exit fullscreen mode

Create archive with absolute path preservation:

tar -czf james.tar.gz -P /data/james/
# Note: -P preserves absolute paths (use with caution)
Enter fullscreen mode Exit fullscreen mode

Verification Commands

Run these to verify everything is correct:

# 1. Check if archive exists in /home
ls -lh /home/james.tar.gz

# 2. View archive contents
tar -tzf /home/james.tar.gz

# 3. Count files in archive
tar -tzf /home/james.tar.gz | wc -l

# 4. Check archive size
du -h /home/james.tar.gz

# 5. Verify archive integrity
tar -tzf /home/james.tar.gz > /dev/null 2>&1
echo $?  # Should return 0

# 6. Compare with original directory
echo "Original directory size:"
du -sh /data/james/
echo "Archive size:"
du -sh /home/james.tar.gz

# 7. Check file permissions
ls -l /home/james.tar.gz

# 8. Extract to a temporary location to test
mkdir /tmp/test_extract
tar -xzf /home/james.tar.gz -C /tmp/test_extract
ls -la /tmp/test_extract/
rm -rf /tmp/test_extract
Enter fullscreen mode Exit fullscreen mode

Expected Output

thor@jump-host ~$ cd /data
thor@jump-host /data$ ls -la james/
total 24
drwxr-xr-x 3 james james 4096 Jul 02 10:00 .
drwxr-xr-x 5 root  root  4096 Jul 02 09:55 ..
-rw-r--r-- 1 james james 1024 Jul 02 10:00 config.yml
-rw-r--r-- 1 james james 2048 Jul 02 10:00 data.txt
drwxr-xr-x 2 james james 4096 Jul 02 10:00 projects
-rw-r--r-- 1 james james 512  Jul 02 10:00 readme.md

thor@jump-host /data$ tar -czf james.tar.gz james/
thor@jump-host /data$ ls -lh james.tar.gz
-rw-r--r-- 1 thor thor 2.3M Jul 02 10:15 james.tar.gz

thor@jump-host /data$ mv james.tar.gz /home/
thor@jump-host /data$ ls -lh /home/james.tar.gz
-rw-r--r-- 1 thor thor 2.3M Jul 02 10:16 /home/james.tar.gz

thor@jump-host /data$ tar -tzf /home/james.tar.gz | head -5
james/
james/config.yml
james/data.txt
james/projects/
james/projects/project1/
Enter fullscreen mode Exit fullscreen mode

Troubleshooting

  1. "tar: james/: Cannot stat: No such file or directory":
   # Check if directory exists
   ls -la /data/james/
   # If not, check if it's in a different location
   find /data -name james -type d
Enter fullscreen mode Exit fullscreen mode
  1. "Permission denied":
   # Check permissions
   ls -la /data/james/
   # If needed, adjust permissions
   sudo chmod -R 755 /data/james/
Enter fullscreen mode Exit fullscreen mode
  1. "No space left on device":
   # Check disk space
   df -h /data /home
   # Clean up if needed
Enter fullscreen mode Exit fullscreen mode
  1. Archive too large:
   # Use tar with compression level 9 (maximum)
   tar -czf james.tar.gz --use-compress-program='gzip -9' james/
Enter fullscreen mode Exit fullscreen mode
  1. Preserve file ownership:
   # Use sudo to preserve ownership
   sudo tar -czf james.tar.gz james/
Enter fullscreen mode Exit fullscreen mode

Complete Script

Create a script archive_james.sh:

#!/bin/bash

echo "Creating archive of /data/james directory..."

# Check if source directory exists
if [ ! -d "/data/james" ]; then
    echo "ERROR: /data/james directory not found!"
    exit 1
fi

# Create the archive
cd /data
echo "Compressing /data/james directory..."
tar -czf james.tar.gz james/

# Check if archive was created successfully
if [ $? -eq 0 ]; then
    echo "✓ Archive created successfully: $(ls -lh james.tar.gz | awk '{print $5}')"
else
    echo "✗ Failed to create archive"
    exit 1
fi

# Move to /home
echo "Moving archive to /home..."
mv james.tar.gz /home/

# Verify
if [ -f "/home/james.tar.gz" ]; then
    echo "✓ Archive moved to /home successfully"
    echo "Archive location: /home/james.tar.gz"
    echo "Archive size: $(ls -lh /home/james.tar.gz | awk '{print $5}')"
    echo ""
    echo "Archive contents (first 5 files):"
    tar -tzf /home/james.tar.gz | head -5
else
    echo "✗ Failed to move archive to /home"
    exit 1
fi

echo ""
echo "✅ Task completed successfully!"
Enter fullscreen mode Exit fullscreen mode

Make it executable and run:

chmod +x archive_james.sh
./archive_james.sh
Enter fullscreen mode Exit fullscreen mode

Security and Best Practices

  1. Verify the archive integrity:
   tar -tzf /home/james.tar.gz > /dev/null 2>&1 && echo "Integrity check passed"
Enter fullscreen mode Exit fullscreen mode
  1. Create checksum for verification:
   md5sum /home/james.tar.gz > /home/james.tar.gz.md5
   # Verify later
   md5sum -c /home/james.tar.gz.md5
Enter fullscreen mode Exit fullscreen mode
  1. Set proper permissions:
   chmod 644 /home/james.tar.gz
   chown thor:thor /home/james.tar.gz
Enter fullscreen mode Exit fullscreen mode
  1. Document the archive:
   echo "Archive created on $(date) by $(whoami)" > /home/james_archive_info.txt
   echo "Original path: /data/james" >> /home/james_archive_info.txt
   echo "Size: $(du -sh /home/james.tar.gz | awk '{print $1}')" >> /home/james_archive_info.txt
Enter fullscreen mode Exit fullscreen mode

Complete Solution Summary

The task has been completed on the Jump Host Server:

  • ✅ Created compressed archive james.tar.gz of /data/james directory
  • ✅ Archive created using tar -czf with gzip compression
  • ✅ Archive moved to /home directory
  • ✅ Verified archive exists in /home with correct size
  • ✅ Archive integrity verified

The developer james can now access their data from /home/james.tar.gz on the Jump Host Server.

Top comments (0)